| 1 |
""" |
| 2 |
afriquexxi.info |
| 3 |
""" |
| 4 |
|
| 5 |
from datetime import datetime |
| 6 |
from zoneinfo import ZoneInfo |
| 7 |
|
| 8 |
from calibre.utils.img import image_from_data |
| 9 |
from calibre.web.feeds.news import BasicNewsRecipe |
| 10 |
|
| 11 |
|
| 12 |
class AfriqueXXIRecipe(BasicNewsRecipe): |
| 13 |
title = 'Afrique XXI' |
| 14 |
description = 'Journal panafricain en ligne' |
| 15 |
language = 'fr' |
| 16 |
oldest_article = 30 |
| 17 |
max_articles_per_feed = 50 |
| 18 |
auto_cleanup = False |
| 19 |
encoding = 'utf-8' |
| 20 |
use_embedded_content = True |
| 21 |
no_stylesheets = True |
| 22 |
remove_javascript = True |
| 23 |
scale_news_images_to_device = True |
| 24 |
|
| 25 |
feeds = [('Afrique XXI', 'https://afriquexxi.info/?page=backend&lang=fr')] |
| 26 |
|
| 27 |
extra_css = ''' |
| 28 |
img { max-width: 100%; height: auto; display: block; margin: 1em auto; } |
| 29 |
h1 { font-size: 2em; margin: 1em 0; } |
| 30 |
h2 { font-size: 1.5em; margin: 1em 0; } |
| 31 |
''' |
| 32 |
|
| 33 |
def default_cover(self, cover_file): |
| 34 |
""" |
| 35 |
Crée une couverture personnalisée pour Afrique XXI |
| 36 |
""" |
| 37 |
from qt.core import QColor, QFont, QImage, QPainter, QPen, QRect, Qt |
| 38 |
|
| 39 |
from calibre.gui2 import ensure_app, load_builtin_fonts, pixmap_to_data |
| 40 |
|
| 41 |
try: |
| 42 |
# Préparation de l'environnement Qt |
| 43 |
ensure_app() |
| 44 |
load_builtin_fonts() |
| 45 |
|
| 46 |
# Date en français |
| 47 |
today = datetime.now(ZoneInfo('Europe/Paris')) |
| 48 |
wkd = today.weekday() |
| 49 |
french_weekday = {0: 'Lundi', 1: 'Mardi', 2: 'Mercredi', 3: 'Jeudi', 4: 'Vendredi', 5: 'Samedi', 6: 'Dimanche'} |
| 50 |
french_month = { |
| 51 |
1: 'janvier', |
| 52 |
2: 'février', |
| 53 |
3: 'mars', |
| 54 |
4: 'avril', |
| 55 |
5: 'mai', |
| 56 |
6: 'juin', |
| 57 |
7: 'juillet', |
| 58 |
8: 'août', |
| 59 |
9: 'septembre', |
| 60 |
10: 'octobre', |
| 61 |
11: 'novembre', |
| 62 |
12: 'décembre', |
| 63 |
} |
| 64 |
|
| 65 |
weekday = french_weekday[wkd] |
| 66 |
month = french_month[today.month] |
| 67 |
date_str = f'{weekday} {today.day} {month} {today.year}' |
| 68 |
edition = today.strftime('Édition de %Hh%M') |
| 69 |
|
| 70 |
# Création de l'image de base (ratio ~1.6 pour format livre) |
| 71 |
img = QImage(1200, 1920, QImage.Format_RGB888) |
| 72 |
img.fill(QColor(0, 255, 0)) # Fond vert électrique (lime) |
| 73 |
|
| 74 |
# Télécharger le logo |
| 75 |
logo_url = 'https://upload.wikimedia.org/wikipedia/fr/7/74/Afrique_xxi.png' |
| 76 |
|
| 77 |
# Utiliser urllib pour télécharger l'image |
| 78 |
try: |
| 79 |
logo_data = self.get_browser().open(logo_url).read() |
| 80 |
logo_img = image_from_data(logo_data) |
| 81 |
if not logo_img.isNull(): |
| 82 |
# Calculer la largeur proportionnelle pour une hauteur de 300px |
| 83 |
logo_height = 300 |
| 84 |
logo_width = int(logo_img.width() * (logo_height / logo_img.height())) |
| 85 |
|
| 86 |
# Texte du titre |
| 87 |
p = QPainter(img) |
| 88 |
|
| 89 |
# Centrer le logo |
| 90 |
logo_x = (1200 - logo_width) // 2 |
| 91 |
logo_y = 300 # Positionnement au-dessus du titre |
| 92 |
|
| 93 |
# Dessiner le logo |
| 94 |
p.drawImage(QRect(logo_x, logo_y, logo_width, logo_height), logo_img) |
| 95 |
|
| 96 |
# Couleur texte (noir) |
| 97 |
pen = QPen(QColor('black')) |
| 98 |
p.setPen(pen) |
| 99 |
|
| 100 |
# Titre principal |
| 101 |
font = QFont() |
| 102 |
font.setFamily('Liberation Sans') |
| 103 |
font.setPointSize(60) |
| 104 |
font.setBold(True) |
| 105 |
p.setFont(font) |
| 106 |
|
| 107 |
# Ajuster la position du titre pour être sous le logo |
| 108 |
title_rect = QRect(0, 700, 1200, 100) |
| 109 |
p.drawText(title_rect, Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter, 'AFRIQUE') |
| 110 |
|
| 111 |
# Sous-titre |
| 112 |
font.setPointSize(40) |
| 113 |
p.setFont(font) |
| 114 |
subtitle_rect = QRect(0, 800, 1200, 100) |
| 115 |
p.drawText(subtitle_rect, Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter, 'XXI') |
| 116 |
|
| 117 |
# Date et édition |
| 118 |
font.setPointSize(36) |
| 119 |
font.setBold(False) |
| 120 |
p.setFont(font) |
| 121 |
date_rect = QRect(0, 950, 1200, 100) |
| 122 |
p.drawText(date_rect, Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter, date_str) |
| 123 |
|
| 124 |
font.setPointSize(32) |
| 125 |
font.setItalic(True) |
| 126 |
p.setFont(font) |
| 127 |
edition_rect = QRect(0, 1050, 1200, 100) |
| 128 |
p.drawText(edition_rect, Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter, edition) |
| 129 |
|
| 130 |
p.end() |
| 131 |
|
| 132 |
except Exception as e: |
| 133 |
self.log.error(f'Erreur lors du téléchargement ou du traitement du logo: {e}') |
| 134 |
|
| 135 |
# Si échec du téléchargement du logo, créer quand même la couverture sans logo |
| 136 |
p = QPainter(img) |
| 137 |
pen = QPen(QColor('black')) |
| 138 |
p.setPen(pen) |
| 139 |
|
| 140 |
# Titre principal |
| 141 |
font = QFont() |
| 142 |
font.setFamily('Liberation Sans') |
| 143 |
font.setPointSize(60) |
| 144 |
font.setBold(True) |
| 145 |
p.setFont(font) |
| 146 |
|
| 147 |
# Positions standard sans logo |
| 148 |
title_rect = QRect(0, 700, 1200, 100) |
| 149 |
p.drawText(title_rect, Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter, 'AFRIQUE') |
| 150 |
|
| 151 |
# Sous-titre |
| 152 |
font.setPointSize(40) |
| 153 |
p.setFont(font) |
| 154 |
subtitle_rect = QRect(0, 800, 1200, 100) |
| 155 |
p.drawText(subtitle_rect, Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter, 'XXI') |
| 156 |
|
| 157 |
# Date et édition |
| 158 |
font.setPointSize(36) |
| 159 |
font.setBold(False) |
| 160 |
p.setFont(font) |
| 161 |
date_rect = QRect(0, 950, 1200, 100) |
| 162 |
p.drawText(date_rect, Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter, date_str) |
| 163 |
|
| 164 |
font.setPointSize(32) |
| 165 |
font.setItalic(True) |
| 166 |
p.setFont(font) |
| 167 |
edition_rect = QRect(0, 1050, 1200, 100) |
| 168 |
p.drawText(edition_rect, Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter, edition) |
| 169 |
|
| 170 |
p.end() |
| 171 |
|
| 172 |
# Sauvegarde de l'image |
| 173 |
img_data = pixmap_to_data(img) |
| 174 |
cover_file.write(img_data) |
| 175 |
cover_file.flush() |
| 176 |
return True |
| 177 |
|
| 178 |
except Exception as e: |
| 179 |
self.log.error(f'Erreur lors de la création de la couverture: {e}') |
| 180 |
return False |