#!/usr/bin/env python
from datetime import date

from calibre.web.feeds.news import BasicNewsRecipe, classes


def absurl(url):
    if url.startswith('/'):
        return 'https://www.irishtimes.com' + url


class IrishTimes(BasicNewsRecipe):
    title = 'The Irish Times (free)'
    __author__ = 'unkn0wn'
    description = 'Daily news from The Irish Times'
    language = 'en_IE'

    masthead_url = 'http://www.irishtimes.com/assets/images/generic/website/logo_theirishtimes.png'

    encoding = 'utf-8'
    max_articles_per_feed = 50
    remove_empty_feeds = True
    no_stylesheets = True
    extra_css = '''
        img {display:block; margin:0 auto;}
        em, blockquote { color:#202020; }
        .b-it-subheadline { font-style:italic; }
        .calibre-nuked-tag-figcaption, .b-it-byline-block {font-size:small;}
    '''

    keep_only_tags = [
        classes('b-it-headline b-it-subheadline b-it-byline-block__text b-it-lead-art__wrapper b-it-article-body'),
    ]

    remove_tags_after = [
        classes('b-it-article-body'),
    ]

    remove_tags = [
        dict(name=['button', 'svg']),
        classes('b-top-table-list arcad-feature c-unordered-list b-it-article-body__podcast'),
    ]

    remove_attributes = ['width', 'height', 'style']
    ignore_duplicate_articles = {'title', 'url'}
    resolve_internal_links = True

    def get_cover_url(self):
        soup = self.index_to_soup('https://www.frontpages.com/ireland-newspapers/')
        for img in soup.findAll('img', src=True):
            if 'irish-times' in img['src']:
                return 'https://www.frontpages.com' + img['src']

    feeds = []

    def parse_index(self):
        index = 'https://www.irishtimes.com/'
        sections = [
            'ireland',
            'world',
            'opinion',
            'politics',
            'crime-law',
            'culture',
            'business',
            'life-style',
            'health',
            'sport',
            'property',
            'food',
            'abroad',
            'environment',
            'obituaries',
        ]
        feeds = []
        soup = self.index_to_soup(index)
        for sec in sections:
            section = sec.capitalize()
            self.log(section)
            articles = []
            for a in soup.findAll('a', attrs={'href': lambda x: x and x.startswith('/' + sec + '/')}):
                url = absurl(a['href'].split('?')[0])
                if url in {index + sec + '/', index + sec}:
                    continue
                if date.today().strftime('%Y') not in url:
                    continue
                title = self.tag_to_string(a)
                self.log('\t', title, '\n\t\t', url)
                articles.append({'title': title, 'url': url})
            if articles:
                feeds.append((section, articles))
        return feeds

    def preprocess_html(self, soup):
        h2 = soup.find(**classes('b-it-subheadline'))
        if h2:
            h2.name = 'p'
        for img in soup.findAll('img', attrs={'srcset': True}):
            img['src'] = img['srcset'].split()[0]
        return soup

    def populate_article_metadata(self, article, soup, first):
        desc = soup.find(**classes('b-it-subheadline'))
        if desc:
            article.summary = article.text_summary = self.tag_to_string(desc)
