__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
'''
Profile to download CNN
'''

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


class CNN(BasicNewsRecipe):

    title = 'CNN'
    description = 'Global news'
    timefmt = ' [%d %b %Y]'
    __author__ = 'Kovid Goyal'
    language = 'en'

    no_stylesheets = True
    use_embedded_content = False
    oldest_article = 2
    ignore_duplicate_articles = {'url'}
    max_articles_per_feed = 25
    remove_attributes = ['style', 'height', 'width']
    keep_only_tags = [
        classes('headline__wrapper headline__sub-container article__main'),
    ]
    remove_tags = [classes('video-inline_carousel')]

    feeds = [
        ('Top News', 'http://rss.cnn.com/rss/cnn_topstories.rss'),
        ('World', 'http://rss.cnn.com/rss/cnn_world.rss'),
        ('U.S.', 'http://rss.cnn.com/rss/cnn_us.rss'),
        # ('Sports', 'http://rss.cnn.com/rss/si_topstories.rss'),
        ('Business', 'http://rss.cnn.com/rss/money_latest.rss'),
        ('Politics', 'http://rss.cnn.com/rss/cnn_allpolitics.rss'),
        ('Law', 'http://rss.cnn.com/rss/cnn_law.rss'),
        ('Technology', 'http://rss.cnn.com/rss/cnn_tech.rss'),
        ('Science & Space', 'http://rss.cnn.com/rss/cnn_space.rss'),
        ('Health', 'http://rss.cnn.com/rss/cnn_health.rss'),
        ('Entertainment', 'http://rss.cnn.com/rss/cnn_showbiz.rss'),
        ('Education', 'http://rss.cnn.com/rss/cnn_education.rss'),
        ('Offbeat', 'http://rss.cnn.com/rss/cnn_offbeat.rss'),
        ('Most Popular', 'http://rss.cnn.com/rss/cnn_mostpopular.rss')
    ]

    def get_article_url(self, article):
        ans = BasicNewsRecipe.get_article_url(self, article)
        ans = ans.partition('?')[0]
        if '.com/videos/' in ans:
            ans = None
        return ans

    def get_masthead_url(self):
        masthead = 'http://i.cdn.turner.com/cnn/.element/img/3.0/global/header/intl/hdr-globe-central.gif'
        br = BasicNewsRecipe.get_browser(self)
        try:
            br.open(masthead)
        except:
            self.log("\nCover unavailable")
            masthead = None
        return masthead
    
    def preprocess_html(self, soup):
        for img in soup.findAll('img', attrs={'src':lambda x: x and x.endswith('.svg')}):
            img.extract()
        return soup


calibre_most_common_ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36'