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

import re

from calibre.web.feeds.news import BasicNewsRecipe


class CSMonitor(BasicNewsRecipe):
    title = 'The Christian Science Monitor - daily'
    __author__ = 'Darko Miletic'
    description = 'The Christian Science Monitor is an international news organization that delivers thoughtful, global coverage via its website, weekly magazine, daily news briefing, and email newsletters.'  # noqa: E501
    publisher = 'The Christian Science Monitor'
    category = 'news, politics, USA'
    oldest_article = 2
    max_articles_per_feed = 200
    no_stylesheets = True
    encoding = 'utf8'
    use_embedded_content = False
    language = 'en'
    remove_empty_feeds = True
    publication_type = 'newspaper'
    masthead_url = 'http://www.csmonitor.com/extension/csm_base/design/csm_design/images/csmlogo_179x46.gif'
    extra_css = '''
                               body{font-family: Arial,Tahoma,Verdana,Helvetica,sans-serif }
                               img{margin-bottom: 0.4em; display:block}
                               .head {font-family: Georgia,"Times New Roman",Times,serif}
                               .sByline,.caption{font-size: x-small}
                               .hide{display: none}
                               .sLoc{font-weight: bold}
                               ul{list-style-type: none}
                            '''

    conversion_options = {'comment': description, 'tags': category, 'publisher': publisher, 'language': language}

    remove_tags = [
        dict(name=['meta', 'link', 'iframe', 'object', 'embed']),
        dict(attrs={'class': re.compile(r'(^|| )podStoryRel($|| )', re.DOTALL)}),
        dict(attrs={'class': ['bottom-rel', 'hide']}),
        dict(attrs={'id': ['pgallerycarousel_enlarge', 'pgallerycarousel_related']}),
    ]
    keep_only_tags = [
        dict(name='h1', attrs={'class': 'head'}),
        dict(name='h2', attrs={'class': 'subhead'}),
        dict(attrs={'class': ['sByline', 'thePhoto', 'ui-body-header']}),
        dict(attrs={'class': re.compile(r'(^|| )sBody($|| )', re.DOTALL)}),
    ]
    remove_attributes = ['xmlns:fb']

    feeds = [
        ('USA', 'http://rss.csmonitor.com/feeds/usa'),
        ('World', 'http://rss.csmonitor.com/feeds/world'),
        ('Politics', 'http://rss.csmonitor.com/feeds/politics'),
        ('Business', 'http://rss.csmonitor.com/feeds/wam'),
        ('Commentary', 'http://rss.csmonitor.com/feeds/commentary'),
        ('Books', 'http://rss.csmonitor.com/feeds/books'),
        ('Arts', 'http://rss.csmonitor.com/feeds/arts'),
        ('Environment', 'http://rss.csmonitor.com/feeds/environment'),
        ('Innovation', 'http://rss.csmonitor.com/feeds/scitech'),
        ('Living', 'http://rss.csmonitor.com/feeds/living'),
        ('Science', 'http://rss.csmonitor.com/feeds/science'),
        ('The Culture', 'http://rss.csmonitor.com/feeds/theculture'),
        ('The Home Forum', 'http://rss.csmonitor.com/feeds/homeforum'),
        ('Articles', 'http://rss.csmonitor.com/feeds/csarticles'),
    ]

    def append_page(self, soup):
        pager = soup.find('div', attrs={'class': 'navigation'})
        if pager:
            nexttag = pager.find(attrs={'id': 'next-button'})
            if nexttag:
                nurl = 'http://www.csmonitor.com' + nexttag['href']
                soup2 = self.index_to_soup(nurl)
                texttag = soup2.find(attrs={'class': re.compile(r'(^|| )sBody($|| )', re.DOTALL)})
                if texttag:
                    appendtag = soup.find(attrs={'class': re.compile(r'(^|| )sBody($|| )', re.DOTALL)})
                    for citem in texttag.findAll(attrs={'class': [re.compile(r'(^|| )podStoryRel($|| )', re.DOTALL), 'bottom-rel', 'hide']}):
                        citem.extract()
                    self.append_page(soup2)
                    texttag.extract()
                    pager.extract()
                    appendtag.append(texttag)

    def preprocess_html(self, soup):
        self.append_page(soup)
        pager = soup.find('div', attrs={'class': 'navigation'})
        if pager:
            pager.extract()
        for item in soup.findAll('a'):
            limg = item.find('img')
            if item.string is not None:
                str = item.string
                item.replaceWith(str)
            elif limg:
                item.name = 'div'
                item.attrs = []
            else:
                str = self.tag_to_string(item)
                item.replaceWith(str)
        for item in soup.findAll('img', src=True):
            if 'scorecardresearch' in item['src']:
                item.extract()
        return soup
