# -*- coding: utf-8 -*-


__project_name__ = 'GmailURLParser'
__author__ = 'Erdinç Yılmaz'
__date__ = '10/22/14'


# import lxml.html
from lxml.html import fromstring
import re
# from bs4 import BeautifulSoup


# #--------------------------------------------------------------------------
# def extract_links_bs4(messageHTML):
#
#     # soup = BeautifulSoup(messageHTML, "html.parser")
#     # soup = BeautifulSoup(messageHTML, "html5lib")
#     soup = BeautifulSoup(messageHTML, "lxml")
#
#     links = []
#     for a in soup.findAll("a"):
#         link = a.get("href")
#         if link:
#             links.append(link)
#
#     return links

#---------------------------------------------------------------------
def extract_links_lxml(messageHTML):

    links = []

    dom = fromstring(messageHTML)
    for link in dom.xpath('//a/@href'):  # select the url in href for all a tags(links)
        links.append(link)
    return links


#---------------------------------------------------------------------
def extract_regex(messageTxt):

    links = []

    # match_urls = re.compile(r"""((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.‌​][a-z]{2,4}/)(?:[^\s()<>]+|(([^\s()<>]+|(([^\s()<>]+)))*))+(?:(([^\s()<>]+|(‌​([^\s()<>]+)))*)|[^\s`!()[]{};:'".,<>?«»“”‘’]))""", re.DOTALL)

    # re_match_urls = re.compile(r"""((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.‌​][a-z]{2,4}/)(?:[^\s()<>]+|(([^\s()<>]+|(([^\s()<>]+)))*))+(?:(([^\s()<>]+|(‌​([^\s()<>]+)))*)|[^\s`!()[]{};:'".,<>?«»“”‘’]))""", re.DOTALL)
    #And e.g. to add <a> tags for links could be done like this: re_match_urls.sub(lambda x: '<a href="%(url)s">%(url)s</a>' % dict(url=str(x.group())), string_to_match)

    GRUBER_URLINTEXT_PAT = re.compile(ur'(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?\xab\xbb\u201c\u201d\u2018\u2019]))')

    grup = GRUBER_URLINTEXT_PAT.findall(messageTxt)

    for link in grup:
        links.append(link[0])

    return links

    # for line in messageTxt.split(" "):
    #     print [mgroups[0] for mgroups in GRUBER_URLINTEXT_PAT.findall(line)]