Debian Codegraph @

recipes/contretemps.recipe

raw
1 #!/usr/bin/env python
2
3 """
4 contretemps.eu
5 """
6
7 from datetime import datetime
8 from zoneinfo import ZoneInfo
9
10 from calibre.web.feeds.news import BasicNewsRecipe
11
12
13 class ContretempsRecipe(BasicNewsRecipe):
14 title = 'Contretemps'
15 __author__ = 'Kabonix'
16 description = 'Revue de critique communiste'
17 publisher = 'Contretemps'
18 category = 'politique, théorie, stratégie'
19 language = 'fr'
20 encoding = 'utf-8'
21 oldest_article = 60
22 max_articles_per_feed = 30
23 publication_type = 'magazine'
24
25 no_stylesheets = True
26 remove_javascript = True
27 auto_cleanup = False
28
29 feeds = [
30 ('Théorie', 'https://www.contretemps.eu/category/theorie/feed/'),
31 ('Stratégie', 'https://www.contretemps.eu/category/strategie/feed/'),
32 ('Conjoncture', 'https://www.contretemps.eu/category/conjoncture/feed/'),
33 ]
34
35 def get_cover_url(self):
36 return None
37
38 def default_cover(self, cover_file):
39 """
40 Crée une couverture personnalisée pour Contretemps
41 """
42 from qt.core import QColor, QFont, QImage, QPainter, QPen, QRect, Qt
43
44 from calibre.gui2 import ensure_app, load_builtin_fonts, pixmap_to_data
45
46 try:
47 # Préparation de l'environnement Qt
48 ensure_app()
49 load_builtin_fonts()
50
51 # Date en français
52 today = datetime.now(ZoneInfo('Europe/Paris'))
53 wkd = today.weekday()
54 french_weekday = {0: 'Lundi', 1: 'Mardi', 2: 'Mercredi', 3: 'Jeudi', 4: 'Vendredi', 5: 'Samedi', 6: 'Dimanche'}
55 french_month = {
56 1: 'janvier',
57 2: 'février',
58 3: 'mars',
59 4: 'avril',
60 5: 'mai',
61 6: 'juin',
62 7: 'juillet',
63 8: 'août',
64 9: 'septembre',
65 10: 'octobre',
66 11: 'novembre',
67 12: 'décembre',
68 }
69
70 weekday = french_weekday[wkd]
71 month = french_month[today.month]
72 date_str = f'{weekday} {today.day} {month} {today.year}'
73 edition = today.strftime('Édition de %Hh%M')
74
75 # Création de l'image de base (ratio ~1.6 pour format livre)
76 img = QImage(1200, 1920, QImage.Format_RGB888)
77 img.fill(QColor('white'))
78
79 # Téléchargement et ajout du logo
80 br = self.get_browser()
81 logo_data = br.open_novisit('https://www.contretemps.eu/wp-content/uploads/ct14-1-512x241.jpg').read()
82 logo_img = QImage()
83 logo_img.loadFromData(logo_data)
84
85 # Position du logo (centré)
86 x = (img.width() - logo_img.width()) // 2
87 y = (img.height() - logo_img.height()) // 2 - 200 # Un peu plus haut que le centre
88
89 # Dessiner le logo
90 p = QPainter(img)
91 p.drawImage(x, y, logo_img)
92 p.end()
93
94 # Ajout des textes
95 painters = [
96 (36, 'Liberation Serif', False, False, date_str, (0, y + logo_img.height() + 100, 1200, 100)),
97 (32, 'Liberation Serif', True, True, edition, (0, y + logo_img.height() + 200, 1200, 100)),
98 ]
99
100 for font_size, font_family, is_bold, is_italic, text, rect in painters:
101 p = QPainter(img)
102 pen = QPen(QColor('black'))
103 p.setPen(pen)
104
105 font = QFont()
106 font.setFamily(font_family)
107 font.setPointSize(font_size)
108 font.setBold(is_bold)
109 font.setItalic(is_italic)
110 p.setFont(font)
111
112 r = QRect(*rect)
113 p.drawText(r, Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter, text)
114 p.end()
115
116 # Sauvegarde de l'image
117 img_data = pixmap_to_data(img)
118 cover_file.write(img_data)
119 cover_file.flush()
120 return True
121
122 except Exception as e:
123 self.log.error(f'Erreur lors de la création de la couverture: {e}')
124 return False
125
126 keep_only_tags = [dict(name='article', attrs={'class': 'post'})]
127
128 remove_tags = [
129 dict(name='div', attrs={'class': ['tags', 'share-buttons', 'related-posts']}),
130 dict(name='div', attrs={'class': ['comments-area', 'nav-links']}),
131 dict(name='div', attrs={'id': ['sidebar', 'comments']}),
132 dict(name='section', attrs={'class': 'comments-area'}),
133 dict(name='footer'),
134 dict(name='div', attrs={'class': 'yarpp-related'}),
135 dict(name='div', attrs={'class': 'related-posts'}),
136 ]
137
138 def preprocess_html(self, soup):
139 # Nettoyage des styles
140 for tag in soup.findAll(True):
141 if 'style' in tag.attrs:
142 del tag['style']
143
144 # Nettoyage des images
145 for img in soup.findAll('img'):
146 for attr in ['srcset', 'sizes', 'loading', 'class', 'width', 'height', 'decoding', 'data-lazy-srcset', 'data-lazy-sizes']:
147 if attr in img.attrs:
148 del img[attr]
149
150 if 'data-lazy-src' in img.attrs:
151 img['src'] = img['data-lazy-src']
152 del img['data-lazy-src']
153
154 if img.get('src', '').startswith('/'):
155 img['src'] = 'https://www.contretemps.eu' + img['src']
156
157 # Suppression de tout ce qui suit "à voir aussi"
158 for text in soup.findAll(text=lambda text: text and 'à voir aussi' in text.lower()):
159 parent = text.parent
160 for sibling in list(parent.find_next_siblings()):
161 sibling.decompose()
162 parent.decompose()
163
164 return soup
165
166 extra_css = '''
167 h1 { font-size: 1.8em; font-weight: bold; margin: 0 0 1em 0; }
168 h2 { font-size: 1.4em; font-weight: bold; margin: 1em 0; }
169 .author { font-style: italic; margin-bottom: 1.5em; color: #666; }
170 img { max-width: 100%; height: auto; margin: 1em auto; }
171 figcaption { font-size: 0.9em; font-style: italic; color: #666; margin: 0.5em 0 1.5em 0; }
172 p { margin-bottom: 1em; line-height: 1.5; }
173 blockquote { margin: 1em 2em; font-style: italic; }
174 .wp-block-quote { margin: 1em 0; padding: 0 1em; border-left: 3px solid #ccc; }
175 '''