#!/usr/bin/env python

__license__ = 'GPL v3'
__copyright__ = '2008, Darko Miletic <darko.miletic at gmail.com>'
'''
sfgate.com
'''

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


class SanFranciscoChronicle(BasicNewsRecipe):
    title = 'San Francisco Chronicle'
    __author__ = 'Kovid Goyal'
    description = 'San Francisco news'
    language = 'en_US'

    oldest_article = 7
    max_articles_per_feed = 20
    no_stylesheets = True
    use_embedded_content = False
    delay = 1

    extra_css = '''
                        h1{font-family :Arial,Helvetica,sans-serif; font-size:large;}
                        h2{font-family :Arial,Helvetica,sans-serif; font-size:medium; color:#666666;}
                        h3{font-family :Arial,Helvetica,sans-serif; font-size:medium; color:#000000;}
                        h4{font-family :Arial,Helvetica,sans-serif; font-size: x-small;}
                        p{font-family :Arial,Helvetica,sans-serif; font-size:x-small;}
                        .byline{font-family :Arial,Helvetica,sans-serif; font-size: xx-small;}
                        .date{font-family :Arial,Helvetica,sans-serif; font-size: xx-small;}
                        .dtlcomment{font-style:italic;}
                        .georgia h3{font-family :Arial,Helvetica,sans-serif; font-size:x-small; color:#000000;}
                     '''
    keep_only_tags = [
        dict(name='h1'),
        dict(**classes('package')),
    ]
    remove_tags = [
        dict(id=['badge-logo', 'everlit-auto-audio-widget']),
        dict(name='svg'),
        dict(attrs={'data-eid': lambda x: x and ('news_alert_bar' in x or 'card' in x or 'mostPopular' in x or 'dynamic_thumbnail_list' in x)}),
        dict(href=lambda x: x and 'california-mini-crossword' in x),
        dict(attrs={'data-block-type': 'embed'}),
        dict(name='img', width='1'),
    ]

    feeds = [
        ('Bay Area News', 'https://www.sfgate.com/bayarea/feed/Bay-Area-News-429.php'),
        ('City Insider', 'https://www.sfgate.com/default/feed/City-Insider-Blog-573.php'),
        ('Crime Scene', 'https://www.sfgate.com/rss/feed/Crime-Scene-Blog-599.php'),
        ('Education News', 'https://www.sfgate.com/education/feed/Education-News-from-SFGate-430.php'),
        ('National News', 'https://www.sfgate.com/rss/feed/National-News-RSS-Feed-435.php'),
        ('Weird News', 'https://www.sfgate.com/weird/feed/Weird-News-RSS-Feed-433.php'),
        ('World News', 'https://www.sfgate.com/rss/feed/World-News-From-SFGate-432.php'),
    ]

    def preprocess_html(self, soup):
        for picture in soup.find_all('picture'):
            img = picture.find('img')
            if img:
                for i, source in enumerate(picture.find_all('source')):
                    if i == 0:
                        img['src'] = source['srcset'].split()[0]
                    source.extract()
        return soup
