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


class Polter(BasicNewsRecipe):
    title = u'Polter.pl'
    __author__ = 'fenuks'
    description = u'Największy polski serwis poświęcony ogólno pojętej fantastyce - grom fabularnym (RPG), książkom, filmowi, komiksowi, grom planszowym, karcianym i bitewnym.'  # noqa
    category = 'fantasy, books, rpg, games'
    language = 'pl'
    extra_css = '.image, .floatright {float: right; margin-left: 10px;} .floatleft {float: left; margin-right: 10px;} .calibre_navbar {clear: both;} .p_title {font-weight: bold;} .p_image {margin-left: auto; margin-right: auto; display: block;} .italic {font-style: italic;}'  # noqa
    cover_url = 'http://static.polter.pl/sub/promo/bpromo2524.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 = ['font', 'fieldset', 'onclick']
    ignore_duplicate_articles = {'title', 'url'}

    keep_only_tags = [dict(attrs={'class': 'boxcontent'})]
    remove_tags = [dict(id='komentarze'),
        dict(name='div',attrs={'class':'ostatnieArtykuly'})]
    remove_tags_after = dict(id='komentarze')

    feeds = [
    (u'Wieści', 'http://polter.pl/wiesci,rss.html'),
    (u'RPG', 'http://rpg.polter.pl/wiesci,rss.html'),
    (u'Książki', 'http://ksiazki.polter.pl/wiesci,rss.html'),
    (u'Film', 'http://film.polter.pl/wiesci,rss.html'),
    (u'Komiks', 'http://komiks.polter.pl/wiesci,rss.html'),
    (u'Gry bitewne', 'http://bitewniaki.polter.pl/wiesci,rss.html'),

    (u'Gry karciane', 'http://karcianki.polter.pl/wiesci,rss.html'),
    (u'Gry planszowe', 'http://planszowki.polter.pl/wiesci,rss.html'),
    (u'Gry PC', 'http://gry.polter.pl/wiesci,rss.html'),
    (u'Gry konsolowe', 'http://konsole.polter.pl/wiesci,rss.html'),
    (u'Konwenty', 'http://konwenty.polter.pl/wiesci,rss.html')]

    def preprocess_html(self, soup):
        for s in soup.findAll(attrs={'style': re.compile('float: ?left')}):
            s['class'] = 'floatleft'
        for s in soup.findAll(attrs={'style': re.compile('float: ?right')}):
            s['class'] = 'floatright'
        for s in soup.findAll(style=True):
            if 'bold;' in s['style']:
                if s.get('class', ''):
                    s['class'] = ''.join(s['class']) + ' p_title'
                else:
                    s['class'] = 'p_title'
            if 'italic;' in s['style']:
                if s.get('class', ''):
                    s['class'] = ''.join(s['class']) + ' italic'
                else:
                    s['class'] = 'italic'
            del s['style']

        tag = soup.find(id='twoja_ocena')
        if tag:
            tag.parent.extract()
        for tag in soup.findAll(id='lista_chce_ile'):
            tag.parent.parent.extract()
        for r in soup.findAll(name='a', href=re.compile(r'^http://www.ceneo.pl/')):
            r.extract()
        return soup

    def preprocess_raw_html(self, raw_html, url):
        return raw_html.replace('<br /><br /><h3>Czytaj również</h3>', '')
