langchain-tikdataapi 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.
- langchain_tikdataapi-0.1.0/.gitignore +9 -0
- langchain_tikdataapi-0.1.0/LICENSE +9 -0
- langchain_tikdataapi-0.1.0/PKG-INFO +169 -0
- langchain_tikdataapi-0.1.0/README.md +147 -0
- langchain_tikdataapi-0.1.0/pyproject.toml +45 -0
- langchain_tikdataapi-0.1.0/src/langchain_tikdataapi/__init__.py +81 -0
- langchain_tikdataapi-0.1.0/src/langchain_tikdataapi/client.py +149 -0
- langchain_tikdataapi-0.1.0/src/langchain_tikdataapi/toolkit.py +64 -0
- langchain_tikdataapi-0.1.0/src/langchain_tikdataapi/tools.py +780 -0
- langchain_tikdataapi-0.1.0/tests/test_tools.py +537 -0
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 tikdataapi.com
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: langchain-tikdataapi
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: LangChain tools for tikdataapi.com: read-only public TikTok profiles, videos, comments, replies, hashtags, sounds, playlists, reposts, related and trending videos, transcripts, live state, video, account, sound, live and hashtag search and follower lists.
|
|
5
|
+
Project-URL: Homepage, https://tikdataapi.com
|
|
6
|
+
Project-URL: Documentation, https://tikdataapi.com/docs
|
|
7
|
+
Project-URL: API reference, https://tikdataapi.com/reference
|
|
8
|
+
Project-URL: Source code, https://github.com/tikdataapi/tikdataapi
|
|
9
|
+
Project-URL: Issue tracker, https://github.com/tikdataapi/tikdataapi/issues
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: agents,comments,followers,hashtags,langchain,langgraph,live,llm,mcp,music,playlists,search,tikdataapi,tiktok,tiktok-api,tools,transcripts,trending,videos
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
17
|
+
Requires-Python: >=3.10
|
|
18
|
+
Requires-Dist: httpx<0.29.0,>=0.27.0
|
|
19
|
+
Requires-Dist: langchain-core<2.0.0,>=0.3.0
|
|
20
|
+
Requires-Dist: pydantic<3.0.0,>=2.7.0
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
|
|
23
|
+
# langchain-tikdataapi
|
|
24
|
+
|
|
25
|
+
LangChain tools for [tikdataapi.com](https://tikdataapi.com): read-only public TikTok data.
|
|
26
|
+
Profiles, the videos of an account, its reposts, playlists, followers and following, videos, comments,
|
|
27
|
+
replies, transcripts, related and trending videos, hashtags, sounds and their videos, the videos of a
|
|
28
|
+
playlist, whether an account is live, and video, account, sound, live and hashtag search.
|
|
29
|
+
|
|
30
|
+
Read-only by design. No posting, no likes, no direct messages, no private data, and no TikTok login: the
|
|
31
|
+
tools read what a logged-out visitor of tiktok.com sees. That includes the general ("Top") search of TikTok,
|
|
32
|
+
videos only, its account search (the "Users" tab), its sound and live search (the "Sounds" and "LIVE" tabs),
|
|
33
|
+
and the follower and following lists of an account. TikTok gives a logged-out visitor no hashtag search, so
|
|
34
|
+
hashtag search ranks the hashtags of the videos of the general search. It does not include the filters and
|
|
35
|
+
sorting of the Videos tab of search (TikTok ignores them for a logged-out visitor), liked videos, video files or
|
|
36
|
+
the streams of live rooms.
|
|
37
|
+
Not affiliated with TikTok or ByteDance.
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pip install langchain-tikdataapi
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Use
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
from langchain_tikdataapi import TikdataapiToolkit
|
|
47
|
+
|
|
48
|
+
# Reads TIKDATAAPI_KEY from the environment.
|
|
49
|
+
tools = TikdataapiToolkit.from_api_key().get_tools()
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
With an agent:
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
from langchain.agents import create_agent
|
|
56
|
+
from langchain_tikdataapi import TikdataapiToolkit
|
|
57
|
+
|
|
58
|
+
agent = create_agent(
|
|
59
|
+
"anthropic:claude-opus-5",
|
|
60
|
+
tools=TikdataapiToolkit.from_api_key(max_results=25).get_tools(),
|
|
61
|
+
)
|
|
62
|
+
result = agent.invoke({"messages": [{"role": "user", "content": "What did @nba post this week, and how did people react?"}]})
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
One tool on its own:
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
from langchain_tikdataapi import GetUserVideos, TikdataapiClient
|
|
69
|
+
|
|
70
|
+
videos = GetUserVideos(client=TikdataapiClient())
|
|
71
|
+
print(videos.invoke({"user": "nba", "count": 10}))
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Tools
|
|
75
|
+
|
|
76
|
+
The names are the tools of the hosted MCP server with a `tiktok_` prefix, so they stay unique next to the
|
|
77
|
+
tools of other toolkits.
|
|
78
|
+
|
|
79
|
+
| Tool | Reads | Credits |
|
|
80
|
+
|---|---|---|
|
|
81
|
+
| `tiktok_get_user` | One profile, by username or `sec_uid` | 1 |
|
|
82
|
+
| `tiktok_get_users` | Up to 100 profiles in one call, by username | 1 per profile |
|
|
83
|
+
| `tiktok_get_user_videos` | The videos of an account: pinned first, then newest first | 1 per video |
|
|
84
|
+
| `tiktok_get_users_videos` | The newest 1 to 16 videos of up to 20 accounts in one call | 1 per video |
|
|
85
|
+
| `tiktok_get_user_followers` | The accounts that follow an account, newest first | 1 per profile |
|
|
86
|
+
| `tiktok_get_user_following` | The accounts an account follows, newest first | 1 per profile |
|
|
87
|
+
| `tiktok_get_user_reposts` | The videos an account reposted | 1 per video |
|
|
88
|
+
| `tiktok_get_user_playlists` | The playlists an account made | 1 per playlist |
|
|
89
|
+
| `tiktok_get_user_live` | Whether an account is live now, with its room | 1, live or not |
|
|
90
|
+
| `tiktok_get_video` | One video, with its author, sound and counts | 1 |
|
|
91
|
+
| `tiktok_get_videos` | Up to 100 videos in one call | 1 per video |
|
|
92
|
+
| `tiktok_get_related_videos` | The videos TikTok shows next to a video: one page of up to 16, no cursor | 1 per video |
|
|
93
|
+
| `tiktok_get_trending_videos` | The explore feed of a logged-out visitor: up to 50, no cursor | 1 per video |
|
|
94
|
+
| `tiktok_get_video_transcript` | The transcript of a video from its subtitles: timed segments and text | 1 |
|
|
95
|
+
| `tiktok_get_comments` | The comments of a video: pinned first, then the most relevant | 1 per comment |
|
|
96
|
+
| `tiktok_get_comment_replies` | The replies to one comment, oldest first | 1 per reply |
|
|
97
|
+
| `tiktok_get_hashtag` | One hashtag with its counts of videos and views | 1 |
|
|
98
|
+
| `tiktok_get_hashtag_videos` | The videos of a hashtag, in the order of the hashtag page | 1 per video |
|
|
99
|
+
| `tiktok_get_music` | One sound (music) by id, with the number of videos that use it | 1 |
|
|
100
|
+
| `tiktok_get_music_videos` | The videos that use a sound, in the order of the sound page | 1 per video |
|
|
101
|
+
| `tiktok_get_playlist_videos` | The videos of a playlist, in its order | 1 per video |
|
|
102
|
+
| `tiktok_search_videos` | The videos that the general ("Top") search of TikTok finds for a query | 1 per video |
|
|
103
|
+
| `tiktok_search_users` | The accounts that the account search of TikTok finds for a query, as short profiles | 1 per profile |
|
|
104
|
+
| `tiktok_search_music` | The sounds that the sound search of TikTok finds for a query, as short sounds | 1 per sound |
|
|
105
|
+
| `tiktok_search_live` | The live rooms that the live search of TikTok finds for a query | 1 per room |
|
|
106
|
+
| `tiktok_search_hashtags` | The hashtags of the videos of the general search for a query, most used first: up to 30, no cursor | 1 per hashtag |
|
|
107
|
+
| `tiktok_get_balance` | Credits left and rate limit | free |
|
|
108
|
+
|
|
109
|
+
Empty results and failed requests cost nothing: a private account, a video with comments turned off, a deleted
|
|
110
|
+
video, a search with no result. A repeated read inside the cache window costs half.
|
|
111
|
+
Every response carries `credits_charged` and `balance_remaining`, so the model can see the budget shrink
|
|
112
|
+
and stop on its own.
|
|
113
|
+
|
|
114
|
+
A `user` is a username, with or without the `@`, or a `sec_uid`. Video and comment ids are strings of digits.
|
|
115
|
+
In an entry of a follower or following list the counts may be rounded; `tiktok_get_user` gives the exact ones.
|
|
116
|
+
An entry of `tiktok_search_users` is a short profile: the follower count is rounded, and there is no following
|
|
117
|
+
count, video count or private flag; `tiktok_get_user` or `tiktok_get_users` gives the full profile.
|
|
118
|
+
A sound and a playlist are read by their numeric id (up to 25 digits): the number at the end of the address
|
|
119
|
+
of the sound, `music.id` of a video, or the `id` of an entry of `tiktok_get_user_playlists`. The count of videos
|
|
120
|
+
of a sound is rounded by TikTok. `tiktok_get_related_videos` and `tiktok_get_trending_videos` have no next page.
|
|
121
|
+
`tiktok_get_user_live` costs 1 credit whether the account is live or not; when it is not, `live` is false and
|
|
122
|
+
the room fields are absent. `tiktok_get_video_transcript` takes an optional `lang` (`eng-US` or `eng`); a video
|
|
123
|
+
with no subtitles is a `not_found` with reason `unavailable`, free.
|
|
124
|
+
`tiktok_search_hashtags` reads about 24 videos of the general search and ranks their hashtags by `result_videos`
|
|
125
|
+
(how many of those videos use the hashtag), then `result_plays` (the sum of their plays). These counts are of
|
|
126
|
+
those videos only, not the totals of TikTok: `tiktok_get_hashtag` gives those. `count` is 1 to 30, default 10, and
|
|
127
|
+
there is no next page.
|
|
128
|
+
Not known yet: how deep TikTok lets a follower list or a search be read, and what it gives for a private
|
|
129
|
+
account or a hidden following list.
|
|
130
|
+
|
|
131
|
+
## Spending guards
|
|
132
|
+
|
|
133
|
+
`max_results` caps the page size of every paging tool, and the videos for each account in
|
|
134
|
+
`tiktok_get_users_videos`, so one agent step cannot ask for more than you allow:
|
|
135
|
+
|
|
136
|
+
```python
|
|
137
|
+
toolkit = TikdataapiToolkit.from_api_key(max_results=25)
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
Narrow the surface with `include` or `exclude`:
|
|
141
|
+
|
|
142
|
+
```python
|
|
143
|
+
toolkit = TikdataapiToolkit.from_api_key(include=["tiktok_get_user", "tiktok_get_user_videos", "tiktok_get_balance"])
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
## Errors
|
|
147
|
+
|
|
148
|
+
A failure the model can act on — no credits, not found (with a `reason`: nonexistent, banned, private,
|
|
149
|
+
removed, unavailable), a bad argument — comes back as JSON with `error.code`, so the model can change plan.
|
|
150
|
+
None of those are charged.
|
|
151
|
+
A missing or revoked key raises `TikdataapiAuthError`, because no model can repair it.
|
|
152
|
+
Rate limits and upstream faults raise `TikdataapiError`, so your framework's retry policy sees them.
|
|
153
|
+
|
|
154
|
+
## MCP instead
|
|
155
|
+
|
|
156
|
+
An agent that speaks MCP needs no package. Connect the hosted server:
|
|
157
|
+
|
|
158
|
+
```bash
|
|
159
|
+
claude mcp add --transport http tikdataapi https://api.tikdataapi.com/mcp
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
It signs in with OAuth on first use. Its registry name is `com.tikdataapi/tikdataapi`.
|
|
163
|
+
|
|
164
|
+
## Keys and pricing
|
|
165
|
+
|
|
166
|
+
Get a key at [tikdataapi.com/dashboard](https://tikdataapi.com/dashboard). 1,000 credits to start, no card.
|
|
167
|
+
Prepaid packs from $10; credits never expire. Live status: [tikdataapi.com/status](https://tikdataapi.com/status).
|
|
168
|
+
|
|
169
|
+
MIT licensed.
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# langchain-tikdataapi
|
|
2
|
+
|
|
3
|
+
LangChain tools for [tikdataapi.com](https://tikdataapi.com): read-only public TikTok data.
|
|
4
|
+
Profiles, the videos of an account, its reposts, playlists, followers and following, videos, comments,
|
|
5
|
+
replies, transcripts, related and trending videos, hashtags, sounds and their videos, the videos of a
|
|
6
|
+
playlist, whether an account is live, and video, account, sound, live and hashtag search.
|
|
7
|
+
|
|
8
|
+
Read-only by design. No posting, no likes, no direct messages, no private data, and no TikTok login: the
|
|
9
|
+
tools read what a logged-out visitor of tiktok.com sees. That includes the general ("Top") search of TikTok,
|
|
10
|
+
videos only, its account search (the "Users" tab), its sound and live search (the "Sounds" and "LIVE" tabs),
|
|
11
|
+
and the follower and following lists of an account. TikTok gives a logged-out visitor no hashtag search, so
|
|
12
|
+
hashtag search ranks the hashtags of the videos of the general search. It does not include the filters and
|
|
13
|
+
sorting of the Videos tab of search (TikTok ignores them for a logged-out visitor), liked videos, video files or
|
|
14
|
+
the streams of live rooms.
|
|
15
|
+
Not affiliated with TikTok or ByteDance.
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pip install langchain-tikdataapi
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Use
|
|
22
|
+
|
|
23
|
+
```python
|
|
24
|
+
from langchain_tikdataapi import TikdataapiToolkit
|
|
25
|
+
|
|
26
|
+
# Reads TIKDATAAPI_KEY from the environment.
|
|
27
|
+
tools = TikdataapiToolkit.from_api_key().get_tools()
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
With an agent:
|
|
31
|
+
|
|
32
|
+
```python
|
|
33
|
+
from langchain.agents import create_agent
|
|
34
|
+
from langchain_tikdataapi import TikdataapiToolkit
|
|
35
|
+
|
|
36
|
+
agent = create_agent(
|
|
37
|
+
"anthropic:claude-opus-5",
|
|
38
|
+
tools=TikdataapiToolkit.from_api_key(max_results=25).get_tools(),
|
|
39
|
+
)
|
|
40
|
+
result = agent.invoke({"messages": [{"role": "user", "content": "What did @nba post this week, and how did people react?"}]})
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
One tool on its own:
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
from langchain_tikdataapi import GetUserVideos, TikdataapiClient
|
|
47
|
+
|
|
48
|
+
videos = GetUserVideos(client=TikdataapiClient())
|
|
49
|
+
print(videos.invoke({"user": "nba", "count": 10}))
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Tools
|
|
53
|
+
|
|
54
|
+
The names are the tools of the hosted MCP server with a `tiktok_` prefix, so they stay unique next to the
|
|
55
|
+
tools of other toolkits.
|
|
56
|
+
|
|
57
|
+
| Tool | Reads | Credits |
|
|
58
|
+
|---|---|---|
|
|
59
|
+
| `tiktok_get_user` | One profile, by username or `sec_uid` | 1 |
|
|
60
|
+
| `tiktok_get_users` | Up to 100 profiles in one call, by username | 1 per profile |
|
|
61
|
+
| `tiktok_get_user_videos` | The videos of an account: pinned first, then newest first | 1 per video |
|
|
62
|
+
| `tiktok_get_users_videos` | The newest 1 to 16 videos of up to 20 accounts in one call | 1 per video |
|
|
63
|
+
| `tiktok_get_user_followers` | The accounts that follow an account, newest first | 1 per profile |
|
|
64
|
+
| `tiktok_get_user_following` | The accounts an account follows, newest first | 1 per profile |
|
|
65
|
+
| `tiktok_get_user_reposts` | The videos an account reposted | 1 per video |
|
|
66
|
+
| `tiktok_get_user_playlists` | The playlists an account made | 1 per playlist |
|
|
67
|
+
| `tiktok_get_user_live` | Whether an account is live now, with its room | 1, live or not |
|
|
68
|
+
| `tiktok_get_video` | One video, with its author, sound and counts | 1 |
|
|
69
|
+
| `tiktok_get_videos` | Up to 100 videos in one call | 1 per video |
|
|
70
|
+
| `tiktok_get_related_videos` | The videos TikTok shows next to a video: one page of up to 16, no cursor | 1 per video |
|
|
71
|
+
| `tiktok_get_trending_videos` | The explore feed of a logged-out visitor: up to 50, no cursor | 1 per video |
|
|
72
|
+
| `tiktok_get_video_transcript` | The transcript of a video from its subtitles: timed segments and text | 1 |
|
|
73
|
+
| `tiktok_get_comments` | The comments of a video: pinned first, then the most relevant | 1 per comment |
|
|
74
|
+
| `tiktok_get_comment_replies` | The replies to one comment, oldest first | 1 per reply |
|
|
75
|
+
| `tiktok_get_hashtag` | One hashtag with its counts of videos and views | 1 |
|
|
76
|
+
| `tiktok_get_hashtag_videos` | The videos of a hashtag, in the order of the hashtag page | 1 per video |
|
|
77
|
+
| `tiktok_get_music` | One sound (music) by id, with the number of videos that use it | 1 |
|
|
78
|
+
| `tiktok_get_music_videos` | The videos that use a sound, in the order of the sound page | 1 per video |
|
|
79
|
+
| `tiktok_get_playlist_videos` | The videos of a playlist, in its order | 1 per video |
|
|
80
|
+
| `tiktok_search_videos` | The videos that the general ("Top") search of TikTok finds for a query | 1 per video |
|
|
81
|
+
| `tiktok_search_users` | The accounts that the account search of TikTok finds for a query, as short profiles | 1 per profile |
|
|
82
|
+
| `tiktok_search_music` | The sounds that the sound search of TikTok finds for a query, as short sounds | 1 per sound |
|
|
83
|
+
| `tiktok_search_live` | The live rooms that the live search of TikTok finds for a query | 1 per room |
|
|
84
|
+
| `tiktok_search_hashtags` | The hashtags of the videos of the general search for a query, most used first: up to 30, no cursor | 1 per hashtag |
|
|
85
|
+
| `tiktok_get_balance` | Credits left and rate limit | free |
|
|
86
|
+
|
|
87
|
+
Empty results and failed requests cost nothing: a private account, a video with comments turned off, a deleted
|
|
88
|
+
video, a search with no result. A repeated read inside the cache window costs half.
|
|
89
|
+
Every response carries `credits_charged` and `balance_remaining`, so the model can see the budget shrink
|
|
90
|
+
and stop on its own.
|
|
91
|
+
|
|
92
|
+
A `user` is a username, with or without the `@`, or a `sec_uid`. Video and comment ids are strings of digits.
|
|
93
|
+
In an entry of a follower or following list the counts may be rounded; `tiktok_get_user` gives the exact ones.
|
|
94
|
+
An entry of `tiktok_search_users` is a short profile: the follower count is rounded, and there is no following
|
|
95
|
+
count, video count or private flag; `tiktok_get_user` or `tiktok_get_users` gives the full profile.
|
|
96
|
+
A sound and a playlist are read by their numeric id (up to 25 digits): the number at the end of the address
|
|
97
|
+
of the sound, `music.id` of a video, or the `id` of an entry of `tiktok_get_user_playlists`. The count of videos
|
|
98
|
+
of a sound is rounded by TikTok. `tiktok_get_related_videos` and `tiktok_get_trending_videos` have no next page.
|
|
99
|
+
`tiktok_get_user_live` costs 1 credit whether the account is live or not; when it is not, `live` is false and
|
|
100
|
+
the room fields are absent. `tiktok_get_video_transcript` takes an optional `lang` (`eng-US` or `eng`); a video
|
|
101
|
+
with no subtitles is a `not_found` with reason `unavailable`, free.
|
|
102
|
+
`tiktok_search_hashtags` reads about 24 videos of the general search and ranks their hashtags by `result_videos`
|
|
103
|
+
(how many of those videos use the hashtag), then `result_plays` (the sum of their plays). These counts are of
|
|
104
|
+
those videos only, not the totals of TikTok: `tiktok_get_hashtag` gives those. `count` is 1 to 30, default 10, and
|
|
105
|
+
there is no next page.
|
|
106
|
+
Not known yet: how deep TikTok lets a follower list or a search be read, and what it gives for a private
|
|
107
|
+
account or a hidden following list.
|
|
108
|
+
|
|
109
|
+
## Spending guards
|
|
110
|
+
|
|
111
|
+
`max_results` caps the page size of every paging tool, and the videos for each account in
|
|
112
|
+
`tiktok_get_users_videos`, so one agent step cannot ask for more than you allow:
|
|
113
|
+
|
|
114
|
+
```python
|
|
115
|
+
toolkit = TikdataapiToolkit.from_api_key(max_results=25)
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
Narrow the surface with `include` or `exclude`:
|
|
119
|
+
|
|
120
|
+
```python
|
|
121
|
+
toolkit = TikdataapiToolkit.from_api_key(include=["tiktok_get_user", "tiktok_get_user_videos", "tiktok_get_balance"])
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Errors
|
|
125
|
+
|
|
126
|
+
A failure the model can act on — no credits, not found (with a `reason`: nonexistent, banned, private,
|
|
127
|
+
removed, unavailable), a bad argument — comes back as JSON with `error.code`, so the model can change plan.
|
|
128
|
+
None of those are charged.
|
|
129
|
+
A missing or revoked key raises `TikdataapiAuthError`, because no model can repair it.
|
|
130
|
+
Rate limits and upstream faults raise `TikdataapiError`, so your framework's retry policy sees them.
|
|
131
|
+
|
|
132
|
+
## MCP instead
|
|
133
|
+
|
|
134
|
+
An agent that speaks MCP needs no package. Connect the hosted server:
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
claude mcp add --transport http tikdataapi https://api.tikdataapi.com/mcp
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
It signs in with OAuth on first use. Its registry name is `com.tikdataapi/tikdataapi`.
|
|
141
|
+
|
|
142
|
+
## Keys and pricing
|
|
143
|
+
|
|
144
|
+
Get a key at [tikdataapi.com/dashboard](https://tikdataapi.com/dashboard). 1,000 credits to start, no card.
|
|
145
|
+
Prepaid packs from $10; credits never expire. Live status: [tikdataapi.com/status](https://tikdataapi.com/status).
|
|
146
|
+
|
|
147
|
+
MIT licensed.
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "langchain-tikdataapi"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "LangChain tools for tikdataapi.com: read-only public TikTok profiles, videos, comments, replies, hashtags, sounds, playlists, reposts, related and trending videos, transcripts, live state, video, account, sound, live and hashtag search and follower lists."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
requires-python = ">=3.10"
|
|
9
|
+
keywords = [
|
|
10
|
+
"langchain", "langgraph", "agents", "tools", "tiktok", "tiktok-api",
|
|
11
|
+
"videos", "comments", "hashtags", "search", "followers", "music", "playlists", "trending", "live",
|
|
12
|
+
"transcripts", "mcp", "llm",
|
|
13
|
+
"tikdataapi",
|
|
14
|
+
]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 4 - Beta",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
20
|
+
]
|
|
21
|
+
dependencies = [
|
|
22
|
+
"langchain-core>=0.3.0,<2.0.0",
|
|
23
|
+
"httpx>=0.27.0,<0.29.0",
|
|
24
|
+
"pydantic>=2.7.0,<3.0.0",
|
|
25
|
+
]
|
|
26
|
+
|
|
27
|
+
[project.urls]
|
|
28
|
+
Homepage = "https://tikdataapi.com"
|
|
29
|
+
Documentation = "https://tikdataapi.com/docs"
|
|
30
|
+
"API reference" = "https://tikdataapi.com/reference"
|
|
31
|
+
"Source code" = "https://github.com/tikdataapi/tikdataapi"
|
|
32
|
+
"Issue tracker" = "https://github.com/tikdataapi/tikdataapi/issues"
|
|
33
|
+
|
|
34
|
+
[dependency-groups]
|
|
35
|
+
dev = ["pytest>=8.0", "pytest-asyncio>=0.24", "respx>=0.21"]
|
|
36
|
+
|
|
37
|
+
[build-system]
|
|
38
|
+
requires = ["hatchling"]
|
|
39
|
+
build-backend = "hatchling.build"
|
|
40
|
+
|
|
41
|
+
[tool.hatch.build.targets.wheel]
|
|
42
|
+
packages = ["src/langchain_tikdataapi"]
|
|
43
|
+
|
|
44
|
+
[tool.pytest.ini_options]
|
|
45
|
+
asyncio_mode = "auto"
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"""LangChain tools for tikdataapi.com: read-only public TikTok data.
|
|
2
|
+
|
|
3
|
+
from langchain_tikdataapi import TikdataapiToolkit
|
|
4
|
+
|
|
5
|
+
tools = TikdataapiToolkit.from_api_key().get_tools()
|
|
6
|
+
|
|
7
|
+
Read-only by design. No posting, no likes, no direct messages, no private data,
|
|
8
|
+
no TikTok login. Not affiliated with TikTok or ByteDance.
|
|
9
|
+
Docs: https://tikdataapi.com/docs
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from .client import TikdataapiAuthError, TikdataapiClient, TikdataapiError
|
|
13
|
+
from .toolkit import TikdataapiToolkit
|
|
14
|
+
from .tools import (
|
|
15
|
+
ALL_TOOLS,
|
|
16
|
+
GetBalance,
|
|
17
|
+
GetCommentReplies,
|
|
18
|
+
GetComments,
|
|
19
|
+
GetHashtag,
|
|
20
|
+
GetHashtagVideos,
|
|
21
|
+
GetMusic,
|
|
22
|
+
GetMusicVideos,
|
|
23
|
+
GetPlaylistVideos,
|
|
24
|
+
GetRelatedVideos,
|
|
25
|
+
GetTrendingVideos,
|
|
26
|
+
GetUser,
|
|
27
|
+
GetUserFollowers,
|
|
28
|
+
GetUserFollowing,
|
|
29
|
+
GetUserLive,
|
|
30
|
+
GetUserPlaylists,
|
|
31
|
+
GetUserReposts,
|
|
32
|
+
GetUsers,
|
|
33
|
+
GetUsersVideos,
|
|
34
|
+
GetUserVideos,
|
|
35
|
+
GetVideo,
|
|
36
|
+
GetVideos,
|
|
37
|
+
GetVideoTranscript,
|
|
38
|
+
SearchHashtags,
|
|
39
|
+
SearchLive,
|
|
40
|
+
SearchMusic,
|
|
41
|
+
SearchUsers,
|
|
42
|
+
SearchVideos,
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
__version__ = "0.1.0"
|
|
46
|
+
|
|
47
|
+
__all__ = [
|
|
48
|
+
"ALL_TOOLS",
|
|
49
|
+
"GetBalance",
|
|
50
|
+
"GetCommentReplies",
|
|
51
|
+
"GetComments",
|
|
52
|
+
"GetHashtag",
|
|
53
|
+
"GetHashtagVideos",
|
|
54
|
+
"GetMusic",
|
|
55
|
+
"GetMusicVideos",
|
|
56
|
+
"GetPlaylistVideos",
|
|
57
|
+
"GetRelatedVideos",
|
|
58
|
+
"GetTrendingVideos",
|
|
59
|
+
"GetUser",
|
|
60
|
+
"GetUserFollowers",
|
|
61
|
+
"GetUserFollowing",
|
|
62
|
+
"GetUserLive",
|
|
63
|
+
"GetUserPlaylists",
|
|
64
|
+
"GetUserReposts",
|
|
65
|
+
"GetUserVideos",
|
|
66
|
+
"GetUsers",
|
|
67
|
+
"GetUsersVideos",
|
|
68
|
+
"GetVideo",
|
|
69
|
+
"GetVideoTranscript",
|
|
70
|
+
"GetVideos",
|
|
71
|
+
"SearchHashtags",
|
|
72
|
+
"SearchLive",
|
|
73
|
+
"SearchMusic",
|
|
74
|
+
"SearchUsers",
|
|
75
|
+
"SearchVideos",
|
|
76
|
+
"TikdataapiAuthError",
|
|
77
|
+
"TikdataapiClient",
|
|
78
|
+
"TikdataapiError",
|
|
79
|
+
"TikdataapiToolkit",
|
|
80
|
+
"__version__",
|
|
81
|
+
]
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"""HTTP client for the tikdataapi.com REST API.
|
|
2
|
+
|
|
3
|
+
The tools hand the model the JSON the API returns, unchanged, so it sees
|
|
4
|
+
``credits_charged`` and ``balance_remaining`` on every call and can stop before
|
|
5
|
+
the balance runs out.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import os
|
|
11
|
+
from typing import Any, Mapping
|
|
12
|
+
|
|
13
|
+
import httpx
|
|
14
|
+
|
|
15
|
+
BASE_URL = "https://api.tikdataapi.com"
|
|
16
|
+
|
|
17
|
+
#: Failures the model can act on: try another username, ask for a smaller page,
|
|
18
|
+
#: tell the user the balance is empty. These come back as data.
|
|
19
|
+
_REPORTED = frozenset({400, 402, 403, 404})
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class TikdataapiError(RuntimeError):
|
|
23
|
+
"""A call failed in a way the model cannot repair."""
|
|
24
|
+
|
|
25
|
+
def __init__(self, message: str, *, code: str = "", status: int = 0, request_id: str = "") -> None:
|
|
26
|
+
super().__init__(message)
|
|
27
|
+
self.code = code
|
|
28
|
+
self.status = status
|
|
29
|
+
self.request_id = request_id
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class TikdataapiAuthError(TikdataapiError):
|
|
33
|
+
"""The key is missing, unknown or revoked. A configuration fault."""
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _problem(response: httpx.Response) -> dict[str, Any]:
|
|
37
|
+
try:
|
|
38
|
+
body = response.json()
|
|
39
|
+
except ValueError:
|
|
40
|
+
body = {}
|
|
41
|
+
return body if isinstance(body, dict) else {}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class TikdataapiClient:
|
|
45
|
+
"""Thin wrapper over the REST API. Sync and async, one key.
|
|
46
|
+
|
|
47
|
+
Args:
|
|
48
|
+
api_key: Key from https://tikdataapi.com/dashboard. Read from
|
|
49
|
+
``TIKDATAAPI_KEY`` when not given.
|
|
50
|
+
base_url: Override for testing or for a private deployment.
|
|
51
|
+
timeout: Seconds per request.
|
|
52
|
+
max_results: Ceiling on ``count`` for every paging tool. Set it to cap
|
|
53
|
+
what one agent step can spend. ``None`` leaves the API default.
|
|
54
|
+
"""
|
|
55
|
+
|
|
56
|
+
def __init__(
|
|
57
|
+
self,
|
|
58
|
+
api_key: str | None = None,
|
|
59
|
+
*,
|
|
60
|
+
base_url: str = BASE_URL,
|
|
61
|
+
timeout: float = 30.0,
|
|
62
|
+
max_results: int | None = None,
|
|
63
|
+
client: httpx.Client | None = None,
|
|
64
|
+
async_client: httpx.AsyncClient | None = None,
|
|
65
|
+
) -> None:
|
|
66
|
+
key = api_key or os.environ.get("TIKDATAAPI_KEY")
|
|
67
|
+
if not key:
|
|
68
|
+
raise TikdataapiAuthError(
|
|
69
|
+
"No API key. Pass api_key=... or set TIKDATAAPI_KEY. "
|
|
70
|
+
"Get one at https://tikdataapi.com/dashboard."
|
|
71
|
+
)
|
|
72
|
+
self.api_key = key
|
|
73
|
+
self.base_url = base_url.rstrip("/")
|
|
74
|
+
self.timeout = timeout
|
|
75
|
+
self.max_results = max_results
|
|
76
|
+
self._client = client
|
|
77
|
+
self._async_client = async_client
|
|
78
|
+
|
|
79
|
+
@property
|
|
80
|
+
def _headers(self) -> dict[str, str]:
|
|
81
|
+
return {
|
|
82
|
+
"x-api-key": self.api_key,
|
|
83
|
+
"accept": "application/json",
|
|
84
|
+
"user-agent": "langchain-tikdataapi",
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
def cap(self, count: int | None) -> int | None:
|
|
88
|
+
"""Apply ``max_results`` to a requested page size."""
|
|
89
|
+
if count is None:
|
|
90
|
+
return self.max_results
|
|
91
|
+
if self.max_results is None:
|
|
92
|
+
return count
|
|
93
|
+
return min(count, self.max_results)
|
|
94
|
+
|
|
95
|
+
def _handle(self, response: httpx.Response) -> dict[str, Any]:
|
|
96
|
+
if response.status_code < 400:
|
|
97
|
+
return _problem(response)
|
|
98
|
+
|
|
99
|
+
body = _problem(response)
|
|
100
|
+
error = body.get("error") or {}
|
|
101
|
+
code = str(error.get("code") or "")
|
|
102
|
+
message = str(error.get("message") or response.reason_phrase or "request failed")
|
|
103
|
+
request_id = str(body.get("request_id") or "")
|
|
104
|
+
|
|
105
|
+
if response.status_code == 401:
|
|
106
|
+
raise TikdataapiAuthError(
|
|
107
|
+
f"{message} (code {code or 'invalid_key'}). "
|
|
108
|
+
"Check TIKDATAAPI_KEY against https://tikdataapi.com/dashboard.",
|
|
109
|
+
code=code,
|
|
110
|
+
status=401,
|
|
111
|
+
request_id=request_id,
|
|
112
|
+
)
|
|
113
|
+
if response.status_code in _REPORTED:
|
|
114
|
+
# Nothing was charged for these. Let the model read and decide.
|
|
115
|
+
return body
|
|
116
|
+
raise TikdataapiError(
|
|
117
|
+
f"{message} (HTTP {response.status_code}, code {code}, request {request_id})",
|
|
118
|
+
code=code,
|
|
119
|
+
status=response.status_code,
|
|
120
|
+
request_id=request_id,
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
@staticmethod
|
|
124
|
+
def _clean(params: Mapping[str, Any] | None) -> dict[str, Any]:
|
|
125
|
+
return {k: v for k, v in (params or {}).items() if v is not None}
|
|
126
|
+
|
|
127
|
+
def get(self, path: str, params: Mapping[str, Any] | None = None) -> dict[str, Any]:
|
|
128
|
+
client = self._client
|
|
129
|
+
if client is None:
|
|
130
|
+
with httpx.Client(timeout=self.timeout) as fresh:
|
|
131
|
+
return self._handle(
|
|
132
|
+
fresh.get(f"{self.base_url}{path}", params=self._clean(params), headers=self._headers)
|
|
133
|
+
)
|
|
134
|
+
return self._handle(
|
|
135
|
+
client.get(f"{self.base_url}{path}", params=self._clean(params), headers=self._headers)
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
async def aget(self, path: str, params: Mapping[str, Any] | None = None) -> dict[str, Any]:
|
|
139
|
+
client = self._async_client
|
|
140
|
+
if client is None:
|
|
141
|
+
async with httpx.AsyncClient(timeout=self.timeout) as fresh:
|
|
142
|
+
return self._handle(
|
|
143
|
+
await fresh.get(
|
|
144
|
+
f"{self.base_url}{path}", params=self._clean(params), headers=self._headers
|
|
145
|
+
)
|
|
146
|
+
)
|
|
147
|
+
return self._handle(
|
|
148
|
+
await client.get(f"{self.base_url}{path}", params=self._clean(params), headers=self._headers)
|
|
149
|
+
)
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"""The whole read surface as one toolkit."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Iterable
|
|
6
|
+
|
|
7
|
+
from langchain_core.tools import BaseTool
|
|
8
|
+
from langchain_core.tools.base import BaseToolkit
|
|
9
|
+
from pydantic import ConfigDict
|
|
10
|
+
|
|
11
|
+
from .client import TikdataapiClient
|
|
12
|
+
from .tools import ALL_TOOLS
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class TikdataapiToolkit(BaseToolkit):
|
|
16
|
+
"""Every read tool, sharing one client and one credit budget.
|
|
17
|
+
|
|
18
|
+
Example:
|
|
19
|
+
>>> from langchain_tikdataapi import TikdataapiToolkit
|
|
20
|
+
>>> toolkit = TikdataapiToolkit.from_api_key(max_results=25)
|
|
21
|
+
>>> tools = toolkit.get_tools()
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
client: A configured :class:`TikdataapiClient`.
|
|
25
|
+
include: Tool names to keep. ``None`` keeps them all.
|
|
26
|
+
exclude: Tool names to drop, applied after ``include``.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
model_config = ConfigDict(arbitrary_types_allowed=True)
|
|
30
|
+
|
|
31
|
+
client: TikdataapiClient
|
|
32
|
+
include: tuple[str, ...] | None = None
|
|
33
|
+
exclude: tuple[str, ...] = ()
|
|
34
|
+
|
|
35
|
+
@classmethod
|
|
36
|
+
def from_api_key(
|
|
37
|
+
cls,
|
|
38
|
+
api_key: str | None = None,
|
|
39
|
+
*,
|
|
40
|
+
max_results: int | None = None,
|
|
41
|
+
include: Iterable[str] | None = None,
|
|
42
|
+
exclude: Iterable[str] = (),
|
|
43
|
+
**client_kwargs: object,
|
|
44
|
+
) -> "TikdataapiToolkit":
|
|
45
|
+
"""Build a toolkit from a key, or from ``TIKDATAAPI_KEY``.
|
|
46
|
+
|
|
47
|
+
``max_results`` caps the page size of every paging tool, which caps what
|
|
48
|
+
one agent step can spend.
|
|
49
|
+
"""
|
|
50
|
+
return cls(
|
|
51
|
+
client=TikdataapiClient(api_key, max_results=max_results, **client_kwargs), # type: ignore[arg-type]
|
|
52
|
+
include=tuple(include) if include is not None else None,
|
|
53
|
+
exclude=tuple(exclude),
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
def get_tools(self) -> list[BaseTool]:
|
|
57
|
+
tools = [tool(client=self.client) for tool in ALL_TOOLS]
|
|
58
|
+
if self.include is not None:
|
|
59
|
+
wanted = set(self.include)
|
|
60
|
+
tools = [t for t in tools if t.name in wanted]
|
|
61
|
+
if self.exclude:
|
|
62
|
+
unwanted = set(self.exclude)
|
|
63
|
+
tools = [t for t in tools if t.name not in unwanted]
|
|
64
|
+
return tools
|