Skip to content

Commit eee318e

Browse files
author
Anonymous Committer
committed
feat: add toutiao.search_v1 with API integration, client support, and test
1 parent 68d537c commit eee318e

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

justoneapi/apis/toutiao.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from justoneapi.apis import request_util
2+
from justoneapi.log import logger
3+
4+
5+
class ToutiaoAPI:
6+
def __init__(self, token: str, base_url: str):
7+
self.token = token
8+
self.base_url = base_url
9+
10+
def search_v1(self, keyword: str, page: int, search_id: str = None):
11+
url = f"{self.base_url}/api/toutiao/search/v1"
12+
params = {
13+
"token": self.token,
14+
"keyword": keyword,
15+
"page": page,
16+
}
17+
if search_id:
18+
params["searchId"] = search_id
19+
20+
has_next_page = False
21+
result, data, message = request_util.get_request_page(url, params)
22+
try:
23+
if data:
24+
if data.get("search_id"):
25+
has_next_page = True
26+
except Exception as e:
27+
logger.warning(f"Pagination parse error at {url}. Contact us to fix it.")
28+
return result, data, message, has_next_page

justoneapi/client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from justoneapi.apis.search import SearchAPI
77
from justoneapi.apis.taobao import TaobaoAPI
88
from justoneapi.apis.tiktok import TikTokAPI
9+
from justoneapi.apis.toutiao import ToutiaoAPI
910
from justoneapi.apis.user import UserAPI
1011
from justoneapi.apis.weibo import WeiboAPI
1112
from justoneapi.apis.xiaohongshu import XiaohongshuAPI
@@ -32,3 +33,4 @@ def __init__(self, token: str, env: str = "cn"):
3233
self.search = SearchAPI(self.token, self.base_url)
3334
self.jd = JdAPI(self.token, self.base_url)
3435
self.tiktok = TikTokAPI(self.token, self.base_url)
36+
self.toutiao = ToutiaoAPI(self.token, self.base_url)

justoneapi/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
BASE_URL_CN = "http://47.117.133.51:30015"
1+
BASE_URL_CN = "http://localhost:8008"
22
BASE_URL_GLOBAL = "https://api.justoneapi.com"

tests/test_toutiao.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import json
2+
import os
3+
from unittest import TestCase
4+
5+
from justoneapi.client import JustOneAPIClient
6+
7+
8+
class TestToutiaoAPI(TestCase):
9+
client = JustOneAPIClient(token=os.environ.get("JUSTONEAPI_TOKEN"))
10+
11+
def test_search_v1(self):
12+
result, data, message, has_next_page = self.client.toutiao.search_v1(keyword="个人公积金", page=1)
13+
if result:
14+
print(json.dumps(data, ensure_ascii=False))

0 commit comments

Comments
 (0)