# Path: app/aml_utils.py
# def getpage(url):
#     """
#     Downloads the html page
# 
#     :rtype: tuple
#     :param url: the page address
#     :return: the header response and contents (bytes) of the page
#     """
#     http = httplib2.Http('.cache', disable_ssl_certificate_validation=True)
#     headers = {
#         'User-agent': 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36'}
# 
#     response, content = http.request(url, headers=headers)
#     return response, content
# 
# def parsepage(content):
#     """
#     Parses a single page and its contents into a BeautifulSoup object
# 
#     :param content: bytearray
#     :return soup: object
#     """
#     soup = BeautifulSoup(content, 'lxml')
#     return soup
# 
# def get_ns(key):
#     """
#     Gets a news_source filtered by key
#     :param key: the key of the news source (e.g: g1, uol, etc.)
#     :return: the news source object
#     """
#     ns = NewsSource.query.filter_by(
#         key=key).first()  # Fetch news source by key
#     return ns


from app.aml_utils import getpage, parsepage, get_ns

def __ny(soup):
    """
    DEPRECATED
   Gets the most read news from NY Times page
   :param soup: the BeautifulSoup object
   :return: a list with the most read news from the NY Times page
   """
    news = []
    divs = soup.find_all('div', class_='story')

    for d in divs[:10]:
        link = d.h3.a['href']
        title = d.h3.a.string

        news.append(dict(title=title, link=link))
    return news


# Strategy Pattern - a dictionary of functions. Key: the name of the News Source. Value: the Function to execute
strategies = dict(fox=__fox, wp=__wp, tg=__tg, lf=__lf, tt=__tt, ep=__ep, ny=__ny, reu=__reu)


def get_most_read(key):
    """
    Gets the most read news from a given page

    :param key: the key of the source page (e.g: g1)
    :return: a list with the most read news from the page and the name of news source
    """

# Path: app/aml_utils.py
# def getpage(url):
#     """
#     Downloads the html page
# 
#     :rtype: tuple
#     :param url: the page address
#     :return: the header response and contents (bytes) of the page
#     """
#     http = httplib2.Http('.cache', disable_ssl_certificate_validation=True)
#     headers = {
#         'User-agent': 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36'}
# 
#     response, content = http.request(url, headers=headers)
#     return response, content
# 
# def parsepage(content):
#     """
#     Parses a single page and its contents into a BeautifulSoup object
# 
#     :param content: bytearray
#     :return soup: object
#     """
#     soup = BeautifulSoup(content, 'lxml')
#     return soup
# 
# def get_ns(key):
#     """
#     Gets a news_source filtered by key
#     :param key: the key of the news source (e.g: g1, uol, etc.)
#     :return: the news source object
#     """
#     ns = NewsSource.query.filter_by(
#         key=key).first()  # Fetch news source by key
#     return ns


# Path: app/news_source_international.py
from app.aml_utils import getpage, parsepage, get_ns


def __ny(soup):
    """
    DEPRECATED
   Gets the most read news from NY Times page
   :param soup: the BeautifulSoup object
   :return: a list with the most read news from the NY Times page
   """
    news = []
    divs = soup.find_all('div', class_='story')

    for d in divs[:10]:
        link = d.h3.a['href']
        title = d.h3.a.string

        news.append(dict(title=title, link=link))
    return news


# Strategy Pattern - a dictionary of functions. Key: the name of the News Source. Value: the Function to execute
strategies = dict(fox=__fox, wp=__wp, tg=__tg, lf=__lf, tt=__tt, ep=__ep, ny=__ny, reu=__reu)


def get_most_read(key):
    """
    Gets the most read news from a given page

    :param key: the key of the source page (e.g: g1)
    :return: a list with the most read news from the page and the name of news source
    """

    ns = get_ns(key)