python-loading-sdk 0.4.0__py3-none-any.whl → 0.5.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- loading_sdk/__init__.py +1 -1
- loading_sdk/_api.py +254 -0
- loading_sdk/_http.py +47 -0
- loading_sdk/_parsing.py +70 -0
- loading_sdk/async_api/client.py +149 -479
- loading_sdk/async_api/extractors.py +18 -80
- loading_sdk/sync_api/client.py +126 -443
- loading_sdk/sync_api/extractors.py +18 -80
- python_loading_sdk-0.5.0.dist-info/METADATA +185 -0
- python_loading_sdk-0.5.0.dist-info/RECORD +16 -0
- {python_loading_sdk-0.4.0.dist-info → python_loading_sdk-0.5.0.dist-info}/WHEEL +1 -1
- python_loading_sdk-0.4.0.dist-info/METADATA +0 -127
- python_loading_sdk-0.4.0.dist-info/RECORD +0 -14
- tests/test_api.py +0 -1924
- /tests/__init__.py → /loading_sdk/py.typed +0 -0
- {python_loading_sdk-0.4.0.dist-info → python_loading_sdk-0.5.0.dist-info/licenses}/LICENSE.md +0 -0
|
@@ -1,41 +1,25 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"""Standalone extractor interfaces retained for existing SDK callers."""
|
|
2
|
+
|
|
3
3
|
from abc import ABC, abstractmethod
|
|
4
4
|
|
|
5
|
-
import
|
|
6
|
-
|
|
5
|
+
import httpx
|
|
6
|
+
|
|
7
|
+
from loading_sdk._parsing import chunk_urls, main_script_path
|
|
8
|
+
from loading_sdk.async_api.client import AsyncLoadingApiClient
|
|
7
9
|
from loading_sdk.settings import BASE_URL, USER_AGENT
|
|
8
10
|
|
|
9
11
|
|
|
10
12
|
class Extractor(ABC):
|
|
11
13
|
async def get_source(self, url: str) -> str:
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
return await response.text()
|
|
16
|
-
|
|
17
|
-
def get_script(self, source: str) -> str:
|
|
18
|
-
soup = BeautifulSoup(source, "html.parser")
|
|
19
|
-
main_script = soup.find(src=re.compile(r"/static/js/main\.[0-9a-zA-Z]+\.js"))
|
|
20
|
-
|
|
21
|
-
return main_script["src"][1:]
|
|
22
|
-
|
|
23
|
-
def get_chunks(self, source: str) -> list:
|
|
24
|
-
chunk_urls = []
|
|
25
|
-
|
|
26
|
-
# Extracts the code with the javascript chunks.
|
|
27
|
-
match = re.search(r"(static/js/).+?(?=\{)(.+?(?=\[)).+(.chunk.js)", source)
|
|
28
|
-
|
|
29
|
-
if match:
|
|
30
|
-
# Transform the code into valid JSON so the chunk ids can be stored in a python dict.
|
|
31
|
-
file_name_values = re.sub(r"([0-9]+?(?=:))", r'"\1"', match.group(2))
|
|
32
|
-
chunk_ids = json.loads(file_name_values)
|
|
14
|
+
async with httpx.AsyncClient(follow_redirects=True, timeout=300) as client:
|
|
15
|
+
response = await client.get(url, headers={"User-Agent": USER_AGENT})
|
|
16
|
+
return response.text
|
|
33
17
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
chunk_urls.append(chunk_url)
|
|
18
|
+
def get_script(self, source: str) -> str | None:
|
|
19
|
+
return main_script_path(source)
|
|
37
20
|
|
|
38
|
-
|
|
21
|
+
def get_chunks(self, source: str) -> list[str]:
|
|
22
|
+
return chunk_urls(source, BASE_URL)
|
|
39
23
|
|
|
40
24
|
@abstractmethod
|
|
41
25
|
async def get_data(self):
|
|
@@ -44,60 +28,14 @@ class Extractor(ABC):
|
|
|
44
28
|
|
|
45
29
|
class AboutExtractor(Extractor):
|
|
46
30
|
async def get_data(self):
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
main_script_source = await self.get_source(f"{BASE_URL}/{main_script_url}")
|
|
50
|
-
chunk_urls = self.get_chunks(main_script_source)
|
|
51
|
-
about_script_url = chunk_urls[-1]
|
|
52
|
-
about_script_source = await self.get_source(about_script_url)
|
|
53
|
-
|
|
54
|
-
match = re.search(
|
|
55
|
-
r"var.e=(.+?)(?=\.map).+a=(.+?)(?=\.map)", about_script_source
|
|
56
|
-
)
|
|
57
|
-
|
|
58
|
-
if not match:
|
|
59
|
-
return None
|
|
60
|
-
|
|
61
|
-
people = re.sub(r"(\{|\,)([a-z]+)(\:)", r'\1"\2"\3', match.group(1))
|
|
62
|
-
people = re.sub(r"(.+)(')(.+)(')(.+)", r'\1"\3"\5', people)
|
|
63
|
-
people = people.replace('slags "vuxen p', "slags 'vuxen p")
|
|
64
|
-
people = people.replace('riktigt"-framtid', "riktigt'-framtid")
|
|
65
|
-
people = people.replace("\\n", "")
|
|
66
|
-
people = people.encode("utf-8").decode("unicode_escape")
|
|
67
|
-
|
|
68
|
-
moderators = re.sub(r"(\{|\,)([a-z]+)(\:)", r'\1"\2"\3', match.group(2))
|
|
69
|
-
moderators = re.sub(r"(.+)(')(.+)(')(.+)", r'\1"\3"\5', moderators)
|
|
70
|
-
moderators = moderators.replace("\\n", "")
|
|
71
|
-
moderators = moderators.encode("utf-8").decode("unicode_escape")
|
|
72
|
-
|
|
73
|
-
data = {
|
|
74
|
-
"people": json.loads(people),
|
|
75
|
-
"moderators": json.loads(moderators),
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
return data
|
|
31
|
+
response = await AsyncLoadingApiClient().get_about()
|
|
32
|
+
return response["data"]
|
|
79
33
|
|
|
80
34
|
|
|
81
35
|
class SocialsExtractor(Extractor):
|
|
82
36
|
async def get_data(self):
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
main_script_source = await self.get_source(f"{BASE_URL}/{main_script_url}")
|
|
86
|
-
|
|
87
|
-
match = re.findall(
|
|
88
|
-
r"(?:href:\")"
|
|
89
|
-
+ r"(https:\/\/|https:\/\/www.(.*?)\..*?\/.*?)"
|
|
90
|
-
+ r"(?:\",target:\"_blank\",rel:\"noreferrer noopener\",className:)"
|
|
91
|
-
+ r"(?:\"Footer-(?:icon|patreon)\")",
|
|
92
|
-
main_script_source,
|
|
93
|
-
)
|
|
94
|
-
|
|
95
|
-
if not match:
|
|
96
|
-
return None
|
|
97
|
-
|
|
98
|
-
data = [{"name": social[1], "link": social[0]} for social in match]
|
|
99
|
-
|
|
100
|
-
return data
|
|
37
|
+
response = await AsyncLoadingApiClient().get_socials()
|
|
38
|
+
return response["data"]
|
|
101
39
|
|
|
102
40
|
|
|
103
41
|
class ExtractorFactory(ABC):
|
|
@@ -116,7 +54,7 @@ class SocialsExtractorFactory(ExtractorFactory):
|
|
|
116
54
|
return SocialsExtractor()
|
|
117
55
|
|
|
118
56
|
|
|
119
|
-
async def extract_data(extractor_name):
|
|
57
|
+
async def extract_data(extractor_name: str):
|
|
120
58
|
factories = {
|
|
121
59
|
"about": AboutExtractorFactory(),
|
|
122
60
|
"socials": SocialsExtractorFactory(),
|