| 1 |
#!/usr/bin/env python |
| 2 |
# vim:fileencoding=utf-8 |
| 3 |
""" |
| 4 |
liberation.fr |
| 5 |
""" |
| 6 |
|
| 7 |
import base64 |
| 8 |
import json |
| 9 |
import time |
| 10 |
from datetime import datetime, timedelta |
| 11 |
from urllib.parse import quote, urlencode, urlparse |
| 12 |
|
| 13 |
from mechanize import Request |
| 14 |
|
| 15 |
from calibre import browser |
| 16 |
from calibre.web.feeds.news import BasicNewsRecipe |
| 17 |
|
| 18 |
|
| 19 |
def resize(x): |
| 20 |
for k, v in x.items(): |
| 21 |
if '_750' in k: |
| 22 |
return v |
| 23 |
|
| 24 |
|
| 25 |
m_fr = { |
| 26 |
1: 'Janvier', |
| 27 |
2: 'Février', |
| 28 |
3: 'Mars', |
| 29 |
4: 'Avril', |
| 30 |
5: 'Mai', |
| 31 |
6: 'Juin', |
| 32 |
7: 'Juillet', |
| 33 |
8: 'Août', |
| 34 |
9: 'Septembre', |
| 35 |
10: 'Octobre', |
| 36 |
11: 'Novembre', |
| 37 |
12: 'Décembre', |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
def json_to_html(raw): |
| 42 |
data = json.loads(raw) |
| 43 |
|
| 44 |
title = '<h1>' + data['headlines']['basic'] + '</h1>\n' |
| 45 |
sub = '<p class="desc">' + data['subheadlines']['basic'] + '</p>' |
| 46 |
|
| 47 |
auth = '<p class="auth">{}</p>\n' |
| 48 |
dt = datetime.fromisoformat(data['last_updated_date'][:-1]) + timedelta(seconds=time.timezone) |
| 49 |
dt = dt.strftime(m_fr[dt.month] + ' %d, %Y') |
| 50 |
a = [x['name'] for x in data['credits']['by']] |
| 51 |
if a: |
| 52 |
auth = auth.format(', '.join(a) + ' | ' + dt) |
| 53 |
else: |
| 54 |
auth = auth.format(dt) |
| 55 |
|
| 56 |
lede = '' |
| 57 |
if data['promo_items']['basic'].get('type', '') == 'image': |
| 58 |
lede = '<br><img src="{}"><div class="figc">{}</div>\n'.format( |
| 59 |
resize(data['promo_items']['basic']['resized_image_urls']), data['promo_items']['basic'].get('caption', '') |
| 60 |
) |
| 61 |
|
| 62 |
body = '' |
| 63 |
for c in data['content_elements']: |
| 64 |
if c.get('type', '') == 'text': |
| 65 |
body += '\t<p>' + c['content'] + '</p>\n' |
| 66 |
if c.get('type', '') == 'image': |
| 67 |
body += '\t<br><img src="{}"><div class="figc">{}</div>\n'.format(resize(c['resized_image_urls']), c.get('caption', '')) |
| 68 |
if c.get('type', '') == 'header': |
| 69 |
body += '\t<h4>' + c['content'] + '</h4>\n' |
| 70 |
if c.get('type', '') == 'list': |
| 71 |
body += '\t<ul>' |
| 72 |
if 'items' in c: |
| 73 |
for l in c['items']: |
| 74 |
if 'content' in l: |
| 75 |
body += '<li>' + l['content'] + '</li>' |
| 76 |
body += '\t</ul>' |
| 77 |
if c.get('type', '') == 'oembed_response': |
| 78 |
if 'raw_oembed' in c: |
| 79 |
if 'html' in c['raw_oembed']: |
| 80 |
body += c['raw_oembed']['html'] |
| 81 |
|
| 82 |
return '<html><body><div>\n' + title + sub + auth + lede + body + '\n</div></body></html>' |
| 83 |
|
| 84 |
|
| 85 |
class Liberation(BasicNewsRecipe): |
| 86 |
title = 'Libération' |
| 87 |
__author__ = 'unkn0wn' |
| 88 |
description = ( |
| 89 |
"Libération est un quotidien d'information libre, vigilant et engagé. L'objectif de Libération est de " |
| 90 |
'fournir une information complète et vérifiée, dans tous les domaines. Sans préjugés, ni complaisance, ' |
| 91 |
"ses enquêtes reportages et analyses s'emploient à comprendre et à décrire l'actualité et à révéler " |
| 92 |
'les mutations des sociétés et des cultures.' |
| 93 |
) |
| 94 |
language = 'fr' |
| 95 |
oldest_article = 1.15 |
| 96 |
remove_empty_feeds = True |
| 97 |
articles_are_obfuscated = True |
| 98 |
timefmt = ' [%s]' % datetime.now().strftime(m_fr[datetime.now().month] + ' %d, %Y') |
| 99 |
ignore_duplicate_articles = {'title', 'url'} |
| 100 |
key = 'YTdYMjltQnZRZVAxTGQ5OENnRjJySzV1VHpXWTRo' |
| 101 |
masthead_url = 'https://journal.liberation.fr/img/logo.svg' |
| 102 |
extra_css = ''' |
| 103 |
.desc { font-style:italic; color:#202020; } |
| 104 |
.auth { font-size:small; } |
| 105 |
.figc { font-size:small; text-align:center; } |
| 106 |
blockquote { color:#202020; } |
| 107 |
''' |
| 108 |
|
| 109 |
recipe_specific_options = { |
| 110 |
'days': { |
| 111 |
'short': 'Oldest article to download from this news source. In days ', |
| 112 |
'long': 'For example, 0.5, gives you articles from the past 12 hours', |
| 113 |
'default': str(oldest_article), |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
def __init__(self, *args, **kwargs): |
| 118 |
BasicNewsRecipe.__init__(self, *args, **kwargs) |
| 119 |
d = self.recipe_specific_options.get('days') |
| 120 |
if d and isinstance(d, str): |
| 121 |
self.oldest_article = float(d) |
| 122 |
|
| 123 |
feeds = [ |
| 124 |
('A la une', 'https://www.liberation.fr/arc/outboundfeeds/rss-all/collection/accueil-une/?outputType=xml'), |
| 125 |
('Politique', 'https://www.liberation.fr/arc/outboundfeeds/rss-all/category/politique/?outputType=xml'), |
| 126 |
('International', 'https://www.liberation.fr/arc/outboundfeeds/rss-all/category/international/?outputType=xml'), |
| 127 |
('CheckNews', 'https://www.liberation.fr/arc/outboundfeeds/rss-all/category/checknews/?outputType=xml'), |
| 128 |
('Culture', 'https://www.liberation.fr/arc/outboundfeeds/rss-all/category/culture/?outputType=xml'), |
| 129 |
('Idées et Débats', 'https://www.liberation.fr/arc/outboundfeeds/rss-all/category/idees-et-debats/?outputType=xml'), |
| 130 |
('Société', 'https://www.liberation.fr/arc/outboundfeeds/rss-all/category/societe/?outputType=xml'), |
| 131 |
('Environnement', 'https://www.liberation.fr/arc/outboundfeeds/rss-all/category/environnement/?outputType=xml'), |
| 132 |
('Economie', 'https://www.liberation.fr/arc/outboundfeeds/rss-all/category/economie/?outputType=xml'), |
| 133 |
('Lifestyle', 'https://www.liberation.fr/arc/outboundfeeds/rss-all/category/lifestyle/?outputType=xml'), |
| 134 |
('Portraits', 'https://www.liberation.fr/arc/outboundfeeds/rss-all/category/portraits/?outputType=xml'), |
| 135 |
('Sports', 'https://www.liberation.fr/arc/outboundfeeds/rss-all/category/sports/?outputType=xml'), |
| 136 |
('Sciences', 'https://www.liberation.fr/arc/outboundfeeds/rss-all/category/sciences/?outputType=xml'), |
| 137 |
('Forums & événements', 'https://www.liberation.fr/arc/outboundfeeds/rss-all/category/forums/?outputType=xml'), |
| 138 |
('Libération', 'https://www.liberation.fr/arc/outboundfeeds/rss/?outputType=xml'), |
| 139 |
] |
| 140 |
|
| 141 |
def get_obfuscated_article(self, url): |
| 142 |
slug = urlparse(url).path |
| 143 |
br = browser() |
| 144 |
b64 = base64.b64decode(self.key) |
| 145 |
query = { |
| 146 |
'website': 'liberation', |
| 147 |
'website_url': str(slug), |
| 148 |
'published': 'true', |
| 149 |
'_sourceInclude': '_id,content_restrictions.content_code,credits,promo_items.basic.caption,promo_items.basic.credits,promo_items.basic.url,promo_items.basic.height,promo_items.basic.width,promo_items.basic.resized_image_urls,promo_items.basic.last_updated_date,promo_items.lead_art.caption,promo_items.lead_art.credits,promo_items.lead_art.url,promo_items.lead_art.height,promo_items.lead_art.width,promo_items.lead_art.resized_image_urls,promo_items.lead_art.last_updated_date,source.additional_properties.legacy_url,content_elements,source.source_id,taxonomy.primary_section.additional_properties.original._admin.alias_ids,taxonomy.primary_section.additional_properties.original.navigation.nav_title,taxonomy.primary_section._id,taxonomy.primary_section.name,taxonomy.primary_section.path,taxonomy.tags,label,subheadlines.basic,headlines.basic,source.additional_properties.legacy_url,source.source_type,first_publish_date,display_date,canonical_url', # noqa: E501 |
| 150 |
} |
| 151 |
headers = {'cache-control': 'public, max-age=5', 'x-api-key': b64.decode(), 'accept-encoding': 'gzip', 'user-agent': 'okhttp/4.11.0'} |
| 152 |
api = 'https://arc.api.liberation.fr/content/v4/?' + urlencode(query, safe='()!', quote_via=quote) |
| 153 |
rq = Request(url=api, headers=headers) |
| 154 |
raw = br.open(rq).read() |
| 155 |
data = {'data': json_to_html(raw), 'url': url} |
| 156 |
return data |
| 157 |
|
| 158 |
def get_cover_url(self): |
| 159 |
soup = self.index_to_soup('https://journal.liberation.fr/') |
| 160 |
cover = soup.find(name='img', attrs={'class': 'ui image'}) |
| 161 |
if cover: |
| 162 |
return 'https:' + cover['src'] |