-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
227 lines (168 loc) · 7.47 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!encoding=utf8
from contextlib import asynccontextmanager
import time
from fastapi import FastAPI, HTTPException, Query
import os
import json
from TikTokApi.flow import Flow
from TikTokApi.tiktok import TikTok
from TikTokApi.api import Api
from apscheduler.schedulers.background import BackgroundScheduler
import asyncio
from get_random_device import DEVICES_DIR, get_random_device
"""
Update this to use proxies
Example:
proxies={http:"http://user:[email protected]:port",https:"http://user:[email protected]:port"}
"""
proxies = None
def cleanup_old_files(days: int = 1):
now = time.time()
cutoff = now - (days * 86400) # Convert days to seconds
for filename in os.listdir(DEVICES_DIR):
file_path = os.path.join(DEVICES_DIR, filename)
if os.path.isfile(file_path) and filename.endswith(".json"):
file_mtime = os.path.getmtime(file_path)
if file_mtime < cutoff:
os.remove(file_path)
print(f"Removed stale devices: {file_path}")
async def background_device_register():
try:
cleanup_old_files(days=1)
instance = TikTok(proxies=proxies, debug=False)
flow = Flow(instance)
await flow.device_register()
device_id = instance.device["device_id"]
file_path = os.path.join(DEVICES_DIR, f"{device_id}.json")
del instance.device["proxies"]
print("Device Registered")
with open(file_path, "w", encoding="utf-8") as file:
json.dump(instance.device, file, ensure_ascii=False, indent=4, sort_keys=True)
except Exception as e:
pass
def sync_background_device_register():
asyncio.run(background_device_register())
def tiktok_api():
instance = TikTok(debug=False)
instance.device = get_random_device()
instance.request.proxies = proxies
api = Api(instance)
return api
# Schedule the background task
scheduler = BackgroundScheduler()
# Register new device every x minutes and store it in devices dir
scheduler.add_job(sync_background_device_register, 'interval', minutes=5)
@asynccontextmanager
async def lifespan(app: FastAPI):
await background_device_register()
# Start the scheduler
scheduler.start()
yield
scheduler.shutdown()
app = FastAPI(lifespan=lifespan)
@app.get("/")
async def feed(cursor: int = Query(0, description="Cursor for pagination, defaults to 0 if not provided")):
try:
return await tiktok_api().aweme_v1.feed(max_cursor=cursor)
except Exception as e:
return HTTPException(status_code=500, detail="Internal Server Error")
"""
Video Endpoints
"""
@app.get("/video/{video_id}")
async def video_detail(video_id: str):
try:
return await tiktok_api().aweme_v1.aweme_detail(video_id)
except Exception as e:
return HTTPException(status_code=500, detail="Internal Server Error")
@app.get("/video/search/{keyword}")
async def search_video(keyword: str, cursor: int = Query(0, description="Cursor for pagination, defaults to 0 if not provided")):
try:
return await tiktok_api().aweme_v1.search_item(keyword, offset=cursor)
except Exception as e:
return HTTPException(status_code=500, detail="Internal Server Error")
@app.get("/video/{aweme_id}/comments")
async def video_comments(aweme_id: str, cursor: int = Query(0, description="Cursor for pagination, defaults to 0 if not provided")):
try:
return await tiktok_api().aweme_v1.comment_list(aweme_id=aweme_id, cursor=cursor)
except Exception as e:
return HTTPException(status_code=500, detail="Internal Server Error")
"""
User Endpoints
"""
@app.get("/user/search/{username}")
async def search_user(username: str, cursor: int = Query(0, description="Cursor for pagination, defaults to 0 if not provided")):
try:
return await tiktok_api().aweme_v1.user_search(username, cursor=cursor)
except Exception as e:
return HTTPException(status_code=500, detail="Internal Server Error")
@app.get("/user/{user_id}")
async def user_detail(user_id: str):
try:
return await tiktok_api().aweme_v1.user_profile_other(user_id=user_id)
except Exception as e:
return HTTPException(status_code=500, detail="Internal Server Error")
@app.get("/user/{user_id}/posts")
async def user_videos(user_id: str, cursor: int = Query(0, description="Cursor for pagination, defaults to 0 if not provided")):
try:
return await tiktok_api().aweme_v1.aweme_post(user_id=user_id, max_cursor=cursor)
except Exception as e:
return HTTPException(status_code=500, detail="Internal Server Error")
@app.get("/user/{user_id}/followers")
async def user_followers(user_id: str, cursor: int = Query(0, description="Cursor for pagination, defaults to 0 if not provided")):
try:
return await tiktok_api().aweme_v1.user_follower_list(user_id=user_id, max_time=cursor)
except Exception as e:
return HTTPException(status_code=500, detail="Internal Server Error")
@app.get("/user/{user_id}/following")
async def user_followings(user_id: str, cursor: int = Query(0, description="Cursor for pagination, defaults to 0 if not provided")):
try:
return await tiktok_api().aweme_v1.user_following_list(user_id=user_id, max_time=cursor)
except Exception as e:
return HTTPException(status_code=500, detail="Internal Server Error")
@app.get("/user/{user_id}/likes")
async def user_likes(user_id: str, cursor: int = Query(0, description="Cursor for pagination, defaults to 0 if not provided")):
try:
return await tiktok_api().aweme_v1.user_likes(user_id=user_id, max_cursor=cursor)
except Exception as e:
return HTTPException(status_code=500, detail="Internal Server Error")
"""
Challenge
"""
@app.get("/challenge/{challenge_id}/posts")
async def challenge_posts(challenge_id: str, cursor: int = Query(0, description="Cursor for pagination, defaults to 0 if not provided")):
try:
return await tiktok_api().aweme_v1.challenge_aweme(ch_id=challenge_id, cursor=cursor)
except Exception as e:
return HTTPException(status_code=500, detail="Internal Server Error")
@app.get("/challenge/search/{keyword}")
async def search_challenge(keyword: str, cursor: int = Query(0, description="Cursor for pagination, defaults to 0 if not provided")):
try:
return await tiktok_api().aweme_v1.search_challenge(keyword, cursor=cursor)
except Exception as e:
return HTTPException(status_code=500, detail="Internal Server Error")
"""
Music
"""
@app.get("/music/{music_id}")
async def music_detail(music_id: str):
try:
return await tiktok_api().aweme_v1.music_detail(music_id=music_id)
except Exception as e:
return HTTPException(status_code=500, detail="Internal Server Error")
@app.get("/music/{music_id}/posts")
async def music_posts(music_id: str, cursor: int = Query(0, description="Cursor for pagination, defaults to 0 if not provided")):
try:
return await tiktok_api().aweme_v1.music_aweme(music_id=music_id, cursor=cursor)
except Exception as e:
return HTTPException(status_code=500, detail="Internal Server Error")
@app.get("/music/search/{keyword}")
async def search_music(keyword: str, cursor: int = Query(0, description="Cursor for pagination, defaults to 0 if not provided")):
try:
return await tiktok_api().aweme_v1.search_music(keyword, cursor=cursor)
except Exception as e:
return HTTPException(status_code=500, detail="Internal Server Error")
"""
Start ASGI Server
uvicorn main:app --reload --host 0.0.0.0 --port 8100
"""