Debian Codegraph @

recipes/le_monde_diplomatique_fr.recipe

raw
1 #!/usr/bin/env python
2 # vim:fileencoding=utf-8
3 from __future__ import unicode_literals
4
5 __license__ = 'GPL v3'
6 __copyright__ = '2013'
7 '''
8 monde-diplomatique.fr
9 '''
10
11 import re
12
13 from calibre.web.feeds import feeds_from_index
14 from calibre.web.feeds.news import BasicNewsRecipe
15
16
17 def absurl(url):
18 if url.startswith('/'):
19 url = 'http://www.monde-diplomatique.fr' + url
20 return url
21
22
23 class LeMondeDiplomatiqueSiteWeb(BasicNewsRecipe):
24 title = 'Le Monde diplomatique.fr'
25 __author__ = 'Gaëtan Lehmann'
26 description = "Le Monde diplomatique est un mensuel français d’information et d’opinion à la ligne éditoriale nettement engagée en faveur d'une gauche de rupture avec le capitalisme. Il aborde de nombreux sujets — géopolitique, relations internationales, économie, questions sociales, écologie, culture, médias, …" # noqa: E501
27 oldest_article = 30
28 max_articles_per_feed = 100
29 auto_cleanup = True
30 publisher = 'monde-diplomatique.fr'
31 category = 'news, France, world'
32 language = 'fr'
33 masthead_url = 'http://www.monde-diplomatique.fr/squelettes/images/logotyfa.png'
34 timefmt = ' [%d %b %Y]'
35 no_stylesheets = True
36
37 recipe_specific_options = {
38 'days': {
39 'short': 'Oldest article to download from this news source. In days ',
40 'long': 'For example, 0.5, gives you articles from the past 12 hours',
41 'default': str(oldest_article),
42 }
43 }
44
45 def __init__(self, *args, **kwargs):
46 BasicNewsRecipe.__init__(self, *args, **kwargs)
47 d = self.recipe_specific_options.get('days')
48 if d and isinstance(d, str):
49 self.oldest_article = float(d)
50
51 feeds = [('Blogs', 'http://blog.mondediplo.net/spip.php?page=backend'), ('Archives', 'http://www.monde-diplomatique.fr/rss/')]
52
53 preprocess_regexps = [
54 (re.compile(r'<title>(.*) - Les blogs du Diplo</title>'), lambda m: '<title>' + m.group(1) + '</title>'),
55 (re.compile(r'<h2>(.*) - Les blogs du Diplo</h2>'), lambda m: '<h2>' + m.group(1) + '</h2>'),
56 (re.compile(r'<title>(.*) \(Le Monde diplomatique\)</title>'), lambda m: '<title>' + m.group(1) + '</title>'),
57 (re.compile(r'<h2>(.*) \(Le Monde diplomatique\)</h2>'), lambda m: '<h2>' + m.group(1) + '</h2>'),
58 (re.compile(r'<h3>Grand format</h3>'), lambda m: ''),
59 ]
60
61 remove_tags = [
62 dict(name='div', attrs={'class': 'voiraussi liste'}),
63 dict(name='ul', attrs={'class': 'hermetique carto hombre_demi_inverse'}),
64 dict(name='a', attrs={'class': 'tousles'}),
65 dict(name='h3', attrs={'class': 'cat'}),
66 dict(name='div', attrs={'class': 'logodiplo'}),
67 dict(name='img', attrs={'class': 'spip_logos'}),
68 dict(name='p', attrs={'id': 'hierarchie'}),
69 dict(name='div', attrs={'class': 'espace'}),
70 ]
71
72 conversion_options = {'comments': description, 'tags': category, 'publisher': publisher, 'linearize_tables': True}
73
74 remove_empty_feeds = True
75
76 filterDuplicates = True
77
78 # don't use parse_index - we need it to send an exception so we can mix
79 # feed and parse_index results in parse_feeds
80 def parse_index_valise(self):
81 articles = []
82 soup = self.index_to_soup('http://www.monde-diplomatique.fr/carnet/')
83 cnt = soup.find('ul', attrs={'class': 'liste double'})
84 for item in cnt.findAll('li'):
85 description = ''
86 feed_link = item.find('a', href=True)
87 title = self.tag_to_string(item.find('h3'))
88 desc = item.find('div', attrs={'class': 'intro'})
89 date = item.find('div', attrs={'class': 'dates_auteurs'})
90 if desc:
91 description = desc.string
92 if feed_link:
93 articles.append({'title': title, 'date': self.tag_to_string(date), 'url': absurl(feed_link['href']), 'description': description})
94 return [('La valise diplomatique', articles)]
95
96 def parse_index_cartes(self):
97 articles = []
98 soup = self.index_to_soup('http://www.monde-diplomatique.fr/cartes/')
99 cnt = soup.find('ul', attrs={'class': 'liste_vignettes hautcartes'})
100 for li in cnt.findAll('li'):
101 feed_link = li.find('a', href=True)
102 h3 = li.find('h3')
103 authorAndDate = li.find('div', attrs={'class': 'dates_auteurs'})
104 author_date = self.tag_to_string(authorAndDate).split(', ')
105 author = author_date[0]
106 date = author_date[-1]
107 if feed_link:
108 title = self.tag_to_string(h3)
109 articles.append({'title': title, 'date': date, 'url': absurl(feed_link['href']), 'description': author})
110 return [('Cartes', articles)]
111
112 def parse_feeds(self):
113 feeds = BasicNewsRecipe.parse_feeds(self)
114 valise = feeds_from_index(self.parse_index_valise(), oldest_article=self.oldest_article, max_articles_per_feed=self.max_articles_per_feed, log=self.log)
115 cartes = feeds_from_index(self.parse_index_cartes(), oldest_article=self.oldest_article, max_articles_per_feed=self.max_articles_per_feed, log=self.log)
116 feeds = valise + feeds + cartes
117 return feeds