-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheudic.py
102 lines (83 loc) · 3.46 KB
/
eudic.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
import asyncio
import logging
import pprint
import httpx
from async_lru import alru_cache
from config import EUDIC_TOKEN
headers = {
"User-Agent": "insomnium/0.2.3-a",
"Authorization": EUDIC_TOKEN,
}
async def add_words_to_eudic(payload: dict) -> dict:
"""
Asynchronously adds words to the Eudic word list via a POST request.
Parameters:
payload (dict): The payload containing words to add.
Returns:
dict: The response from the Eudic API.
"""
url = "https://api.frdic.com/api/open/v1/studylist/words"
try:
async with httpx.AsyncClient(timeout=120) as client:
response = await client.post(url, json=payload, headers=headers)
response.raise_for_status() # Raises an exception for 4XX/5XX errors
return response.json() # Directly return the JSON response
except httpx.RequestError as e:
logging.error(f"Request error occurred: {e}")
raise # Rethrowing the exception for the caller to handle
except httpx.HTTPStatusError as e:
logging.error(f"HTTP error occurred: {e}")
raise # Rethrowing the exception for the caller to handle
async def remove_words_from_eudic(payload: dict) -> bool:
"""
Asynchronously deletes words from the Eudic word list via a DELETE request.
Parameters:
payload (dict): The payload containing words to delete.
Returns:
bool: True if deletion was successful, False otherwise.
"""
url = "https://api.frdic.com/api/open/v1/studylist/words"
try:
async with httpx.AsyncClient(timeout=120) as client:
response = await client.request(
url=url, method="DELETE", json=payload, headers=headers
)
if response.status_code == 204:
return True
else:
logging.error("Failed to delete words: HTTP %s", response.status_code)
return False
except httpx.RequestError as e:
logging.error("Request error occurred: %s", str(e))
return False
except Exception as e:
logging.error("Unexpected error occurred: %s", str(e))
return False
@alru_cache(ttl=3600 * 1)
async def list_eudic_vocabulary(page, page_size=50):
querystring = {"language": "en", "page": str(page), "page_size": str(page_size)}
url = "https://api.frdic.com/api/open/v1/studylist/words/0"
if page_size == 0:
querystring = {"language": "en"}
try:
async with httpx.AsyncClient(timeout=120) as client:
response = await client.get(url, headers=headers, params=querystring)
logging.debug(f"{response.headers=}, {url=}, {querystring=}, {headers=}")
return response.json()["data"]
except httpx.RequestError as e:
logging.error(f"Request error occurred: {e}")
raise # Rethrowing the exception for the caller to handle
except httpx.HTTPStatusError as e:
logging.error(f"HTTP error occurred: {e}")
raise # Rethrowing the exception for the caller to handle
except Exception as e:
logging.exception(f"Unexpected error occurred: {e}", exc_info=True)
raise # Rethrowing the exception for the caller to handle
def format_words(words: list[dict[str]]):
for w in words:
if w.get("exp"):
w["exp"] = w["exp"].replace("<br>", "\n")
logging.debug(pprint.pformat(words))
return [w["word"] for w in words]
if __name__ == "__main__":
asyncio.run(list_eudic_vocabulary(page=44))