<commit_before>from lxml.html import fromstring as lxml_from_string
from unidecode import unidecode


UNSUBSCRIBE_MARKERS = [

  # English
  "unsub", "blacklist", "opt-out", "opt out",

  # French
  "desinscription", "desinscrire", "desabonner", "desabonnement",
  "ne souhaitez plus", "ne plus recevoir", "cesser de recevoir"

]


def FindUnsubscribeLink(message):

  unsubscribe_link = None
  unsubscribe_links = []

  for part in message.walk():
    if part.get_content_type() == 'text/html':
      html = part.get_payload(decode=True)

      doc = lxml_from_string(html)

      for element, attribute, link, pos in doc.iterlinks():
        link_content = unidecode(element.text_content()).lower()
        link = link.lower()

        unsubscribe_links.append((repr(link_content)[0:100], link[0:100]))
        for pattern in UNSUBSCRIBE_MARKERS:
          if (pattern in link_content) or (pattern in link):
            unsubscribe_link = link

  return unsubscribe_link, unsubscribe_links
<commit_msg>Fix a bug with uppercase links<commit_after>from lxml.html import fromstring as lxml_from_string
from unidecode import unidecode


UNSUBSCRIBE_MARKERS = [

  # English
  "unsub", "blacklist", "opt-out", "opt out", "removealert", "removeme",

  # French
  "desinscription", "desinscrire", "desabonner", "desabonnement",
  "ne souhaitez plus", "ne plus recevoir", "cesser de recevoir"

]


def FindUnsubscribeLink(message):

  unsubscribe_link = None
  unsubscribe_links = []

  for part in message.walk():
    if part.get_content_type() == 'text/html':
      html = part.get_payload(decode=True)

      doc = lxml_from_string(html)

      for element, attribute, link, pos in doc.iterlinks():
        link_content = unidecode(element.text_content()).lower()

        unsubscribe_links.append((repr(link_content)[0:100], link[0:100]))
        for pattern in UNSUBSCRIBE_MARKERS:
          if (pattern in link_content) or (pattern in link.lower()):
            unsubscribe_link = link

  return unsubscribe_link, unsubscribe_links

{'fraction_non_alphanumeric': 0.11269685039370078, 'fraction_numerical': 0.007874015748031496, 'mean_word_length': 3.8987951807228916, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3579516', 'n_tokens_mistral': 682, 'n_tokens_neox': 659, 'n_words': 159}