| 1 |
from calibre import strftime |
| 2 |
from calibre.ebooks.BeautifulSoup import BeautifulSoup |
| 3 |
from calibre.web.feeds.recipes import BasicNewsRecipe |
| 4 |
|
| 5 |
MANIFESTO_BASEURL = 'http://www.ilmanifesto.it/' |
| 6 |
|
| 7 |
|
| 8 |
class IlManifesto(BasicNewsRecipe): |
| 9 |
title = 'Il Manifesto' |
| 10 |
__author__ = 'Giacomo Lacava' |
| 11 |
description = 'quotidiano comunista - ultima edizione html disponibile' |
| 12 |
publication_type = 'newspaper' |
| 13 |
publisher = 'il manifesto coop. editrice a r.l.' |
| 14 |
language = 'it' |
| 15 |
|
| 16 |
oldest_article = 2 |
| 17 |
max_articles_per_feed = 100 |
| 18 |
delay = 1 |
| 19 |
no_stylesheets = True |
| 20 |
simultaneous_downloads = 5 |
| 21 |
timeout = 30 |
| 22 |
auto_cleanup = True |
| 23 |
remove_tags = [dict(name='div', attrs={'class': 'column_1 float_left'})] |
| 24 |
remove_tags_before = dict(name='div', attrs={'class': 'column_2 float_right'}) |
| 25 |
remove_tags_after = dict(id='myPrintArea') |
| 26 |
|
| 27 |
manifesto_index = None |
| 28 |
manifesto_datestr = None |
| 29 |
|
| 30 |
def _set_manifesto_index(self): |
| 31 |
if self.manifesto_index is None: |
| 32 |
startUrl = MANIFESTO_BASEURL + 'area-abbonati/in-edicola/' |
| 33 |
startSoup = self.index_to_soup(startUrl) |
| 34 |
lastEdition = startSoup.findAll('div', id='accordion_inedicola')[1].find('a')['href'] |
| 35 |
del startSoup |
| 36 |
self.manifesto_index = MANIFESTO_BASEURL + lastEdition |
| 37 |
urlsplit = lastEdition.split('/') |
| 38 |
self.manifesto_datestr = urlsplit[-1] |
| 39 |
if urlsplit[-1] == '': |
| 40 |
self.manifesto_datestr = urlsplit[-2] |
| 41 |
|
| 42 |
def get_cover_url(self): |
| 43 |
self._set_manifesto_index() |
| 44 |
url = MANIFESTO_BASEURL + 'fileadmin/archivi/in_edicola/%sprimapagina.gif' % self.manifesto_datestr |
| 45 |
return url |
| 46 |
|
| 47 |
def parse_index(self): |
| 48 |
self._set_manifesto_index() |
| 49 |
soup = self.index_to_soup(self.manifesto_index) |
| 50 |
feedLinks = soup.find('div', id='accordion_inedicola').findAll('a') |
| 51 |
result = [] |
| 52 |
for feed in feedLinks: |
| 53 |
articles = [] |
| 54 |
feedName = feed.find('h2').string |
| 55 |
feedUrl = MANIFESTO_BASEURL + feed['href'] |
| 56 |
feedSoup = self.index_to_soup(feedUrl) |
| 57 |
indexRoot = feedSoup.find('div', attrs={'class': 'column1'}) |
| 58 |
for div in indexRoot.findAll('div', attrs={'class': 'strumenti1_inedicola'}): |
| 59 |
artLink = div.find('a') |
| 60 |
if artLink is None: |
| 61 |
continue # empty div |
| 62 |
title = artLink.string |
| 63 |
url = MANIFESTO_BASEURL + artLink['href'] |
| 64 |
|
| 65 |
description = '' |
| 66 |
descNode = div.find('div', attrs={'class': 'text_12'}) |
| 67 |
if descNode is not None: |
| 68 |
description = descNode.string |
| 69 |
|
| 70 |
author = '' |
| 71 |
authNode = div.find('div', attrs={'class': 'firma'}) |
| 72 |
if authNode is not None: |
| 73 |
author = authNode.string |
| 74 |
|
| 75 |
articleText = '' |
| 76 |
article = {'title': title, 'url': url, 'date': strftime('%d %B %Y'), 'description': description, 'content': articleText, 'author': author} |
| 77 |
articles.append(article) |
| 78 |
result.append((feedName, articles)) |
| 79 |
return result |
| 80 |
|
| 81 |
def extract_readable_article(self, html, url): |
| 82 |
|
| 83 |
bs = BeautifulSoup(html) |
| 84 |
col1 = bs.find('div', attrs={'class': 'column1'}) |
| 85 |
|
| 86 |
content = col1.find('div', attrs={'class': 'bodytext'}) |
| 87 |
title = bs.find(id='titolo_articolo').string |
| 88 |
author = col1.find('span', attrs={'class': 'firma'}) |
| 89 |
subtitle = '' |
| 90 |
subNode = col1.findPrevious('div', attrs={'class': 'occhiello_rosso'}) |
| 91 |
if subNode is not None: |
| 92 |
subtitle = subNode |
| 93 |
summary = '' |
| 94 |
sommNode = bs.find('div', attrs={'class': 'sommario'}) |
| 95 |
if sommNode is not None: |
| 96 |
summary = sommNode |
| 97 |
|
| 98 |
template = "<html><head><title>%(title)s</title></head><body><h1>%(title)s</h1><h2>%(subtitle)s</h2><h3>%(author)s</h3><div style='font-size: x-large;'>%(summary)s</div><div>%(content)s</div></body></html>" # noqa: E501 |
| 99 |
del bs |
| 100 |
return template % dict(title=title, subtitle=subtitle, author=author, summary=summary, content=content) |