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

import re
from calibre.web.feeds.recipes import BasicNewsRecipe


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
    }

    preprocess_regexps = [
        (re.compile(
            r'<meta name="description" content="[^"]+"\s*/?>',
            re.IGNORECASE | re.DOTALL),
         lambda m:''),
        (re.compile(r'<!--.+?-->', re.IGNORECASE | re.DOTALL),
         lambda m: ''),
    ]
    remove_tags = [
        dict(name=['object', 'link', 'map', 'style']),
        dict(attrs={'class': 'st2014-realted-links'}),
    ]

    keep_only_tags = [dict(name='div', attrs={'class': 'story'})]
    remove_tags_after = dict(name='div', attrs={'class': 'hr_thin'})

    feeds = [
              (u'Top of the News' , u'http://www.straitstimes.com/print/top-of-the-news/rss.xml')
              ,(u'World'           , u'http://www.straitstimes.com/print/world/rss.xml')
              ,(u'Home'            , u'http://www.straitstimes.com/print/home/rss.xml')
              ,(u'Business'        , u'http://www.straitstimes.com/print/business/rss.xml')
              ,(u'Life'            , u'http://www.straitstimes.com/print/life/rss.xml')
              ,(u'Science'         , u'http://www.straitstimes.com/print/science/rss.xml')
              ,(u'Digital'         , u'http://www.straitstimes.com/print/digital/rss.xml')
              ,(u'Insight'         , u'http://www.straitstimes.com/print/insight/rss.xml')
              ,(u'Opinion'         , u'http://www.straitstimes.com/print/opinion/rss.xml')
              ,(u'Forum'           , u'http://www.straitstimes.com/print/forum/rss.xml')
              ,(u'Big Picture'     , u'http://www.straitstimes.com/print/big-picture/rss.xml')
              ,(u'Community'       , u'http://www.straitstimes.com/print/community/rss.xml')
              ,(u'Education'       , u'http://www.straitstimes.com/print/education/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
