from calibre.ebooks.BeautifulSoup import BeautifulSoup
from calibre.web.feeds.news import BasicNewsRecipe


class Elektroda(BasicNewsRecipe):
    title = u'Elektroda'
    oldest_article = 8
    __author__ = 'fenuks'
    description = 'Międzynarodowy portal elektroniczny udostępniający bogate zasoby z dziedziny elektroniki oraz forum dyskusyjne.'
    cover_url = 'http://demotywatory.elektroda.pl/Thunderpic/logo.gif'
    category = 'electronics'
    language = 'pl'
    max_articles_per_feed = 100
    no_stylesheets = True
    feeds = [(u'Elektroda', u'http://www.elektroda.pl/rtvforum/rss.php')]

    keep_only_tags = [dict(name='div', attrs={'class': 'title-wrap pull-left'}),
                      dict(name='ul', attrs={'class': 'topic-lists clearfix'})
    ]

    def preprocess_html(self, soup):
        tag = soup.find('span', attrs={'class': 'postbody'})
        if tag:
            pos = len(tag.contents)
            tag.insert(pos, BeautifulSoup('<br />'))
        return soup

    def parse_feeds(self):
        feeds = BasicNewsRecipe.parse_feeds(self)
        for feed in feeds:
            for article in feed.articles[:]:
                article.title = article.title[article.title.find("::") + 3:]
        return feeds
