__author__ = 'ohmyxm'
# coding: utf-8

from crawler import crawl_url
from core import *
# TODO logging module, article template several versions and self-made

NOT_FOUND = u'None Article'


class Extractor():
    def __init__(self, html=' ', url=None, base_href=None, **extras):
        """
        init the class

        :param html: the html doc crawled
        :param url: if url param is set, the html param is ignored, and crawl the url page
        :param base_href: the base href settings in a page
        :param extras: extra settings here to add
        """
        # TODO **extras params
        if url is None:
            self.html = html
        else:
            crawled_page = crawl_url(url=url)
            self.html = crawled_page
        self.base_href = base_href
        self._parsed = build_html_tree(self.html, self.base_href)
        self.encode = get_encode_type(self.html)
        self.content_node = None

    def _content(self):
        """
        taking out the article node or return 'None' if there's no

        :return: article content node
        """
        html = get_html_body(self._parsed)
        noise_ticker(html)
        candidates = p_search_score(html)

        # use the default thresh = 18
        tree_node = vote_for_president(candidates=candidates)
        self.content_node = tree_node
        return self.content_node

    def content(self, tag_filter=False):
        """

        :param tag_filter: whether to filter the tags
        :return: if not found article -> 'None Article'(NOT_FOUND)
        """
        result = self._content()
        if result is None:
            string_article = None
        else:
            if tag_filter is True:
                string_article = unicode(result.text_content())
            else:
                string_article = print_as_string(result)
        return string_article

    def title(self, tag_filter=True):
        if tag_filter is False:
            _title = get_title(self._parsed)
            tag_version = '<title>%s</title>' % _title
            return tag_version
        return unicode(get_title(self._parsed))

    def article(self, html_doc=None):
        if html_doc is None:
            content_result = self.content()
            if content_result is None:
                article = None
            else:
                article = make_article(content_result)
        else:
            html_doc = unicode(html_doc)
            article = make_article(html_doc)
        return article

    def date(self):
        # TODO date problem
        pass

    def links(self):
        # TODO article links extract not including img
        pass

    def images(self):
        # TODO not sure about the basehref problem
        if self.content_node is not None:
            img_list = self.content_node.findall('.//img')
            link_list = [node.get('src') for node in img_list]
            return link_list
        else:
            return None


if __name__ == '__main__':

    pass