__license__ = 'GPL v3'
__copyright__ = '2009-2010, Darko Miletic <darko.miletic at gmail.com>'
'''
miamiherald.com
'''

from calibre.web.feeds.news import BasicNewsRecipe


def classes(classes):
    q = frozenset(classes.split(' '))
    return dict(attrs={'class': lambda x: x and frozenset(x.split()).intersection(q)})


class TheMiamiHerald(BasicNewsRecipe):
    title = 'The Miami Herald'
    __author__ = 'Kovid Goyal'
    description = "Miami-Dade and Broward's source for the latest breaking local news on sports, weather, business, jobs, real estate, shopping, health, travel, entertainment, & more."  # noqa: E501
    oldest_article = 1
    max_articles_per_feed = 100
    publisher = 'The Miami Herald'
    language = 'en_US'
    no_stylesheets = True
    use_embedded_content = False
    encoding = 'utf-8'
    remove_javascript = True
    conversion_options = {'comment': description, 'publisher': publisher, 'language': language}

    keep_only_tags = [classes('story-body')]
    remove_tags = [classes('social-network-macro social-media')]

    feeds = [
        ('News', 'https://www.miamiherald.com/news/?widgetName=rssfeed&widgetContentId=712015&getXmlFeed=true'),
        ('Miami-Dade', 'https://www.miamiherald.com/news/local/community/miami-dade/?widgetName=rssfeed&widgetContentId=712015&getXmlFeed=true'),
        ('Broward', 'https://www.miamiherald.com/news/local/community/broward/?widgetName=rssfeed&widgetContentId=712015&getXmlFeed=true'),
        ('Florida Keys', 'https://www.miamiherald.com/news/local/community/florida-keys/?widgetName=rssfeed&widgetContentId=712015&getXmlFeed=true'),
        ('Florida', 'https://www.miamiherald.com/news/state/florida/?widgetName=rssfeed&widgetContentId=712015&getXmlFeed=true'),
        ('National', 'https://www.miamiherald.com/news/nation-world/national/?widgetName=rssfeed&widgetContentId=712015&getXmlFeed=true'),
        ('World', 'https://www.miamiherald.com/news/nation-world/world/?widgetName=rssfeed&widgetContentId=712015&getXmlFeed=true'),
        ('Americas', 'https://www.miamiherald.com/news/nation-world/world/americas/?widgetName=rssfeed&widgetContentId=712015&getXmlFeed=true'),
        ('Cuba', 'https://www.miamiherald.com/news/nation-world/world/americas/cuba/?widgetName=rssfeed&widgetContentId=712015&getXmlFeed=true'),
        ('Haiti', 'https://www.miamiherald.com/news/nation-world/world/americas/haiti/?widgetName=rssfeed&widgetContentId=712015&getXmlFeed=true'),
        ('Politics', 'https://www.miamiherald.com/news/politics-government/?widgetName=rssfeed&widgetContentId=712015&getXmlFeed=true'),
        ('Education', 'https://www.miamiherald.com/news/local/education/?widgetName=rssfeed&widgetContentId=712015&getXmlFeed=true'),
        ('Environment', 'https://www.miamiherald.com/news/local/environment/?widgetName=rssfeed&widgetContentId=712015&getXmlFeed=true'),
        ('Sports', 'https://www.miamiherald.com/sports/?widgetName=rssfeed&widgetContentId=712015&getXmlFeed=true'),
    ]

    def get_browser(self, *a, **kw):
        # MyClatchy servers don't like the user-agent header, they hang forever
        # when it is present
        br = BasicNewsRecipe.get_browser(self, *a, **kw)
        br.addheaders = [x for x in br.addheaders if x[0].lower() != 'user-agent']
        return br

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