__license__ = 'GPL v3'
__copyright__ = '2009, Darko Miletic <darko.miletic at gmail.com>'
'''
www.straitstimes.com
'''

from calibre.web.feeds.recipes import BasicNewsRecipe


def classes(classes):
    q = frozenset(classes.split(' '))
    return dict(attrs={'class': lambda x: x and frozenset(x.split()).intersection(q)})


class StraitsTimes(BasicNewsRecipe):
    title = 'The Straits Times'
    __author__ = 'Darko Miletic'
    description = 'Singapore newspaper'
    oldest_article = 2
    max_articles_per_feed = 100
    no_stylesheets = True
    use_embedded_content = False
    encoding = 'utf-8'
    publisher = 'Singapore Press Holdings Ltd.'
    category = 'news, politics, singapore, asia'
    language = 'en_SG'

    conversion_options = {'comments': description, 'tags': category, 'language': language, 'publisher': publisher}
    keep_only_tags = [
        classes('node-header node-subheadline group-byline-info group-updated-timestamp group-image-frame field-name-body article-content-rawhtml')
    ]
    remove_tags = [
        classes('st_telegram_boilerplate dropdown-menu ads'),
        dict(name='source'),
    ]

    feeds = [
        ('World', 'https://www.straitstimes.com/news/world/rss.xml'),
        ('Business', 'https://www.straitstimes.com/news/business/rss.xml'),
        ('Life', 'https://www.straitstimes.com/news/life/rss.xml'),
        ('Tech', 'https://www.straitstimes.com/news/tech/rss.xml'),
        ('Opinion', 'https://www.straitstimes.com/news/opinion/rss.xml'),
        ('Life', 'https://www.straitstimes.com/news/life/rss.xml'),
        ('Singapore', 'https://www.straitstimes.com/news/singapore/rss.xml'),
        ('Asia', 'https://www.straitstimes.com/news/asia/rss.xml'),
        ('Multimedia', 'https://www.straitstimes.com/news/multimedia/rss.xml'),
        ('Sport', 'https://www.straitstimes.com/news/sport/rss.xml'),
    ]

    def preprocess_html(self, soup):
        for img in soup.findAll('img', srcset=True):
            img['src'] = img['srcset'].partition(' ')[0]
            img['srcset'] = ''
        return soup
