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

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

Fix a bug with uppercase links
Fix a bug with uppercase links

  from lxml.html import fromstring as lxml_from_string
  from unidecode import unidecode
  
  
  UNSUBSCRIBE_MARKERS = [
  
    # English
-   "unsub", "blacklist", "opt-out", "opt out",
+   "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()
-         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):
+           if (pattern in link_content) or (pattern in link.lower()):
              unsubscribe_link = link
  
    return unsubscribe_link, unsubscribe_links
  
Fix a bug with uppercase links
## Code 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

## Instruction:
Fix a bug with uppercase links
## Code 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
