#!/usr/bin/env python
__license__ = 'GPL v3'
__author__ = 'Gabriele Marini, based on Darko Miletic'
__copyright__ = '2009, Darko Miletic <darko.miletic at gmail.com>'
description = 'Italian daily newspaper - v1.00 05-05-2010'

'''
http://www.leggo.it
'''
import time

from calibre.web.feeds.news import BasicNewsRecipe


class LeggoIT(BasicNewsRecipe):
    __author__ = 'Gabriele Marini'
    description = 'Italian Free daily newspaper'

    # cover_url      = 'http://www.leggo.it/img/logo-leggo2.gif'
    title = 'Leggo.it'
    publisher = 'Ced Caltagirone Editore S.p.A.'
    category = 'News, politics, culture, economy, general interest'

    language = 'it'
    timefmt = '[%a, %d %b, %Y]'

    oldest_article = 5
    max_articles_per_feed = 100
    use_embedded_content = False
    recursion = 100

    no_stylesheets = True
    remove_javascript = True
    conversion_options = {'linearize_tables': True}

    keep_only_tags = [dict(name='h1', attrs={'class': 'nero22'}), dict(name='div', attrs={'id': 'testodim'})]
    feeds = [
        ('Home Page', 'http://www.leggo.it/rss/home.xml'),
        ('Italia', 'http://www.leggo.it/rss/italia.xml'),
        ('Esteri', 'http://www.leggo.it/rss/esteri.xml'),
        ('Economia', 'http://www.leggo.it/rss/economia.xml'),
        ('Sport', 'http://www.leggo.it/rss/sport.xml'),
        ('Gossip', 'http://www.leggo.it/rss/gossip.xml'),
        ('Spettacoli', 'http://www.leggo.it/rss/spettacoli.xml'),
        ('Salute', 'http://www.leggo.it/rss/salute.xml'),
        ('Scienza', 'http://www.leggo.it/rss/scienza.xml'),
    ]

    def get_cover_url(self):
        cover = None
        st = time.localtime()
        year = str(st.tm_year)
        month = '%.2d' % st.tm_mon
        day = '%.2d' % st.tm_mday
        cover = 'http://www.leggo.it/' + year + month + day + '/jpeg/LEGGO_ROMA_1.jpg'
        br = BasicNewsRecipe.get_browser(self)
        try:
            br.open(cover)
        except Exception:
            cover = 'http://www.leggo.it/' + year + month + day + '/jpeg/LEGGO_ROMA_3.jpg'
            br = BasicNewsRecipe.get_browser(self)
            try:
                br.open(cover)
            except Exception:
                self.log('\nCover unavailable')
                cover = 'http://www.leggo.it/img/logo-leggo2.gif'
        return cover
