-
Notifications
You must be signed in to change notification settings - Fork 4
/
apis.py
47 lines (36 loc) · 1.51 KB
/
apis.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
"""
dayong.exts.apis
~~~~~~~~~~~~~~~~
Module in charge of retrieving content from API endpoints.
"""
import asyncio
import json
import urllib.request
from typing import Any
from dayong.exts.contents import ThirdPartyContent
from dayong.interfaces import Client
from dayong.settings import CONTENT_PROVIDER
class RESTClient(Client):
"""Represents a client for interacting with REST APIs."""
_headers = {"User-Agent": "Mozilla/5.0"}
_request = urllib.request.Request("http://127.0.0.1", headers=_headers)
@staticmethod
async def get_content(*args: Any, **kwargs: Any) -> ThirdPartyContent:
loop = asyncio.get_running_loop()
resp = await loop.run_in_executor(None, urllib.request.urlopen, args[0])
data = await loop.run_in_executor(None, json.loads, resp.read())
return ThirdPartyContent(data, list(kwargs.values())[0])
async def get_devto_article(self, sort_by_date: bool = False) -> ThirdPartyContent:
"""Retrieve URLs of dev.to articles.
Args:
sort_by_date (bool, optional): Whether to order articles by descending
publish date. Defaults to False.
Returns:
list[str]: List of article URLs.
"""
request = self._request
if sort_by_date:
request.full_url = f"""{CONTENT_PROVIDER["dev"]}/api/articles/latest/"""
else:
request.full_url = f"""{CONTENT_PROVIDER["dev"]}/api/articles/"""
return await RESTClient.get_content(request, constraint="canonical_url")