This repository has been archived by the owner on Mar 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
run.py
executable file
·382 lines (316 loc) · 12.6 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#!/usr/bin/env python3
import itertools
import json
import re
from dataclasses import dataclass
from time import perf_counter
from typing import Any, List, Optional
import bleach
import httpx
import markdown
import minicli
from jinja2 import Environment, FileSystemLoader
from progressist import ProgressBar
from selectolax import parser
from truncate import Truncator
environment = Environment(loader=FileSystemLoader("."))
MATOMO_SITE_ID = 109
TIMEOUT = httpx.Timeout(15, read_timeout=60)
@dataclass
class Playlist:
slug: str
title: str
@dataclass(order=True)
class Dataset:
nb_hits: int # Keep it first for ordering.
default_order: int # Keep it second for ordering.
id: str # Useful to deduplicate.
title: str
source: str
page: str
acronym: Optional[str]
post_url: Optional[str]
logo_url: Optional[str]
certified: bool
description: str
excerpt: Optional[str] = ""
def __post_init__(self) -> None:
html_description = markdown.markdown(self.description)
self.populate_excerpt(html_description)
def __hash__(self) -> int:
"""Required for deduplication via set()."""
return hash(self.id)
def __eq__(self, other: "Dataset") -> bool:
"""Required for deduplication via set()."""
if isinstance(other, Dataset):
return self.id == other.id
raise NotImplementedError
def populate_excerpt(self, html_description: str, num_words: int = 50) -> None:
sanitized_description = bleach.clean(
html_description, tags=["h3", "p", "li", "ol", "ul"], strip=True,
)
truncated_description = Truncator(sanitized_description).words(
num=num_words, truncate="…", html=True
)
if len(truncated_description) > num_words * 10:
# May happen with Data INSEE sur les communes given the description
# https://www.data.gouv.fr/fr/datasets/data-insee-sur-les-communes/
truncated_description = Truncator(truncated_description).chars(
num=num_words * 10, truncate="…", html=True
)
# We do not want to keep h3 tags but neither have untagged content.
truncated_description = truncated_description.replace("h3", "p")
# And we do not need newlines in resulting HTML.
truncated_description = truncated_description.replace("\n", "")
self.excerpt = re.sub(r"http\S+", "", truncated_description)
@property
def asdict(self):
return {
"id": self.id,
"title": self.title,
"source": self.source,
"excerpt": self.excerpt,
"acronym": self.acronym,
"page": self.page,
"post_url": self.post_url,
"logo_url": self.logo_url,
"certified": self.certified,
}
async def fetch_stats_for(url: str) -> dict:
async with httpx.AsyncClient(base_url="https://stats.data.gouv.fr") as client:
params = {
"idSite": MATOMO_SITE_ID,
"module": "API",
"method": "Actions.getPageUrl",
"pageUrl": url,
"format": "json",
"period": "year",
"date": "last1",
"token_auth": "anonymous",
}
try:
response = await client.get("/", params=params, timeout=TIMEOUT)
except httpx.exceptions.ReadTimeout:
raise Exception(f"Timeout from {client.base_url}{url}")
return response.json()
async def fetch_json_data(url: str, headers: Optional[dict] = None) -> dict:
async with httpx.AsyncClient(
base_url="https://www.data.gouv.fr", headers=headers
) as client:
try:
response = await client.get(url, timeout=TIMEOUT)
except httpx.exceptions.ReadTimeout:
raise Exception(f"Timeout from {client.base_url}{url}")
result = response.json()
return {} if "message" in result else result
async def fetch_datagouv_page(url: str) -> List[str]:
async with httpx.AsyncClient(base_url="https://www.data.gouv.fr") as client:
try:
response = await client.get(url, timeout=TIMEOUT)
except httpx.exceptions.ReadTimeout:
raise Exception(f"Timeout from {client.base_url}{url}")
return response.text
def extract_source(item: dict) -> str:
if item["organization"]:
source = item["organization"]["name"]
elif item["owner"]:
source = f"{item['owner']['first_name']} {item['owner']['last_name']}"
else:
source = "Source inconnue"
return source
def extract_logo_url(item: dict) -> str:
if item["organization"]:
logo_url = item["organization"]["logo_thumbnail"]
elif item["owner"]:
logo_url = item["owner"]["avatar_thumbnail"]
else:
logo_url = ""
return logo_url
async def extract_certified(item: dict) -> bool:
if item["organization"]:
organization_badges = await fetch_json_data(
f"/api/1/organizations/{item['organization']['slug']}/",
headers={"X-Fields": "badges"},
)
return any(
badge["kind"] == "certified" for badge in organization_badges["badges"]
)
return False
async def convert_to_dataset(item: dict, index: int) -> Optional[Dataset]:
return Dataset(
nb_hits=item["metrics"].get("nb_hits", 0),
default_order=index,
id=item["id"],
title=item["title"],
source=extract_source(item),
description=item["description"],
acronym=item["acronym"],
page=item["page"],
post_url="",
logo_url=extract_logo_url(item),
certified=await extract_certified(item),
)
def deduplicate_datasets(datasets: List[Dataset]) -> List[Dataset]:
return set(datasets)
def write_datasets(datasets: List[Dataset], name="datasets.json") -> None:
print(f"Writing {len(datasets)} datasets to {name}")
data = [d.asdict for d in datasets]
open(name, "w").write(json.dumps(data, indent=2))
def extract_slug(url: str) -> str:
slug = url[len("https://www.data.gouv.fr/fr/datasets/") :]
if slug.endswith("/"):
slug = slug[:-1]
return slug
def flatten(list_of_lists: List[List[Any]]) -> List[Any]:
return list(itertools.chain(*list_of_lists))
async def fetch_datasets_from_urls(dataset_urls: List[str]) -> List[Dataset]:
print("Fetching datasets from URLs.")
dataset_slugs = [
extract_slug(dataset_url)
for dataset_url in dataset_urls
if dataset_url.startswith("https://www.data.gouv.fr/fr/datasets/")
]
datasets = []
bar = ProgressBar(total=len(dataset_slugs))
for i, dataset_slug in enumerate(bar.iter(dataset_slugs)):
data = await fetch_json_data(
f"/api/1/datasets/{dataset_slug}/",
headers={
"X-Fields": (
"id,title,metrics,description,acronym,page,"
"owner{first_name,last_name,avatar_thumbnail},"
"organization{name,slug,logo_thumbnail}"
)
},
)
if data and "id" in data:
dataset = await convert_to_dataset(data, i)
datasets.append(dataset)
return datasets
async def fetch_playlist(playlist: Playlist) -> List[Dataset]:
print(f"Fetching playlist {playlist.title}")
dataset = await fetch_json_data(
f"/api/1/datasets/{playlist.slug}/",
headers={"X-Fields": "resources{title,url}"},
)
for resource in dataset["resources"]:
if resource["title"] == playlist.title:
dataset_urls = await fetch_datagouv_page(resource["url"])
datasets = await fetch_datasets_from_urls(dataset_urls.split("\n"))
return datasets
async def fetch_playlists(playlists: List[Playlist]) -> List[Dataset]:
return flatten([await fetch_playlist(playlist) for playlist in playlists])
async def fetch_statistics(datasets: List[Dataset]) -> List[Dataset]:
print(f"Fetching statistics")
nb_updated_datasets = 0
bar = ProgressBar(total=len(datasets))
for dataset in bar.iter(datasets):
results = await fetch_stats_for(dataset.page)
if results["2020"]:
dataset.nb_hits = results["2020"][0]["nb_hits"]
nb_updated_datasets += 1
print(f"{nb_updated_datasets} datasets updated from Matomo")
return datasets
async def fetch_suivi_posts_urls():
print("Fetching lists of suivi posts URLs.")
posts_page = await fetch_datagouv_page("/fr/posts/")
posts_urls = {
post_link.attributes["href"]
for post_link in parser.HTMLParser(posts_page).css(
".search-results .post-result a"
)
if "href" in post_link.attributes
and "suivi-des-sorties" in post_link.attributes["href"]
}
return posts_urls
async def fetch_datasets_urls_from_posts_urls(suivi_posts_urls: List[str]) -> List[str]:
print("Discovering datasets URLs from posts.")
datasets_urls = []
for suivi_post_url in suivi_posts_urls:
post_page = await fetch_datagouv_page(suivi_post_url)
datasets_urls.append(
[
link.attributes["href"]
for link in parser.HTMLParser(post_page).css(".post-body a")
if "href" in link.attributes
and link.attributes["href"].startswith(
"https://www.data.gouv.fr/fr/datasets/"
)
]
)
return flatten(datasets_urls)
@minicli.cli
async def fetch_datagouv_posts() -> None:
suivi_posts_urls = await fetch_suivi_posts_urls()
datasets_urls = await fetch_datasets_urls_from_posts_urls(suivi_posts_urls)
datasets = await fetch_datasets_from_urls(datasets_urls)
datasets = await fetch_statistics(datasets)
write_datasets(sorted(datasets, reverse=True), name="datasets_posts.json")
async def fetch_matomo_crap() -> str:
"""Warning: quite fragile, we assume that from this page:
https://stats.data.gouv.fr/index.php?module=CoreHome&action=index&idSite=109
&period=range&date=previous30#?idSite=109&period=year&date=today&segment=
&category=General_Actions&subcategory=General_Pages
The first one is `fr` and the second one is `datasets` which is the most probable.
This is the AJAX query performed by Matomo to display the data because the
Matomo API and its documentation are a nightmare.
"""
async with httpx.AsyncClient(base_url="https://stats.data.gouv.fr") as client:
params = {
"idSite": MATOMO_SITE_ID,
"token_auth": "anonymous",
"date": "today",
"module": "Actions",
"action": "getPageUrls",
"period": "year",
"search_recursive": "1",
"keep_totals_row": "0",
"filter_sort_column": "nb_visits",
"filter_sort_order": "desc",
"idSubtable": "2",
}
try:
response = await client.get("/index.php", params=params, timeout=TIMEOUT)
except httpx.exceptions.ReadTimeout:
raise Exception(f"Timeout from {client.base_url}")
return response.text
def dataset_slug_to_url(slug: str) -> str:
return f"https://www.data.gouv.fr/fr/datasets/{slug}/"
async def fetch_popular_datasets_urls() -> List[str]:
print("Fetching popular datasets from Matomo")
matomo_crap = await fetch_matomo_crap()
# Let's make it look like a regular HTML page.
matomo_crap = (
f"<!doctype html><html><body><table>{matomo_crap}</table></body></html>"
)
datasets_urls = [
dataset_slug_to_url(dataset_slug.text().strip())
for dataset_slug in parser.HTMLParser(matomo_crap).css("tr td.first")
if not dataset_slug.text().strip().startswith("/")
and dataset_slug.text().strip() != "Others"
]
return datasets_urls
@minicli.cli
async def generate_data() -> None:
playlists = [
Playlist(slug="mes-playlists-13", title="SPD"),
]
playlists_datasets = await fetch_playlists(playlists)
suivi_posts_urls = await fetch_suivi_posts_urls()
suivi_datasets_urls = await fetch_datasets_urls_from_posts_urls(suivi_posts_urls)
posts_datasets = await fetch_datasets_from_urls(suivi_datasets_urls)
popular_datasets_urls = await fetch_popular_datasets_urls()
popular_datasets = await fetch_datasets_from_urls(popular_datasets_urls)
datasets = deduplicate_datasets(
playlists_datasets + posts_datasets + popular_datasets
)
datasets = await fetch_statistics(datasets)
write_datasets(sorted(datasets, reverse=True))
@minicli.wrap
def perf_wrapper():
start = perf_counter()
yield
elapsed = perf_counter() - start
print(f"Done in {elapsed:.5f} seconds.")
if __name__ == "__main__":
minicli.run()