from calibre.web.feeds.news import BasicNewsRecipe


class GazetaKrakowska(BasicNewsRecipe):
    title = 'Gazeta Krakowska'
    __author__ = 'fenuks'
    description = 'Gazeta Regionalna Gazeta Krakowska. Najnowsze Wiadomości Kraków. Informacje Kraków. Czytaj!'
    category = 'newspaper'
    language = 'pl'
    encoding = 'iso-8859-2'
    masthead_url = 'http://s.polskatimes.pl/g/logo_naglowek/gazetakrakowska.png?24'
    oldest_article = 7
    max_articles_per_feed = 100
    remove_empty_feeds = True
    no_stylesheets = True
    use_embedded_content = False
    ignore_duplicate_articles = {'title', 'url'}
    remove_tags_after = dict(attrs={'src': 'http://nm.dz.com.pl/dz.png'})
    remove_tags = [dict(id='mat-podobne'), dict(name='a', attrs={'class': 'czytajDalej'}), dict(attrs={'src': 'http://nm.dz.com.pl/dz.png'})]

    feeds = [
        ('Fakty24', 'http://gazetakrakowska.feedsportal.com/c/32980/f/533770/index.rss?201302'),
        ('Krak\xf3w', 'http://www.gazetakrakowska.pl/rss/gazetakrakowska_krakow.xml?201302'),
        ('Tarn\xf3w', 'http://www.gazetakrakowska.pl/rss/gazetakrakowska_tarnow.xml?201302'),
        ('Nowy S\u0105cz', 'http://www.gazetakrakowska.pl/rss/gazetakrakowska_nsacz.xml?201302'),
        ('Ma\u0142. Zach.', 'http://www.gazetakrakowska.pl/rss/gazetakrakowska_malzach.xml?201302'),
        ('Podhale', 'http://www.gazetakrakowska.pl/rss/gazetakrakowska_podhale.xml?201302'),
        ('Sport', 'http://gazetakrakowska.feedsportal.com/c/32980/f/533771/index.rss?201302'),
        ('Kultura', 'http://gazetakrakowska.feedsportal.com/c/32980/f/533772/index.rss?201302'),
        ('Opinie', 'http://www.gazetakrakowska.pl/rss/gazetakrakowska_opinie.xml?201302'),
        ('Magazyn', 'http://www.gazetakrakowska.pl/rss/gazetakrakowska_magazyn.xml?201302'),
    ]

    def print_version(self, url):
        return url.replace('artykul', 'drukuj')

    def skip_ad_pages(self, soup):
        if 'Advertisement' in soup.title:
            nexturl = soup.find('a')['href']
            return self.index_to_soup(nexturl, raw=True)

    def get_cover_url(self):
        soup = self.index_to_soup('http://www.prasa24.pl/gazeta/gazeta-krakowska/')
        self.cover_url = soup.find(id='pojemnik').img['src']
        return getattr(self, 'cover_url', self.cover_url)
