-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathwikipedia.py
More file actions
63 lines (45 loc) · 1.49 KB
/
Copy pathwikipedia.py
File metadata and controls
63 lines (45 loc) · 1.49 KB
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
"""Client for the Wikipedia REST API, version 1.
See `API documentation <https://en.wikipedia.org/api/rest_v1/#/>`_.
"""
from dataclasses import dataclass
import click
import desert
import marshmallow
import requests
API_URL: str = "https://{language}.wikipedia.org/api/rest_v1/page/random/summary"
@dataclass
class Page:
"""Page resource.
Attributes:
title: The title of the Wikipedia page.
extract: A plain text summary.
"""
title: str
extract: str
schema = desert.schema(Page, meta={"unknown": marshmallow.EXCLUDE})
def random_page(language: str = "en") -> Page:
"""Return a random page.
Performs a GET request to the /page/random/summary endpoint.
Args:
language: The Wikipedia language edition. By default, the English
Wikipedia is used ("en").
Returns:
A page resource.
Raises:
ClickException: The HTTP request failed or the HTTP response
contained an invalid body.
Example:
>>> from hypermodern_python import wikipedia
>>> page = wikipedia.random_page(language="en")
>>> bool(page.title)
True
"""
url = API_URL.format(language=language)
try:
with requests.get(url) as response:
response.raise_for_status()
data = response.json()
return schema.load(data)
except (requests.RequestException, marshmallow.ValidationError) as error:
message = str(error)
raise click.ClickException(message)