| 1 |
__license__ = 'GPL v3' |
| 2 |
import re |
| 3 |
|
| 4 |
from calibre.ebooks.BeautifulSoup import BeautifulSoup, Comment |
| 5 |
from calibre.web.feeds.news import BasicNewsRecipe |
| 6 |
|
| 7 |
|
| 8 |
class EsensjaRSS(BasicNewsRecipe): |
| 9 |
title = 'Esensja (RSS)' |
| 10 |
__author__ = 'fenuks' |
| 11 |
description = 'Magazyn kultury popularnej' |
| 12 |
category = 'reading, fantasy, reviews, boardgames, culture' |
| 13 |
language = 'pl' |
| 14 |
encoding = 'utf-8' |
| 15 |
INDEX = 'http://www.esensja.pl' |
| 16 |
cover_url = '' |
| 17 |
masthead_url = 'http://esensja.pl/img/wrss.gif' |
| 18 |
use_embedded_content = False |
| 19 |
oldest_article = 7 |
| 20 |
max_articles_per_feed = 100 |
| 21 |
no_stylesheets = True |
| 22 |
remove_empty_feeds = True |
| 23 |
remove_javascript = True |
| 24 |
ignore_duplicate_articles = {'title', 'url'} |
| 25 |
preprocess_regexps = [(re.compile(r'alt="[^"]*"'), lambda match: ''), (re.compile(r'(title|alt)="[^"]*?"', re.DOTALL), lambda match: '')] |
| 26 |
remove_attributes = ['style', 'bgcolor', 'alt', 'color'] |
| 27 |
keep_only_tags = [ |
| 28 |
dict(attrs={'class': 'sekcja'}), |
| 29 |
] |
| 30 |
remove_tags_after = dict(id='tekst') |
| 31 |
|
| 32 |
remove_tags = [ |
| 33 |
dict(name='img', attrs={'src': ['../../../2000/01/img/tab_top.gif', '../../../2000/01/img/tab_bot.gif']}), |
| 34 |
dict(name='div', attrs={'class': 't-title2 nextpage'}), |
| 35 |
# dict(attrs={'rel':'lightbox[galeria]'}) |
| 36 |
dict(attrs={'class': ['tekst_koniec', 'ref', 'wykop']}), |
| 37 |
dict(attrs={'itemprop': ['copyrightHolder', 'publisher']}), |
| 38 |
dict(id='komentarze'), |
| 39 |
] |
| 40 |
|
| 41 |
feeds = [ |
| 42 |
('Książka', 'http://esensja.pl/rss/ksiazka.rss'), |
| 43 |
('Film', 'http://esensja.pl/rss/film.rss'), |
| 44 |
('Komiks', 'http://esensja.pl/rss/komiks.rss'), |
| 45 |
('Gry', 'http://esensja.pl/rss/gry.rss'), |
| 46 |
('Muzyka', 'http://esensja.pl/rss/muzyka.rss'), |
| 47 |
('Twórczość', 'http://esensja.pl/rss/tworczosc.rss'), |
| 48 |
('Varia', 'http://esensja.pl/rss/varia.rss'), |
| 49 |
('Zgryźliwi Tetrycy', 'http://esensja.pl/rss/tetrycy.rss'), |
| 50 |
('Nowe książki', 'http://esensja.pl/rss/xnowosci.rss'), |
| 51 |
('Ostatnio dodane książki', 'http://esensja.pl/rss/xdodane.rss'), |
| 52 |
] |
| 53 |
|
| 54 |
def get_cover_url(self): |
| 55 |
soup = self.index_to_soup(self.INDEX) |
| 56 |
cover = soup.find(id='panel_1') |
| 57 |
self.cover_url = self.INDEX + cover.find('a')['href'].replace('index.html', '') + 'img/ilustr/cover_b.jpg' |
| 58 |
return getattr(self, 'cover_url', self.cover_url) |
| 59 |
|
| 60 |
def append_page(self, soup, appendtag): |
| 61 |
r = appendtag.find(attrs={'class': 'wiecej_xxx'}) |
| 62 |
if r: |
| 63 |
nr = r.findAll(attrs={'class': 'tn-link'})[-1] |
| 64 |
try: |
| 65 |
nr = int(nr.a.string) |
| 66 |
except Exception: |
| 67 |
return |
| 68 |
baseurl = soup.find(attrs={'property': 'og:url'})['content'] + '&strona={0}' |
| 69 |
for number in range(2, nr + 1): |
| 70 |
soup2 = self.index_to_soup(baseurl.format(number)) |
| 71 |
pagetext = soup2.find(attrs={'class': 'tresc'}) |
| 72 |
pos = len(appendtag.contents) |
| 73 |
appendtag.insert(pos, pagetext) |
| 74 |
for r in appendtag.findAll(attrs={'class': ['wiecej_xxx', 'tekst_koniec']}): |
| 75 |
r.extract() |
| 76 |
for r in appendtag.findAll('script'): |
| 77 |
r.extract() |
| 78 |
|
| 79 |
comments = appendtag.findAll(text=lambda text: isinstance(text, Comment)) |
| 80 |
for comment in comments: |
| 81 |
comment.extract() |
| 82 |
|
| 83 |
def preprocess_html(self, soup): |
| 84 |
self.append_page(soup, soup.body) |
| 85 |
for tag in soup.findAll(attrs={'class': 'img_box_right'}): |
| 86 |
temp = tag.find('img') |
| 87 |
src = '' |
| 88 |
if temp: |
| 89 |
src = temp.get('src', '') |
| 90 |
for r in tag.findAll('a', recursive=False): |
| 91 |
r.extract() |
| 92 |
info = tag.find(attrs={'class': 'img_info'}) |
| 93 |
text = str(tag) |
| 94 |
if not src: |
| 95 |
src = re.search(r'src="[^"]*?"', text) |
| 96 |
if src: |
| 97 |
src = src.group(0) |
| 98 |
src = src[5:].replace('//', '/') |
| 99 |
if src: |
| 100 |
tag.contents = [] |
| 101 |
tag.insert(0, BeautifulSoup('<img src="{0}{1}" />'.format(self.INDEX, src))) |
| 102 |
if info: |
| 103 |
tag.insert(len(tag.contents), info) |
| 104 |
return soup |