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.
@@ -1,41 +1,25 @@
1
- import json
2
- import re
1
+ """Standalone extractor interfaces retained for existing SDK callers."""
2
+
3
3
  from abc import ABC, abstractmethod
4
4
 
5
- import requests
6
- from bs4 import BeautifulSoup
5
+ import httpx
6
+
7
+ from loading_sdk._parsing import chunk_urls, main_script_path
7
8
  from loading_sdk.settings import BASE_URL, USER_AGENT
9
+ from loading_sdk.sync_api.client import LoadingApiClient
8
10
 
9
11
 
10
12
  class Extractor(ABC):
11
13
  def get_source(self, url: str) -> str:
12
- headers = {"User-Agent": USER_AGENT}
13
- response = requests.get(url, headers=headers, timeout=10)
14
-
15
- return 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
+ with httpx.Client(follow_redirects=True, timeout=10) as client:
15
+ response = client.get(url, headers={"User-Agent": USER_AGENT})
16
+ return response.text
33
17
 
34
- for key, value in chunk_ids.items():
35
- chunk_url = f"{BASE_URL}/{match.group(1)}{key}.{value}{match.group(3)}"
36
- chunk_urls.append(chunk_url)
18
+ def get_script(self, source: str) -> str | None:
19
+ return main_script_path(source)
37
20
 
38
- return chunk_urls
21
+ def get_chunks(self, source: str) -> list[str]:
22
+ return chunk_urls(source, BASE_URL)
39
23
 
40
24
  @abstractmethod
41
25
  def get_data(self):
@@ -44,60 +28,14 @@ class Extractor(ABC):
44
28
 
45
29
  class AboutExtractor(Extractor):
46
30
  def get_data(self):
47
- about_page_source = self.get_source(f"{BASE_URL}/om")
48
- main_script_url = self.get_script(about_page_source)
49
- main_script_source = 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 = 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 = LoadingApiClient().get_about()
32
+ return response["data"]
79
33
 
80
34
 
81
35
  class SocialsExtractor(Extractor):
82
36
  def get_data(self):
