| 1 |
#!/usr/bin/env python |
| 2 |
# vim:fileencoding=utf-8 |
| 3 |
# |
| 4 |
# 11 Jan 2021 - L. Houpert - Major changes in the Mediapart recipe: |
| 5 |
# 1) Summary of the article are noow available |
| 6 |
# 2) Additional sections International, France, Economie and Culture have |
| 7 |
# been added through custom entries in the function my_parse_index. |
| 8 |
# 3) Fix the cover image so it doesn't disappear from the Kindle menu |
| 9 |
# ( cover image format is changed to .jpeg) |
| 10 |
# 14 Jan 2021 - Add Mediapart Logo url as masthead_url and change cover |
| 11 |
# by overlaying the date on top of the Mediapart cover |
| 12 |
# 22 Mar 2023 - Switch to Google feeds |
| 13 |
|
| 14 |
from __future__ import unicode_literals |
| 15 |
|
| 16 |
__license__ = 'GPL v3' |
| 17 |
__copyright__ = '2021, Loïc Houpert <houpertloic at gmail .com>. Adapted from: 2016, Daniel Bonnery; 2009, Mathieu Godlewski; 2010-2012, Louis Gesbert' |
| 18 |
''' |
| 19 |
Mediapart |
| 20 |
''' |
| 21 |
|
| 22 |
from datetime import datetime, timedelta, timezone |
| 23 |
|
| 24 |
from calibre.ptempfile import PersistentTemporaryFile |
| 25 |
from calibre.web.feeds.news import BasicNewsRecipe, classes |
| 26 |
|
| 27 |
|
| 28 |
class Mediapart(BasicNewsRecipe): |
| 29 |
title = 'Mediapart' |
| 30 |
__author__ = 'Loïc Houpert, unkn0wn' |
| 31 |
description = 'Global news in French from news site Mediapart' |
| 32 |
publication_type = 'newspaper' |
| 33 |
language = 'fr' |
| 34 |
needs_subscription = True |
| 35 |
|
| 36 |
use_embedded_content = False |
| 37 |
no_stylesheets = True |
| 38 |
|
| 39 |
keep_only_tags = [classes('news__heading__top news__heading__center news__body__center__article')] |
| 40 |
|
| 41 |
remove_tags = [ |
| 42 |
classes('action-links media--rich read-also login-subscribe print-source_url screen-reader-only'), |
| 43 |
dict(name='svg'), |
| 44 |
] |
| 45 |
|
| 46 |
conversion_options = {'smarten_punctuation': True} |
| 47 |
|
| 48 |
masthead_url = 'https://raw.githubusercontent.com/lhoupert/calibre_contrib/main/mediapart_masthead.png' |
| 49 |
|
| 50 |
ignore_duplicate_articles = {'title'} |
| 51 |
resolve_internal_links = True |
| 52 |
remove_empty_feeds = True |
| 53 |
|
| 54 |
articles_are_obfuscated = True |
| 55 |
|
| 56 |
def get_obfuscated_article(self, url): |
| 57 |
br = self.get_browser() |
| 58 |
try: |
| 59 |
br.open(url) |
| 60 |
except Exception as e: |
| 61 |
url = e.hdrs.get('location') |
| 62 |
soup = self.index_to_soup(url) |
| 63 |
link = soup.find('a', href=True) |
| 64 |
skip_sections = [ # add sections you want to skip |
| 65 |
'/video/', |
| 66 |
'/videos/', |
| 67 |
'/media/', |
| 68 |
] |
| 69 |
if any(x in link['href'] for x in skip_sections): |
| 70 |
self.log('Aborting Article ', link['href']) |
| 71 |
self.abort_article('skipping video links') |
| 72 |
|
| 73 |
self.log('Downloading ', link['href']) |
| 74 |
html = br.open(link['href']).read() |
| 75 |
pt = PersistentTemporaryFile('.html') |
| 76 |
pt.write(html) |
| 77 |
pt.close() |
| 78 |
return pt.name |
| 79 |
|
| 80 |
feeds = [] |
| 81 |
|
| 82 |
sections = ['france', 'international', 'economie', 'culture-et-idees', 'politique', 'ecologie', 'fil-dactualites'] |
| 83 |
|
| 84 |
mediapart_feed = 'https://www.mediapart.fr/articles/feed' |
| 85 |
for sec in sections: |
| 86 |
feeds.append((sec.capitalize(), mediapart_feed)) |
| 87 |
feeds.append(('Autres', mediapart_feed)) |
| 88 |
|
| 89 |
def parse_feeds(self): |
| 90 |
feeds = BasicNewsRecipe.parse_feeds(self) |
| 91 |
for feed in feeds: |
| 92 |
feed_name = feed.title.lower() |
| 93 |
for article in feed.articles: |
| 94 |
if feed_name != 'autres' and feed_name not in article.url: |
| 95 |
feed.articles.remove(article) |
| 96 |
if feed_name == 'autres' and any(section in article.url for section in self.sections): |
| 97 |
feed.articles.remove(article) |
| 98 |
|
| 99 |
return feeds |
| 100 |
|
| 101 |
def get_browser(self): |
| 102 |
# -- Handle login |
| 103 |
br = BasicNewsRecipe.get_browser(self) |
| 104 |
if self.username is not None and self.password is not None: |
| 105 |
br.open('https://www.mediapart.fr/login') |
| 106 |
br.select_form(nr=0) |
| 107 |
br['username'] = self.username |
| 108 |
br['password'] = self.password |
| 109 |
br.submit() |
| 110 |
|
| 111 |
return br |
| 112 |
|
| 113 |
def default_cover(self, cover_file): |
| 114 |
""" |
| 115 |
Create a generic cover for recipes that don't have a cover |
| 116 |
""" |
| 117 |
from qt.core import QFont, QImage, QPainter, QPen, QRect, Qt |
| 118 |
|
| 119 |
from calibre.gui2 import ensure_app, load_builtin_fonts, pixmap_to_data |
| 120 |
|
| 121 |
def init_environment(): |
| 122 |
ensure_app() |
| 123 |
load_builtin_fonts() |
| 124 |
|
| 125 |
def create_cover_mediapart(date): |
| 126 |
"Create a cover for mediapart adding the date on Mediapart Cover" |
| 127 |
init_environment() |
| 128 |
# Get data |
| 129 |
image_url = 'https://raw.githubusercontent.com/lhoupert/calibre_contrib/main/mediapart.jpeg' |
| 130 |
data = self.index_to_soup(image_url, raw=True) |
| 131 |
# Get date and hour corresponding to french time zone |
| 132 |
today = datetime.now(timezone.utc) + timedelta(hours=1) |
| 133 |
wkd = today.weekday() |
| 134 |
french_weekday = {0: 'Mon', 1: 'Mar', 2: 'Mer', 3: 'Jeu', 4: 'Ven', 5: 'Sam', 6: 'Dim'} |
| 135 |
day = french_weekday[wkd] + '.' |
| 136 |
date = day + ' ' + today.strftime('%d %b. %Y') |
| 137 |
edition = today.strftime('Édition de %Hh') |
| 138 |
|
| 139 |
# Get Cover data |
| 140 |
img = QImage() |
| 141 |
img.loadFromData(data) |
| 142 |
|
| 143 |
# Overlay date on cover |
| 144 |
p = QPainter(img) |
| 145 |
pen = QPen(Qt.black) |
| 146 |
pen.setWidth(6) |
| 147 |
p.setPen(pen) |
| 148 |
font = QFont() |
| 149 |
font.setFamily('Times') |
| 150 |
font.setPointSize(72) |
| 151 |
p.setFont(font) |
| 152 |
r = QRect(0, 600, 744, 100) |
| 153 |
p.drawText(r, Qt.AlignmentFlag.AlignJustify | Qt.AlignmentFlag.AlignVCenter | Qt.AlignmentFlag.AlignCenter, date) |
| 154 |
p.end() |
| 155 |
|
| 156 |
# Overlay edition information on cover |
| 157 |
p = QPainter(img) |
| 158 |
pen = QPen(Qt.black) |
| 159 |
pen.setWidth(4) |
| 160 |
p.setPen(pen) |
| 161 |
font = QFont() |
| 162 |
font.setFamily('Times') |
| 163 |
font.setItalic(True) |
| 164 |
font.setPointSize(66) |
| 165 |
p.setFont(font) |
| 166 |
# Add date |
| 167 |
r = QRect(0, 720, 744, 100) |
| 168 |
p.drawText(r, Qt.AlignmentFlag.AlignJustify | Qt.AlignmentFlag.AlignVCenter | Qt.AlignmentFlag.AlignCenter, edition) |
| 169 |
p.end() |
| 170 |
return pixmap_to_data(img) |
| 171 |
|
| 172 |
try: |
| 173 |
today = datetime.today() |
| 174 |
date = today.strftime('%d %b %Y') |
| 175 |
img_data = create_cover_mediapart(date) |
| 176 |
cover_file.write(img_data) |
| 177 |
cover_file.flush() |
| 178 |
except Exception: |
| 179 |
self.log.exception('Failed to generate default cover') |
| 180 |
return False |
| 181 |
return True |