__license__ = 'GPL v3'
from calibre.web.feeds.news import BasicNewsRecipe


class FDBPl(BasicNewsRecipe):
    title = u'Fdb.pl'
    __author__ = 'fenuks'
    description = u'Wiadomości ze świata filmu, baza danych filmowych, recenzje, zwiastuny, boxoffice.'
    category = 'film'
    language = 'pl'
    extra_css = '.options-left > li {display: inline;} em {display: block;}'
    cover_url = 'https://i1.fdbimg.pl/hygg2xp1/480x300_magq39.jpg'
    use_embedded_content = False
    oldest_article = 7
    max_articles_per_feed = 100
    no_stylesheets = True
    remove_empty_feeds = True
    remove_javascript = True
    remove_attributes = ['style', 'font']
    ignore_duplicate_articles = {'title', 'url'}

    keep_only_tags = [dict(attrs={'class': ['row justify-content-center', 'figure']})]
    remove_tags = [
        dict(attrs={'class': ['news-footer infinite-scroll-breakepoit', 'list-inline text-muted m-0']})]
    feeds = []

    def parse_index(self):
        feeds = []
        feeds.append((u'Wiadomości', self.get_articles(
            'https://fdb.pl/wiadomosci?page={0}', 2)))
        return feeds

    def get_articles(self, url, pages=1):
        articles = []
        for nr in range(1, pages + 1):
            soup = self.index_to_soup(url.format(nr))
            for tag in soup.findAll(attrs={'class': 'col-xs-6 col-sm-4 col-md-4 col-lg-3'}):
                node = tag.find('h5')
                title = node.a.string
                url = node.a['href']
                date = ''
                articles.append({'title': title,
                                 'url': url,
                                 'date': date,
                                 'description': ''
                                 })
        return articles
