-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
114 lines (92 loc) · 3.06 KB
/
api.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
import logging
import uuid
from datetime import datetime
from typing import Dict
import requests
from PIL import Image
from languages import LANGS
TODAY = datetime.utcnow().strftime("%Y/%m/%d")
TODAY_ISO = datetime.utcnow().strftime("%Y-%m-%d") # ISO 8601 compliant
API_URL = "https://api.wikimedia.org/"
HEADERS = {
"User-Agent": f"CursoryBot/0.0.1 (https://github.com/jake-anto/cursory; cursory@itsjake.me) requests/{requests.__version__}"
}
def get_featured(lang="en") -> Dict:
"""Get the featured content for today from the Wikimedia API.
Parameters
----------
lang : str, optional
The language to get the featured content for, by default 'en'
Returns
-------
dict
The featured content for today.
"""
response = requests.get(
API_URL + f"feed/v1/wikipedia/{lang}/featured/{TODAY}", headers=HEADERS
)
if response.status_code == 200:
return response.json()
def optimize_image(link: str, lang: str) -> str:
"""Convert an image to WebP and save it to the site/lang/ directory.
Parameters
----------
link : str
The link to the image to optimize.
lang : str
To determine the directory to save the image to.
Returns
-------
str
The link to the optimized image. If the image could not be optimized,
the original link is returned.
"""
try:
file_id = uuid.uuid4().hex
filename = f"site/{lang}/{file_id}.webp"
image = Image.open(requests.get(link, stream=True, headers=HEADERS).raw)
image.save(filename, format="webp")
return f"/{lang}/{file_id}.webp"
except Exception:
logging.warning("Could not optimize image. Fallback to original link instead.")
return link
def generate_sitemap(canonical_url: str) -> str:
"""Generate a sitemap for the given languages.
Parameters
----------
canonical_url : str
The canonical URL of the site.
Returns
-------
str
The sitemap.
"""
sitemap = """<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xhtml="http://www.w3.org/1999/xhtml">
"""
for lang in LANGS:
# Add the homepage for each language
# If the language is English, the homepage is just canonical_url
sitemap += f"""
<url>
<loc>{canonical_url}{f'{lang}/' if lang != 'en' else ''}</loc>
<lastmod>{TODAY_ISO}</lastmod>
<changefreq>daily</changefreq>
"""
# Add alternate links for each language
# https://developers.google.com/search/docs/specialty/international/localized-versions#sitemap
for lang in LANGS:
sitemap += f"""
<xhtml:link
rel="alternate"
hreflang="{lang}"
href="{canonical_url}{f'{lang}/' if lang != 'en' else ''}"
/>
"""
sitemap += "</url>"
sitemap += f"""\n<url>
<loc>{canonical_url}about</loc>
</url>"""
sitemap += "\n</urlset>"
return sitemap