83
- page_source = self.get_source(BASE_URL)
84
- main_script_url = self.get_script(page_source)
85
- main_script_source = 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 = LoadingApiClient().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
- def extract_data(extractor_name):
57
+ def extract_data(extractor_name: str):
120
58
  factories = {
121
59
  "about": AboutExtractorFactory(),
122
60
  "socials": SocialsExtractorFactory(),
@@ -0,0 +1,185 @@
1
+ Metadata-Version: 2.4
2
+ Name: python-loading-sdk
3
+ Version: 0.5.0
4
+ Summary: Synchronous and asynchronous Python clients for Loading.se
5
+ Author: hnrkcode
6
+ Author-email: hnrkcode <44243358+hnrkcode@users.noreply.github.com>
7
+ License-Expression: MIT
8
+ License-File: LICENSE.md
9
+ Classifier: Programming Language :: Python :: 3 :: Only
10
+ Classifier: Programming Language :: Python :: 3.11
11
+ Classifier: Programming Language :: Python :: 3.12
12
+ Classifier: Programming Language :: Python :: 3.13
13
+ Classifier: Programming Language :: Python :: 3.14
14
+ Classifier: Programming Language :: Python :: 3.15
15
+ Requires-Dist: httpx>=0.28.1,<0.29
16
+ Requires-Dist: beautifulsoup4>=4.15,<5
17
+ Requires-Python: >=3.11
18
+ Project-URL: Repository, https://github.com/hnrkcode/python-loading-sdk
19
+ Project-URL: Changelog, https://github.com/hnrkcode/python-loading-sdk/blob/main/CHANGELOG.md
20
+ Project-URL: Issues, https://github.com/hnrkcode/python-loading-sdk/issues
21
+ Description-Content-Type: text/markdown
22
+
23
+ # Python Loading SDK
24
+
25
+ [![PyPI Version][pypi-image]][pypi-url]
26
+ [![Supported Python versions][versions-image]][versions-url]
27
+ [![Build Status][build-image]][build-url]
28
+
29
+ [pypi-image]: https://img.shields.io/pypi/v/python-loading-sdk
30
+ [pypi-url]: https://pypi.org/project/python-loading-sdk/
31
+ [versions-image]: https://img.shields.io/pypi/pyversions/python-loading-sdk
32
+ [versions-url]: https://pypi.org/project/python-loading-sdk/
33
+ [build-image]: https://github.com/hnrkcode/python-loading-sdk/actions/workflows/build.yaml/badge.svg
34
+ [build-url]: https://github.com/hnrkcode/python-loading-sdk/actions/workflows/build.yaml
35
+
36
+ Access the Loading.se forums and editorial content from synchronous or asynchronous Python code.
37
+
38
+ ## Install
39
+
40
+ Requires Python 3.11 or newer.
41
+
42
+ ```
43
+ pip install python-loading-sdk
44
+ ```
45
+
46
+ See the [release notes](https://github.com/hnrkcode/python-loading-sdk/blob/main/CHANGELOG.md) for Python support and HTTP behavior changes.
47
+
48
+ ## Usage
49
+
50
+ Create a client without credentials to read public content. Provide credentials to access your profile or create and edit posts.
51
+
52
+ ```python
53
+ from loading_sdk import LoadingApiClient
54
+
55
+ client = LoadingApiClient(email="your@email.com", password="your_password")
56
+
57
+ response = client.get_profile()
58
+ ```
59
+
60
+ The async factory is awaitable.
61
+
62
+ ```python
63
+ from loading_sdk import AsyncLoadingApiClient
64
+
65
+ client = await AsyncLoadingApiClient(email="your@email.com", password="your_password")
66
+
67
+ response = await client.get_profile()
68
+ ```
69
+
70
+ ### Connection ownership and errors
71
+
72
+ Ordinary calls open and close a temporary HTTPX client for each SDK operation. Pagination and extraction reuse that connection pool across their requests. Login cookies stay with the SDK instance between operations.
73
+
74
+ A context manager shares one pool across multiple operations.
75
+
76
+ ```python
77
+ with LoadingApiClient() as client:
78
+ games = client.get_games()
79
+ socials = client.get_socials()
80
+
81
+ async with await AsyncLoadingApiClient() as client:
82
+ games = await client.get_games()
83
+ socials = await client.get_socials()
84
+ ```
85
+
86
+ Both factories accept a keyword-only `transport` for custom HTTPX transports, including `httpx.MockTransport`. Supplying a transport gives the SDK ownership and enables pooling immediately. Use the context manager or call `close()` / `await aclose()` to release it. Each SDK instance needs its own transport.
87
+
88
+ The SDK follows redirects. It sends login cookies only on authenticated API calls, respecting the API origin and cookie domain, path, and security attributes. A redirect that would replay an authenticated form to another origin raises `httpx.RequestError`.
89
+
90
+ Sync requests default to a 10-second timeout per network phase. Async requests default to a 300-second total deadline per request, including redirects, with a 30-second connection timeout. Set `timeout` in seconds on either factory to change the deadline or phase limit. Network failures propagate as HTTPX exceptions, including `httpx.TimeoutException` and `httpx.ConnectError`. API error responses retain their SDK dictionary format.
91
+
92
+ ## Examples
93
+
94
+ ### Requires Auth
95
+
96
+ ```python
97
+ response = client.get_profile()
98
+ ```
99
+
100
+ ```python
101
+ response = client.create_post(
102
+ thread_id="5bbb986af1deda001d33bc4b", message="My message!"
103
+ )
104
+ ```
105
+
106
+ ```python
107
+ response = client.edit_post(
108
+ post_id="5bc876dd70a79c001dab7ebe", message="My updated message!"
109
+ )
110
+ ```
111
+
112
+ ```python
113
+ response = client.create_thread(
114
+ title="My title", message="The content!", category_name="games"
115
+ )
116
+ ```
117
+
118
+ ```python
119
+ response = client.edit_thread(
120
+ thread_id="5bbb986af1deda001d33bc4b", message="My updated message!"
121
+ )
122
+ ```
123
+
124
+ ### Anonymous
125
+
126
+ ```python
127
+ response = client.search(query="search query")
128
+ ```
129
+
130
+ ```python
131
+ response = client.get_post(post_id="5bc876dd70a79c001dab7ebe")
132
+ ```
133
+
134
+ ```python
135
+ response = client.get_thread(thread_id="5bbb986af1deda001d33bc4b", page=3)
136
+ ```
137
+
138
+ ```python
139
+ response = client.get_games(page=5)
140
+ ```
141
+
142
+ ```python
143
+ response = client.get_other(page=7)
144
+ ```
145
+
146
+ ```python
147
+ response = client.get_editorials(page=2, post_type="review", sort="title")
148
+ ```
149
+
150
+ ```python
151
+ response = client.get_about()
152
+ ```
153
+
154
+ ```python
155
+ response = client.get_socials()
156
+ ```
157
+
158
+ ```python
159
+ response = client.get_total_thread_pages(thread_id="5bbb986af1deda001d33bc4b")
160
+ ```
161
+
162
+ ```python
163
+ response = client.get_total_category_pages(category="games")
164
+ ```
165
+
166
+ ## Development
167
+
168
+ Install [uv](https://docs.astral.sh/uv/getting-started/installation/) and run these commands from the checkout. The project defaults to Python 3.13.
169
+
170
+ ```sh
171
+ uv sync --locked
172
+ uv run ruff format --check .
173
+ uv run ruff check .
174
+ uv run ty check
175
+ uv run pytest --cov=loading_sdk --cov-branch
176
+ uv build
177
+ ```
178
+
179
+ The SDK lives in `src/loading_sdk/`. The build produces a wheel and source distribution in `dist/`. To build the documentation, run `uv run --locked --group docs sphinx-build -b html docs/source docs/build/html`.
180
+
181
+ ### Publishing a release
182
+
183
+ Set the project version with `uv version`, commit the updated metadata and lockfile, and push a matching `v<version>` tag. A prerelease tag such as `v0.5.0rc1` targets TestPyPI. After its index verification succeeds, a stable tag such as `v0.5.0` targets PyPI.
184
+
185
+ Configure a GitHub Trusted Publisher on each index for `hnrkcode/python-loading-sdk`, workflow `pypi.yaml`, and the corresponding GitHub environment (`testpypi` or `pypi`). The workflow checks the distributions, uploads the verified files, and installs both downloads from the target index.
@@ -0,0 +1,16 @@
1
+ loading_sdk/__init__.py,sha256=gnW3noxcprWMjQWbz6KqjQidwl0e6s20KBuL0reltrk,163
2
+ loading_sdk/_api.py,sha256=eRlya9G1zT1wyvzLJnntixjJjnWo3yrE-NILxmaYntA,8180
3
+ loading_sdk/_http.py,sha256=VMKL1Dssde4RKKbgVp023xUfWirvpRABZ5UZvvzjp6M,1561
4
+ loading_sdk/_parsing.py,sha256=yKfPkjUbJ_k8BRMw33GpDpWdrWFTpjqWunWv7xckU0U,2291
5
+ loading_sdk/async_api/__init__.py,sha256=-z2WyggntKRTeTI4MkU0IrXmACdXRmSpbXHea7oQ0pU,137
6
+ loading_sdk/async_api/client.py,sha256=-t0B2GHjZTwvqCLyYjUFCYvxP6LsFb5DB9J67P1Aic4,9237
7
+ loading_sdk/async_api/extractors.py,sha256=CV-I_lgDAl7DX8ZRXyR4ExUVzVynxxN1PRZ9mKgJiPM,1870
8
+ loading_sdk/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
+ loading_sdk/settings.py,sha256=Dr5-FyT7ycq7zklF2J83PC2oNzvgHo70pF5MwKKZvvk,384
10
+ loading_sdk/sync_api/__init__.py,sha256=jvGPT2E0Ce6Du9Od0jDChMgivnROjpb1xT3ReBt06jE,89
11
+ loading_sdk/sync_api/client.py,sha256=EXBR2qQdehTdB7Q4mklFbo24WPkk2ee5ZGw4vlEoDx4,8308
12
+ loading_sdk/sync_api/extractors.py,sha256=8RaNLv-NwB7dRXHccDMA8ZuI_xlvTG9wJ2R5l8dk27s,1788
13
+ python_loading_sdk-0.5.0.dist-info/licenses/LICENSE.md,sha256=XH9UVWZ2ydHJNRwW1mY2FkdMd_R1LcO6U25QZpCEWDQ,1065
14
+ python_loading_sdk-0.5.0.dist-info/WHEEL,sha256=G_-rrEibZr7OEM7zdH1dSO6s-VdjfnS5PA4CLAdI9Vw,80
15
+ python_loading_sdk-0.5.0.dist-info/METADATA,sha256=bNfJ2ugFF9ypruE8KtC7D2Si4hEJLthAmdCdIP8rMaw,6339
16
+ python_loading_sdk-0.5.0.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: poetry-core 1.1.0
2
+ Generator: uv 0.13.9
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
@@ -1,127 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: python-loading-sdk
3
- Version: 0.4.0
4
- Summary: Python api wrapper for loading.se
5
- Home-page: https://github.com/hnrkcode/python-loading-sdk
6
- License: MIT
7
- Author: hnrkcode
8
- Author-email: 44243358+hnrkcode@users.noreply.github.com
9
- Requires-Python: >=3.8,<4.0
10
- Classifier: License :: OSI Approved :: MIT License
11
- Classifier: Programming Language :: Python :: 3
12
- Classifier: Programming Language :: Python :: 3.8
13
- Classifier: Programming Language :: Python :: 3.9
14
- Classifier: Programming Language :: Python :: 3.10
15
- Requires-Dist: aiohttp (>=3.8.1,<4.0.0)
16
- Requires-Dist: beautifulsoup4 (>=4.11.1,<5.0.0)
17
- Requires-Dist: requests (>=2.28.1,<3.0.0)
18
- Project-URL: Repository, https://github.com/hnrkcode/python-loading-sdk
19
- Description-Content-Type: text/markdown
20
-
21
- # Python Loading SDK
22
-
23
- [![PyPI Version][pypi-image]][pypi-url]
24
- [![][versions-image]][versions-url]
25
- [![Build Status][build-image]][build-url]
26
-
27
- [pypi-image]: https://img.shields.io/pypi/v/python-loading-sdk
28
- [pypi-url]: https://pypi.org/project/python-loading-sdk/
29
- [versions-image]: https://img.shields.io/pypi/pyversions/python-loading-sdk
30
- [versions-url]: https://pypi.org/project/python-loading-sdk/
31
- [build-image]: https://github.com/hnrkcode/python-loading-sdk/actions/workflows/build.yaml/badge.svg
32
- [build-url]: https://github.com/hnrkcode/python-loading-sdk/actions/workflows/build.yaml
33
-
34
- ## Install
35
-
36
- ```
37
- pip install python-loading-sdk
38
- ```
39
-
40
- ## Usage
41
-
42
- Instantiate the client and optionally provide login credentials to be able to use methods that requires the user to be logged in.
43
-
44
- ```python
45
- from loading_sdk import LoadingApiClient
46
-
47
- client = LoadingApiClient(email="your@email.com", password="your_password")
48
-
49
- response = client.get_profile()
50
- ```
51
-
52
- It can also be used asyncrounously:
53
-
54
- ```python
55
- from loading_sdk import AsyncLoadingApiClient
56
-
57
- client = await AsyncLoadingApiClient(email="your@email.com", password="your_password")
58
-
59
- response = await client.get_profile()
60
- ```
61
-
62
- ## Examples
63
-
64
- ### Requires Auth
65
-
66
- ```python
67
- response = client.get_profile()
68
- ```
69
-
70
- ```python
71
- response = client.create_post(thread_id="5bbb986af1deda001d33bc4b", message="My message!")
72
- ```
73
-
74
- ```python
75
- response = client.edit_post(post_id="5bc876dd70a79c001dab7ebe", message="My updated message!")
76
- ```
77
-
78
- ```python
79
- response = client.create_thread(title="My title", message="The content!", category_name="games")
80
- ```
81
-
82
- ```python
83
- response = client.edit_thread(post_id="5bbb986af1deda001d33bc4b", message="My updated message!")
84
- ```
85
-
86
- ### Anonymous
87
-
88
- ```python
89
- response = client.search(query="search query")
90
- ```
91
-
92
- ```python
93
- response = client.get_post(post_id="5bc876dd70a79c001dab7ebe")
94
- ```
95
-
96
- ```python
97
- response = client.get_thread(thread_id="5bbb986af1deda001d33bc4b", page=3)
98
- ```
99
-
100
- ```python
101
- response = client.get_games(page=5)
102
- ```
103
-
104
- ```python
105
- response = client.get_other(page=7)
106
- ```
107
-
108
- ```python
109
- response = client.get_editorials(page=2, post_type="review", sort="title")
110
- ```
111
-
112
- ```python
113
- response = client.get_about()
114
- ```
115
-
116
- ```python
117
- response = client.get_socials()
118
- ```
119
-
120
- ```python
121
- response = client.get_total_thread_pages(thread_id="5bbb986af1deda001d33bc4b")
122
- ```
123
-
124
- ```python
125
- response = client.get_total_category_pages(category="games")
126
- ```
127
-
@@ -1,14 +0,0 @@
1
- loading_sdk/__init__.py,sha256=tA9N_QP2wdQqfUE6X6oltDx-Ca64TlamWz_DEG9V3Ig,163
2
- loading_sdk/async_api/__init__.py,sha256=-z2WyggntKRTeTI4MkU0IrXmACdXRmSpbXHea7oQ0pU,137
3
- loading_sdk/async_api/client.py,sha256=yt7XNLWsEeC22iJ6PffuTEyUD8YEp1Bk6ll3abduZuY,19160
4
- loading_sdk/async_api/extractors.py,sha256=qlhzVA5g9fUKP-rPc5ewLDEbnbU4hs4_yZgXmsZDz0I,4242
5
- loading_sdk/settings.py,sha256=Dr5-FyT7ycq7zklF2J83PC2oNzvgHo70pF5MwKKZvvk,384
6
- loading_sdk/sync_api/__init__.py,sha256=jvGPT2E0Ce6Du9Od0jDChMgivnROjpb1xT3ReBt06jE,89
7
- loading_sdk/sync_api/client.py,sha256=0rKW9qOLf0YpmwZL7j7hcQI3pAl5ReDPWDncMBLxmME,16917
8
- loading_sdk/sync_api/extractors.py,sha256=S1GsmDHKmyZTeIqesVvLp13D1T0YaiUVjPtbiFJnZO8,4103
9
- tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
10
- tests/test_api.py,sha256=7DoFMg93m0uvvWyuDZ3-N2RqoWD-5DsNKl-qvwkXrY4,144089
11
- python_loading_sdk-0.4.0.dist-info/LICENSE.md,sha256=XH9UVWZ2ydHJNRwW1mY2FkdMd_R1LcO6U25QZpCEWDQ,1065
12
- python_loading_sdk-0.4.0.dist-info/WHEEL,sha256=bbU3AyvhQ312rVm7zzRQjs6axI1UYWC3nmFA2E6FFSI,88
13
- python_loading_sdk-0.4.0.dist-info/METADATA,sha256=Asw9C0jGITAHam0vLfltoDlw_RtsRt_ac6B-y7Cw5AA,3162
14
- python_loading_sdk-0.4.0.dist-info/RECORD,,