Debian Codegraph @

recipes/pagina_12_print_ed.recipe

raw
1 #!/usr/bin/env python
2
3 from __future__ import print_function
4
5 __license__ = 'GPL v3'
6 __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
7 '''
8 pagina12.com.ar
9 '''
10 import re
11
12 from calibre.ebooks.BeautifulSoup import NavigableString, Tag
13 from calibre.web.feeds.news import BasicNewsRecipe
14
15
16 def new_tag(soup, name, attrs=()):
17 impl = getattr(soup, 'new_tag', None)
18 if impl is not None:
19 return impl(name, attrs=dict(attrs))
20 return Tag(soup, name, attrs=attrs or None)
21
22
23 class Pagina12(BasicNewsRecipe):
24 title = 'Pagina/12 - Edicion Impresa'
25 __author__ = 'Pablo Marfil'
26 description = 'Diario argentino'
27 INDEX = 'http://www.pagina12.com.ar/diario/secciones/index.html'
28 language = 'es'
29 encoding = 'cp1252'
30 remove_tags_before = dict(id='fecha')
31 remove_tags_after = dict(id='fin')
32 remove_tags = [dict(id=['fecha', 'fin', 'pageControls', 'logo', 'logo_suple', 'fecha_suple', 'volver'])]
33 masthead_url = 'http://www.pagina12.com.ar/commons/imgs/logo-home.gif'
34 no_stylesheets = True
35
36 preprocess_regexps = [(re.compile(r'<!DOCTYPE[^>]+>', re.I), lambda m: '')]
37
38 def get_cover_url(self):
39 soup = self.index_to_soup('http://www.pagina12.com.ar/diario/principal/diario/index.html')
40 for image in soup.findAll('img', alt=True):
41 if image['alt'].startswith('Tapa de la fecha'):
42 return image['src']
43 print(image)
44 return None
45
46 def parse_index(self):
47 articles = []
48 numero = 1
49 raw = self.index_to_soup('http://www.pagina12.com.ar/diario/secciones/index.html', raw=True)
50 raw = re.sub(r'(?i)<!DOCTYPE[^>]+>', '', raw)
51 soup = self.index_to_soup(raw)
52
53 feeds = []
54
55 seen_titles = set()
56 for section in soup.findAll('div', 'seccionx'):
57 numero += 1
58 print(numero)
59 section_title = self.tag_to_string(section.find('div', 'desplegable_titulo on_principal right'))
60 self.log('Found section:', section_title)
61 articles = []
62 for post in section.findAll('h2'):
63 h = post.find('a', href=True)
64 title = self.tag_to_string(h)
65 if title in seen_titles:
66 continue
67 seen_titles.add(title)
68 a = post.find('a', href=True)
69 url = a['href']
70 if url.startswith('/'):
71 url = 'http://pagina12.com.ar/imprimir' + url
72 p = post.find('div', attrs={'h2'})
73 desc = None
74 self.log('\tFound article:', title, 'at', url)
75 if p is not None:
76 desc = self.tag_to_string(p)
77 self.log('\t\t', desc)
78 articles.append({'title': title, 'url': url, 'description': desc, 'date': ''})
79 if articles:
80 feeds.append((section_title, articles))
81 return feeds
82
83 def postprocess_html(self, soup, first):
84 for table in soup.findAll('table', align='right'):
85 img = table.find('img')
86 if img is not None:
87 img.extract()
88 caption = self.tag_to_string(table).strip()
89 div = new_tag(soup, 'div')
90 div['style'] = 'text-align:center'
91 div.insert(0, img)
92 div.insert(1, new_tag(soup, 'br'))
93 if caption:
94 div.insert(2, NavigableString(caption))
95 table.replaceWith(div)
96
97 return soup