elh-trends 0.1.0__tar.gz
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.
- elh_trends-0.1.0/PKG-INFO +46 -0
- elh_trends-0.1.0/README.md +30 -0
- elh_trends-0.1.0/elh_trends/__init__.py +163 -0
- elh_trends-0.1.0/elh_trends.egg-info/PKG-INFO +46 -0
- elh_trends-0.1.0/elh_trends.egg-info/SOURCES.txt +7 -0
- elh_trends-0.1.0/elh_trends.egg-info/dependency_links.txt +1 -0
- elh_trends-0.1.0/elh_trends.egg-info/top_level.txt +1 -0
- elh_trends-0.1.0/pyproject.toml +20 -0
- elh_trends-0.1.0/setup.cfg +4 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: elh-trends
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python client for the ELH Taiwan Trending Topics API: what Taiwan media are reporting on, ranked and charted.
|
|
5
|
+
Author-email: "Amano Hands Studio (ELH)" <support@easylifehub.net>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://api.easylifehub.net/trends/
|
|
8
|
+
Project-URL: Source, https://github.com/myappleknight/elh-trends
|
|
9
|
+
Project-URL: Documentation, https://api.easylifehub.net/trends/openapi.json
|
|
10
|
+
Keywords: taiwan,trending,news,trends,api,x402,mcp
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Natural Language :: Chinese (Traditional)
|
|
14
|
+
Requires-Python: >=3.8
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
|
|
17
|
+
# elh-trends (Python)
|
|
18
|
+
|
|
19
|
+
Client for the [ELH Taiwan Trending Topics API](https://api.easylifehub.net/trends/). No dependencies.
|
|
20
|
+
台灣熱門話題 API 的 Python 套件,不需要任何相依套件。
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pip install elh-trends
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
from elh_trends import TrendsClient
|
|
28
|
+
|
|
29
|
+
c = TrendsClient(api_key="trd_...") # or set ELH_TRENDS_KEY
|
|
30
|
+
for t in c.trends(period=7, limit=10)["items"]:
|
|
31
|
+
print(t["rank"], t["term"], t["sources"], t["articles"])
|
|
32
|
+
|
|
33
|
+
c.charts("music") # music, drama-tv, drama-film, ai-drama
|
|
34
|
+
c.topic_history("<topic id>", days=30)
|
|
35
|
+
c.classify("台積電法說會")
|
|
36
|
+
print(c.last_quota_remaining)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
No key yet? `TrendsClient().new_trial_key()` gives a free 3-day key with 30 requests.
|
|
40
|
+
|
|
41
|
+
## Pay per request (x402)
|
|
42
|
+
|
|
43
|
+
Without a key, paid endpoints answer HTTP 402 with x402 payment terms (USDC on Base, from 0.005 USDC).
|
|
44
|
+
Catch `PaymentRequired` to read them, or pass `x402_signer` to pay automatically. See `examples/x402/`.
|
|
45
|
+
|
|
46
|
+
Errors raise `TrendsError` with `status`, `code`, `message` and `request_id`.
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# elh-trends (Python)
|
|
2
|
+
|
|
3
|
+
Client for the [ELH Taiwan Trending Topics API](https://api.easylifehub.net/trends/). No dependencies.
|
|
4
|
+
台灣熱門話題 API 的 Python 套件,不需要任何相依套件。
|
|
5
|
+
|
|
6
|
+
```bash
|
|
7
|
+
pip install elh-trends
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
```python
|
|
11
|
+
from elh_trends import TrendsClient
|
|
12
|
+
|
|
13
|
+
c = TrendsClient(api_key="trd_...") # or set ELH_TRENDS_KEY
|
|
14
|
+
for t in c.trends(period=7, limit=10)["items"]:
|
|
15
|
+
print(t["rank"], t["term"], t["sources"], t["articles"])
|
|
16
|
+
|
|
17
|
+
c.charts("music") # music, drama-tv, drama-film, ai-drama
|
|
18
|
+
c.topic_history("<topic id>", days=30)
|
|
19
|
+
c.classify("台積電法說會")
|
|
20
|
+
print(c.last_quota_remaining)
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
No key yet? `TrendsClient().new_trial_key()` gives a free 3-day key with 30 requests.
|
|
24
|
+
|
|
25
|
+
## Pay per request (x402)
|
|
26
|
+
|
|
27
|
+
Without a key, paid endpoints answer HTTP 402 with x402 payment terms (USDC on Base, from 0.005 USDC).
|
|
28
|
+
Catch `PaymentRequired` to read them, or pass `x402_signer` to pay automatically. See `examples/x402/`.
|
|
29
|
+
|
|
30
|
+
Errors raise `TrendsError` with `status`, `code`, `message` and `request_id`.
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"""ELH Taiwan Trending Topics API client. Zero dependencies (standard library only)."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import base64
|
|
5
|
+
import json
|
|
6
|
+
import os
|
|
7
|
+
import urllib.error
|
|
8
|
+
import urllib.parse
|
|
9
|
+
import urllib.request
|
|
10
|
+
from typing import Any, Callable, Optional
|
|
11
|
+
|
|
12
|
+
__version__ = "0.1.0"
|
|
13
|
+
__all__ = ["TrendsClient", "TrendsError", "PaymentRequired"]
|
|
14
|
+
|
|
15
|
+
BASE_URL = "https://api.easylifehub.net/trends"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class TrendsError(Exception):
|
|
19
|
+
"""API error. `status` is the HTTP status, `code` the API error code, `body` the decoded JSON."""
|
|
20
|
+
|
|
21
|
+
def __init__(self, status: int, code: str, message: str, body: Any = None, request_id: str = ""):
|
|
22
|
+
super().__init__(f"{status} {code}: {message}")
|
|
23
|
+
self.status = status
|
|
24
|
+
self.code = code
|
|
25
|
+
self.message = message
|
|
26
|
+
self.body = body
|
|
27
|
+
self.request_id = request_id
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class PaymentRequired(TrendsError):
|
|
31
|
+
"""HTTP 402. `requirements` is the decoded x402 PAYMENT-REQUIRED object (accepts, resource, ...)."""
|
|
32
|
+
|
|
33
|
+
def __init__(self, requirements: Any, body: Any = None, request_id: str = ""):
|
|
34
|
+
super().__init__(402, "payment_required", "This request needs an API key or an x402 payment.", body, request_id)
|
|
35
|
+
self.requirements = requirements
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class TrendsClient:
|
|
39
|
+
"""
|
|
40
|
+
client = TrendsClient(api_key="trd_...") # or set ELH_TRENDS_KEY
|
|
41
|
+
client.trends(period=7, limit=20)
|
|
42
|
+
|
|
43
|
+
Without an API key, paid endpoints answer 402. Pass `x402_signer`, a function that takes the
|
|
44
|
+
decoded PAYMENT-REQUIRED object and returns the base64 PAYMENT-SIGNATURE header, to pay per
|
|
45
|
+
request in USDC on Base (see examples/x402).
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
def __init__(
|
|
49
|
+
self,
|
|
50
|
+
api_key: Optional[str] = None,
|
|
51
|
+
base_url: str = BASE_URL,
|
|
52
|
+
lang: Optional[str] = None,
|
|
53
|
+
timeout: float = 30.0,
|
|
54
|
+
x402_signer: Optional[Callable[[Any], str]] = None,
|
|
55
|
+
):
|
|
56
|
+
self.api_key = api_key if api_key is not None else os.environ.get("ELH_TRENDS_KEY", "")
|
|
57
|
+
self.base_url = base_url.rstrip("/")
|
|
58
|
+
self.lang = lang
|
|
59
|
+
self.timeout = timeout
|
|
60
|
+
self.x402_signer = x402_signer
|
|
61
|
+
self.last_quota_remaining: Optional[int] = None
|
|
62
|
+
self.last_request_id = ""
|
|
63
|
+
self.last_payment_response: Optional[Any] = None
|
|
64
|
+
|
|
65
|
+
# ---------- endpoints ----------
|
|
66
|
+
def trends(self, **params: Any) -> Any:
|
|
67
|
+
"""Ranking. Params: date, period (1/3/7/15/20), category, type, limit, sort (hot/new/rising), lang."""
|
|
68
|
+
return self._get("/v1/trends", params)
|
|
69
|
+
|
|
70
|
+
def topic_history(self, topic_id: str, days: Optional[int] = None, to: Optional[str] = None) -> Any:
|
|
71
|
+
return self._get(f"/v1/topics/{urllib.parse.quote(str(topic_id))}/history", {"days": days, "to": to})
|
|
72
|
+
|
|
73
|
+
def related(self, topic_id: str, **params: Any) -> Any:
|
|
74
|
+
"""Params: from, to, compare_from, compare_to, limit."""
|
|
75
|
+
return self._get(f"/v1/topics/{urllib.parse.quote(str(topic_id))}/related", params)
|
|
76
|
+
|
|
77
|
+
def search(self, q: str) -> Any:
|
|
78
|
+
return self._get("/v1/topics/search", {"q": q})
|
|
79
|
+
|
|
80
|
+
def categories(self) -> Any:
|
|
81
|
+
return self._get("/v1/categories", {})
|
|
82
|
+
|
|
83
|
+
def classify(self, text: Optional[str] = None, items: Optional[list] = None, **fields: Any) -> Any:
|
|
84
|
+
body = {"items": items} if items is not None else {"text": text, **fields}
|
|
85
|
+
return self._request("POST", "/v1/classify", {}, body)
|
|
86
|
+
|
|
87
|
+
def overview(self, **params: Any) -> Any:
|
|
88
|
+
"""Dashboard bundle. Params: date, period, sort."""
|
|
89
|
+
return self._get("/v1/overview", params)
|
|
90
|
+
|
|
91
|
+
def charts(self, chart: Optional[str] = None, at: Optional[str] = None) -> Any:
|
|
92
|
+
"""chart: music, drama-tv, drama-film, ai-drama. Without chart, lists the charts."""
|
|
93
|
+
if chart is None:
|
|
94
|
+
return self._get("/v1/charts", {})
|
|
95
|
+
return self._get(f"/v1/charts/{urllib.parse.quote(chart)}", {"at": at})
|
|
96
|
+
|
|
97
|
+
def me(self) -> Any:
|
|
98
|
+
return self._get("/v1/me", {})
|
|
99
|
+
|
|
100
|
+
def pricing(self) -> Any:
|
|
101
|
+
return self._get("/v1/x402", {})
|
|
102
|
+
|
|
103
|
+
def status(self) -> Any:
|
|
104
|
+
return self._get("/v1/status", {})
|
|
105
|
+
|
|
106
|
+
def new_trial_key(self) -> Any:
|
|
107
|
+
"""Free 3-day trial key (30 requests). Shown once; store it."""
|
|
108
|
+
return self._request("POST", "/v1/keys", {}, {})
|
|
109
|
+
|
|
110
|
+
# ---------- transport ----------
|
|
111
|
+
def _get(self, path: str, params: dict) -> Any:
|
|
112
|
+
return self._request("GET", path, params, None)
|
|
113
|
+
|
|
114
|
+
def _request(self, method: str, path: str, params: dict, body: Any, payment: Optional[str] = None) -> Any:
|
|
115
|
+
q = {k: v for k, v in params.items() if v is not None}
|
|
116
|
+
if self.lang and "lang" not in q:
|
|
117
|
+
q["lang"] = self.lang
|
|
118
|
+
url = self.base_url + path + ("?" + urllib.parse.urlencode(q) if q else "")
|
|
119
|
+
headers = {"accept": "application/json", "user-agent": f"elh-trends-python/{__version__}"}
|
|
120
|
+
if self.api_key:
|
|
121
|
+
headers["x-api-key"] = self.api_key
|
|
122
|
+
if payment:
|
|
123
|
+
headers["payment-signature"] = payment
|
|
124
|
+
data = None
|
|
125
|
+
if body is not None:
|
|
126
|
+
data = json.dumps(body).encode("utf-8")
|
|
127
|
+
headers["content-type"] = "application/json"
|
|
128
|
+
req = urllib.request.Request(url, data=data, headers=headers, method=method)
|
|
129
|
+
try:
|
|
130
|
+
with urllib.request.urlopen(req, timeout=self.timeout) as r:
|
|
131
|
+
self._remember(r.headers)
|
|
132
|
+
return json.loads(r.read().decode("utf-8"))
|
|
133
|
+
except urllib.error.HTTPError as e:
|
|
134
|
+
self._remember(e.headers)
|
|
135
|
+
raw = e.read().decode("utf-8", "replace")
|
|
136
|
+
try:
|
|
137
|
+
j = json.loads(raw)
|
|
138
|
+
except ValueError:
|
|
139
|
+
j = {"error": {"code": "http_error", "message": raw[:200]}}
|
|
140
|
+
if e.code == 402:
|
|
141
|
+
reqs = _decode(e.headers.get("payment-required")) or j
|
|
142
|
+
if self.x402_signer and not payment:
|
|
143
|
+
return self._request(method, path, params, body, self.x402_signer(reqs))
|
|
144
|
+
raise PaymentRequired(reqs, j, self.last_request_id) from None
|
|
145
|
+
err = j.get("error") if isinstance(j, dict) else None
|
|
146
|
+
if not isinstance(err, dict):
|
|
147
|
+
err = {"code": str(err or "http_error"), "message": str(j.get("message", "")) if isinstance(j, dict) else ""}
|
|
148
|
+
raise TrendsError(e.code, err.get("code", "http_error"), err.get("message", ""), j, self.last_request_id) from None
|
|
149
|
+
|
|
150
|
+
def _remember(self, h: Any) -> None:
|
|
151
|
+
self.last_request_id = h.get("x-request-id", "") or ""
|
|
152
|
+
q = h.get("x-quota-remaining")
|
|
153
|
+
self.last_quota_remaining = int(q) if q and q.lstrip("-").isdigit() else None
|
|
154
|
+
self.last_payment_response = _decode(h.get("payment-response"))
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def _decode(v: Optional[str]) -> Any:
|
|
158
|
+
if not v:
|
|
159
|
+
return None
|
|
160
|
+
try:
|
|
161
|
+
return json.loads(base64.b64decode(v).decode("utf-8"))
|
|
162
|
+
except Exception:
|
|
163
|
+
return None
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: elh-trends
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python client for the ELH Taiwan Trending Topics API: what Taiwan media are reporting on, ranked and charted.
|
|
5
|
+
Author-email: "Amano Hands Studio (ELH)" <support@easylifehub.net>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://api.easylifehub.net/trends/
|
|
8
|
+
Project-URL: Source, https://github.com/myappleknight/elh-trends
|
|
9
|
+
Project-URL: Documentation, https://api.easylifehub.net/trends/openapi.json
|
|
10
|
+
Keywords: taiwan,trending,news,trends,api,x402,mcp
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Natural Language :: Chinese (Traditional)
|
|
14
|
+
Requires-Python: >=3.8
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
|
|
17
|
+
# elh-trends (Python)
|
|
18
|
+
|
|
19
|
+
Client for the [ELH Taiwan Trending Topics API](https://api.easylifehub.net/trends/). No dependencies.
|
|
20
|
+
台灣熱門話題 API 的 Python 套件,不需要任何相依套件。
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pip install elh-trends
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
from elh_trends import TrendsClient
|
|
28
|
+
|
|
29
|
+
c = TrendsClient(api_key="trd_...") # or set ELH_TRENDS_KEY
|
|
30
|
+
for t in c.trends(period=7, limit=10)["items"]:
|
|
31
|
+
print(t["rank"], t["term"], t["sources"], t["articles"])
|
|
32
|
+
|
|
33
|
+
c.charts("music") # music, drama-tv, drama-film, ai-drama
|
|
34
|
+
c.topic_history("<topic id>", days=30)
|
|
35
|
+
c.classify("台積電法說會")
|
|
36
|
+
print(c.last_quota_remaining)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
No key yet? `TrendsClient().new_trial_key()` gives a free 3-day key with 30 requests.
|
|
40
|
+
|
|
41
|
+
## Pay per request (x402)
|
|
42
|
+
|
|
43
|
+
Without a key, paid endpoints answer HTTP 402 with x402 payment terms (USDC on Base, from 0.005 USDC).
|
|
44
|
+
Catch `PaymentRequired` to read them, or pass `x402_signer` to pay automatically. See `examples/x402/`.
|
|
45
|
+
|
|
46
|
+
Errors raise `TrendsError` with `status`, `code`, `message` and `request_id`.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
elh_trends
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "elh-trends"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Python client for the ELH Taiwan Trending Topics API: what Taiwan media are reporting on, ranked and charted."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.8"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [{ name = "Amano Hands Studio (ELH)", email = "support@easylifehub.net" }]
|
|
13
|
+
keywords = ["taiwan", "trending", "news", "trends", "api", "x402", "mcp"]
|
|
14
|
+
classifiers = ["Programming Language :: Python :: 3", "License :: OSI Approved :: MIT License", "Natural Language :: Chinese (Traditional)"]
|
|
15
|
+
dependencies = []
|
|
16
|
+
|
|
17
|
+
[project.urls]
|
|
18
|
+
Homepage = "https://api.easylifehub.net/trends/"
|
|
19
|
+
Source = "https://github.com/myappleknight/elh-trends"
|
|
20
|
+
Documentation = "https://api.easylifehub.net/trends/openapi.json"
|