| 1 |
#!/usr/bin/env python |
| 2 |
""" |
| 3 |
frieze.com - Magazine d'art contemporain |
| 4 |
""" |
| 5 |
|
| 6 |
import re |
| 7 |
from datetime import datetime |
| 8 |
from zoneinfo import ZoneInfo |
| 9 |
|
| 10 |
from calibre.web.feeds.news import BasicNewsRecipe |
| 11 |
|
| 12 |
|
| 13 |
class FriezeMagazineRecipe(BasicNewsRecipe): |
| 14 |
title = 'Frieze Magazine' |
| 15 |
__author__ = 'Kabonix' |
| 16 |
description = "Magazine international d'art contemporain" |
| 17 |
language = 'en_GB' |
| 18 |
oldest_article = 60 |
| 19 |
max_articles_per_feed = 50 |
| 20 |
auto_cleanup = False |
| 21 |
encoding = 'utf-8' |
| 22 |
no_stylesheets = True |
| 23 |
remove_javascript = True |
| 24 |
scale_news_images_to_device = True |
| 25 |
|
| 26 |
base_url = 'https://www.frieze.com' |
| 27 |
|
| 28 |
keep_only_tags = [ |
| 29 |
{'class': 'article-header-container'}, |
| 30 |
{'class': 'article-header-title'}, |
| 31 |
{'class': 'article-header-headline'}, |
| 32 |
{'class': 'article-header-author'}, |
| 33 |
{'class': 'body-text'}, |
| 34 |
{'class': 'body-field'}, |
| 35 |
] |
| 36 |
|
| 37 |
remove_tags = [ |
| 38 |
{'class': 'social-share-container'}, |
| 39 |
{'class': 'share-buttons'}, |
| 40 |
{'class': 'article-header-social-responsive'}, |
| 41 |
{'class': 'newsletter-subscribe-container'}, |
| 42 |
{'class': 'suggested-articles-container'}, |
| 43 |
{'class': 'article-footer-container'}, |
| 44 |
{'class': 'ad-surround'}, |
| 45 |
{'id': re.compile(r'ad-.*')}, |
| 46 |
{'class': 'hidden'}, |
| 47 |
] |
| 48 |
|
| 49 |
extra_css = ''' |
| 50 |
img { max-width: 100%; height: auto; display: block; margin: 1em auto; } |
| 51 |
h1 { font-size: 2em; margin: 1em 0; font-weight: bold; } |
| 52 |
h2 { font-size: 1.5em; margin: 1em 0; font-weight: bold; } |
| 53 |
p { font-size: 1.1em; line-height: 1.6; margin-bottom: 1em; } |
| 54 |
figcaption { font-style: italic; font-size: 0.9em; color: #555; text-align: center; } |
| 55 |
em { font-style: italic; } |
| 56 |
.article-header-title { margin-bottom: 0.5em; } |
| 57 |
.article-header-headline { margin-bottom: 1.5em; } |
| 58 |
.article-header-author { font-size: 0.9em; margin-bottom: 2em; color: #555; } |
| 59 |
''' |
| 60 |
|
| 61 |
def parse_index(self): |
| 62 |
magazine_url = self.base_url + '/magazines/frieze-magazine' |
| 63 |
soup = self.index_to_soup(magazine_url) |
| 64 |
|
| 65 |
# Recherche du dernier numéro de façon plus robuste |
| 66 |
# Essayer plusieurs sélecteurs possibles pour trouver la liste des numéros |
| 67 |
issue_containers = soup.find_all('div', {'class': re.compile(r'teaser-search-col')}) |
| 68 |
|
| 69 |
# S'il n'y a pas de conteneurs trouvés avec la classe spécifique, cherchons plus largement |
| 70 |
if not issue_containers: |
| 71 |
self.log.warning('Recherche élargie des numéros du magazine') |
| 72 |
issue_containers = soup.find_all('div', {'class': re.compile(r'teaser')}) |
| 73 |
|
| 74 |
# Trouvons le premier numéro de magazine (le plus récent) parmi les conteneurs |
| 75 |
latest_issue = None |
| 76 |
for container in issue_containers: |
| 77 |
# Vérifie si c'est un conteneur de magazine (pas un article) |
| 78 |
if container.find('a', href=re.compile(r'/magazines/|/issues/')): |
| 79 |
latest_issue = container |
| 80 |
break |
| 81 |
|
| 82 |
if not latest_issue: |
| 83 |
self.log.warning('Aucun numéro spécifique trouvé, utilisation de la page principale') |
| 84 |
issue_url = magazine_url |
| 85 |
else: |
| 86 |
issue_link = latest_issue.find('a') |
| 87 |
if issue_link and issue_link.get('href'): |
| 88 |
issue_url = self.base_url + issue_link['href'] if issue_link['href'].startswith('/') else issue_link['href'] |
| 89 |
self.log.info(f'Dernier numéro trouvé: {issue_url}') |
| 90 |
else: |
| 91 |
issue_url = magazine_url |
| 92 |
self.log.warning('Lien vers le numéro non trouvé') |
| 93 |
|
| 94 |
self.log.info(f'Accès au numéro: {issue_url}') |
| 95 |
issue_soup = self.index_to_soup(issue_url) |
| 96 |
|
| 97 |
# Extraction plus robuste de l'image de couverture |
| 98 |
# Essayons plusieurs sélecteurs possibles |
| 99 |
cover_image = None |
| 100 |
|
| 101 |
# Méthode 1: Chercher dans la section d'en-tête du magazine |
| 102 |
cover_div = issue_soup.find('div', class_=re.compile(r'magazine-header-image|issue-header-image')) |
| 103 |
if cover_div: |
| 104 |
img_tag = cover_div.find('img') |
| 105 |
if img_tag and img_tag.get('src'): |
| 106 |
cover_image = img_tag['src'] |
| 107 |
|
| 108 |
# Méthode 2: Chercher dans la section principale |
| 109 |
if not cover_image: |
| 110 |
main_section = issue_soup.find('section', class_=re.compile(r'main|content')) |
| 111 |
if main_section: |
| 112 |
img_tag = main_section.find('img') |
| 113 |
if img_tag and img_tag.get('src'): |
| 114 |
cover_image = img_tag['src'] |
| 115 |
|
| 116 |
# Méthode 3: Recherche générale d'une grande image en haut de la page |
| 117 |
if not cover_image: |
| 118 |
for img in issue_soup.find_all('img', src=True)[:5]: # Limiter aux 5 premières images |
| 119 |
if 'cover' in img.get('src', '').lower() or 'header' in img.get('src', '').lower(): |
| 120 |
cover_image = img['src'] |
| 121 |
break |
| 122 |
|
| 123 |
if cover_image: |
| 124 |
self.cover_url = cover_image if cover_image.startswith('http') else self.base_url + cover_image |
| 125 |
self.log.info(f'Couverture trouvée: {self.cover_url}') |
| 126 |
else: |
| 127 |
self.log.warning("Pas d'image de couverture trouvée") |
| 128 |
|
| 129 |
# Extraction du titre du numéro |
| 130 |
issue_title = None |
| 131 |
for heading in issue_soup.find_all(['h1', 'h2']): |
| 132 |
if 'issue' in heading.text.lower() or 'magazine' in heading.text.lower(): |
| 133 |
issue_title = heading.text.strip() |
| 134 |
break |
| 135 |
|
| 136 |
if not issue_title: |
| 137 |
# Recherche plus générale |
| 138 |
for heading in issue_soup.find_all(['h1', 'h2'])[:3]: # Limiter aux 3 premiers titres |
| 139 |
issue_title = heading.text.strip() |
| 140 |
if issue_title: |
| 141 |
break |
| 142 |
|
| 143 |
if not issue_title: |
| 144 |
issue_title = 'Frieze Magazine - Dernier numéro' |
| 145 |
|
| 146 |
# Extraction des articles |
| 147 |
articles = [] |
| 148 |
|
| 149 |
# Recherche différents modèles de teasers d'articles |
| 150 |
article_containers = issue_soup.find_all('div', {'class': re.compile(r'teaser-content|article-teaser|article-item')}) |
| 151 |
|
| 152 |
if not article_containers: |
| 153 |
# Recherche plus large si les conteneurs spécifiques ne sont pas trouvés |
| 154 |
article_containers = issue_soup.find_all('div', {'class': re.compile(r'teaser|article')}) |
| 155 |
|
| 156 |
for container in article_containers: |
| 157 |
article_link = container.find('a') |
| 158 |
if not article_link or not article_link.get('href'): |
| 159 |
continue |
| 160 |
|
| 161 |
article_url = article_link['href'] |
| 162 |
if article_url.startswith('/'): |
| 163 |
article_url = self.base_url + article_url |
| 164 |
|
| 165 |
# Extraction du titre avec différentes classes possibles |
| 166 |
title_element = container.find(['div', 'h2', 'h3', 'h4'], {'class': re.compile(r'title|heading')}) |
| 167 |
title = title_element.text.strip() if title_element else 'Sans titre' |
| 168 |
|
| 169 |
# Extraction de la description |
| 170 |
desc_element = container.find(['div', 'p'], {'class': re.compile(r'deck|description|summary|excerpt')}) |
| 171 |
description = desc_element.text.strip() if desc_element else '' |
| 172 |
|
| 173 |
# Extraction de l'auteur |
| 174 |
author_element = container.find(['div', 'span'], {'class': re.compile(r'author|byline')}) |
| 175 |
if author_element: |
| 176 |
author_links = author_element.find_all('a') |
| 177 |
if author_links: |
| 178 |
author = ', '.join([a.text.strip() for a in author_links]) |
| 179 |
else: |
| 180 |
author = author_element.text.strip() |
| 181 |
else: |
| 182 |
author = '' |
| 183 |
|
| 184 |
# Vérification que c'est bien un article et pas un lien interne |
| 185 |
if '/article/' in article_url or '/feature/' in article_url or '/review/' in article_url: |
| 186 |
articles.append({ |
| 187 |
'title': title, |
| 188 |
'url': article_url, |
| 189 |
'description': description, |
| 190 |
'author': author, |
| 191 |
'date': datetime.now(ZoneInfo('Europe/Paris')).strftime('%Y-%m-%d'), |
| 192 |
}) |
| 193 |
self.log.debug(f'Article trouvé: {title} - {article_url}') |
| 194 |
|
| 195 |
self.log.info(f"Nombre d'articles trouvés: {len(articles)}") |
| 196 |
return [(issue_title, articles)] |
| 197 |
|
| 198 |
def get_cover_url(self): |
| 199 |
return getattr(self, 'cover_url', None) |
| 200 |
|
| 201 |
def preprocess_html(self, soup): |
| 202 |
# Nettoyage des éléments superflus |
| 203 |
for element in soup.find_all(class_=lambda c: c and ('share' in c or 'ad-' in c)): |
| 204 |
element.decompose() |
| 205 |
|
| 206 |
for tag in soup.find_all(['script', 'style']): |
| 207 |
tag.decompose() |
| 208 |
|
| 209 |
# Optimisation des images |
| 210 |
for img in soup.find_all('img'): |
| 211 |
# Sauvegarde des attributs importants seulement |
| 212 |
src = img.get('src') or img.get('data-src') or img.get('data-lazy-src') |
| 213 |
alt = img.get('alt', '') |
| 214 |
|
| 215 |
# Réinitialiser tous les attributs |
| 216 |
img.attrs = {} |
| 217 |
|
| 218 |
# Réappliquer seulement src et alt |
| 219 |
if src: |
| 220 |
img['src'] = src |
| 221 |
if alt: |
| 222 |
img['alt'] = alt |
| 223 |
|
| 224 |
# Formatage des métadonnées |
| 225 |
for class_name, style in [ |
| 226 |
('article-header-title', 'font-size: 2em; font-weight: bold;'), |
| 227 |
('article-header-headline', 'font-style: italic;'), |
| 228 |
('article-header-author', 'color: #555;'), |
| 229 |
]: |
| 230 |
element = soup.find('div', class_=class_name) |
| 231 |
if element: |
| 232 |
element.attrs.clear() |
| 233 |
element['style'] = style |
| 234 |
|
| 235 |
return soup |