
__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 = [
        (u'World'            , u'https://www.straitstimes.com/news/world/rss.xml')
        ,(u'Business'        , u'https://www.straitstimes.com/news/business/rss.xml')
        ,(u'Life'            , u'https://www.straitstimes.com/news/life/rss.xml')
        ,(u'Tech'            , u'https://www.straitstimes.com/news/tech/rss.xml')
        ,(u'Opinion'         , u'https://www.straitstimes.com/news/opinion/rss.xml')
        ,(u'Life'            , u'https://www.straitstimes.com/news/life/rss.xml')
        ,(u'Singapore'       , u'https://www.straitstimes.com/news/singapore/rss.xml')
        ,(u'Asia'     	     , u'https://www.straitstimes.com/news/asia/rss.xml')
        ,(u'Multimedia'      , u'https://www.straitstimes.com/news/multimedia/rss.xml')
        ,(u'Sport'           , u'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
