-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.py
249 lines (225 loc) · 9.62 KB
/
api.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
from turtle import Turtle
import requests
import json
import base64
import time
import re, traceback
import urllib.parse
import pickle
from urllib.parse import urlencode, quote
from .browser import Browser
from .ultis import set_url, get_param_url
from .encryption import get_tt_param
class Tiktok:
def __init__(self):
self.BASE_URL = 'https://www.tiktok.com/node/'
def openBrowser(self, url = 'https://tiktok.com/', show_br = False):
self.browser = Browser(url, show_br)
self.browser.launch_borwser()
self.default_params = self.browser.get_defaut_params()
def closeBrowser(self):
self.browser.close_browser()
def getTrendingFeed(self, cookies = {}, limit = 40, first = True):
if first == True:
return self.browser.first_data()
try:
params = {
'count': "30"
}
params.update(self.default_params)
params = dict(sorted(params.items(), key=lambda item: item[1]))
api_url = set_url('/api/recommend/item_list/', params)
data = self.browser.fetch_browser(api_url)
return data
except Exception:
import traceback
print(traceback.format_exc())
return False
def getUserFeed(self, secUid = '', cursor = 0, first = True):
if first == True:
return self.browser.first_data()
try:
params = {
'secUid': secUid,
'count': '30',
'cursor': cursor,
'userId': 'undefined',
}
params.update(self.default_params)
params = dict(sorted(params.items(), key=lambda item: item[1]))
api_url = set_url('/api/post/item_list/', params)
tt_params = get_tt_param(get_param_url(params))
data = self.browser.fetch_browser(api_url, tt_params)
return data
except Exception:
import traceback
print(traceback.format_exc())
return False
def getChallengeFeed(self, ch_id = '', cursor = 0, first = True):
if first == True:
return self.browser.first_data()
try:
params = {
'challengeID': ch_id,
'count': '30',
'cursor': cursor,
}
params.update(self.default_params)
params = dict(sorted(params.items(), key=lambda item: item[1]))
api_url = set_url('/api/challenge/item_list/', params)
tt_params = get_tt_param(get_param_url(params))
data = self.browser.fetch_browser(api_url, tt_params)
return data
except Exception:
import traceback
print(traceback.format_exc())
return False
def getMusicFeed(self, music = '', max_cursor = 0):
if music == '':
return 'Challenge or Ch_id is required'
param = {
"type": 4,
"secUid": "",
"id": '',
"count": 30,
"minCursor": 0,
"maxCursor": max_cursor,
"shareUid": "",
"lang": "",
"verifyFp": "",
}
# ch = self.getInfoMusic(music)
if music:
param['id'] = music
else:
return False
try:
url = self.BASE_URL + 'video/feed'
res = requests.get(
url,
params=param,
headers={
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"authority": "www.tiktok.com",
"Accept-Encoding": "gzip, deflate",
"Connection": "keep-alive",
"Host": "www.tiktok.com",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) coc_coc_browser/86.0.170 Chrome/80.0.3987.170 Safari/537.36",
}
)
resp = res.json()
return resp['body'], res.cookies.get_dict()
except Exception:
print(traceback.format_exc())
return False
def getInfoUser(self, username = '', user_url = '', use_selenium = False):
if username != '':
url = 'http://www.tiktok.com/@{}?lang=en'.format(quote(username))
if user_url != '':
url = user_url
try:
if use_selenium == True:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
options = webdriver.ChromeOptions()
options.add_argument('--disable-gpu')
# options.add_argument('--headless')
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
driver.get(url)
html = driver.page_source
if 'tiktok-verify-page' in html:
input('Please bypass captcha and enter any character to continue:')
html = driver.page_source
driver.close()
driver.quit()
else:
res = requests.get(
url,
headers={
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"authority": "www.tiktok.com",
# "path": "/@{}".format(quote(username)),
"Accept-Encoding": "gzip, deflate",
"Connection": "keep-alive",
"Host": "www.tiktok.com",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) coc_coc_browser/86.0.170 Chrome/80.0.3987.170 Safari/537.36",
}
)
html = res.text
# print(html)
resp = self.__extra_next_data__(html)
return json.loads(resp)['UserModule']
except Exception:
print(traceback.format_exc())
return False
def getInfoChallenge(self, challenge):
if challenge == '':
return 'Challenge is required'
try:
res = requests.get(
'https://www.tiktok.com/tag/{}'.format(quote(challenge)),
headers={
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"authority": "www.tiktok.com",
"path": "/tag/{}".format(quote(challenge)),
"Accept-Encoding": "gzip, deflate",
"Connection": "keep-alive",
"Host": "www.tiktok.com",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) coc_coc_browser/86.0.170 Chrome/80.0.3987.170 Safari/537.36",
}
)
resp = self.__extra_next_data__(res.text)
return json.loads(resp)['ChallengePage']
except Exception:
print(traceback.format_exc())
return False
def getInfoMusic(self, music_url):
if music_url == '':
return 'Challenge is required'
try:
res = requests.get(
music_url,
headers={
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"authority": "www.tiktok.com",
"Accept-Encoding": "gzip, deflate",
"Connection": "keep-alive",
"Host": "www.tiktok.com",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) coc_coc_browser/86.0.170 Chrome/80.0.3987.170 Safari/537.36",
}
)
resp = self.__extra_next_data__(res.text)
return json.loads(resp)['props']['pageProps']
except Exception:
print(traceback.format_exc())
return False
def getInfoVideo(self, url):
if url == '':
return 'URL is required'
try:
res = requests.get(
url,
headers={
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"authority": "www.tiktok.com",
"Accept-Encoding": "gzip, deflate",
"Connection": "keep-alive",
"Host": "www.tiktok.com",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) coc_coc_browser/86.0.170 Chrome/80.0.3987.170 Safari/537.36",
}
)
resp = self.__extra_next_data__(res.text)
video_info = json.loads(resp)['ItemModule']
vid = list(video_info.keys())[0]
return video_info[vid], res.cookies.get_dict()
except Exception:
print(traceback.format_exc())
return False
def __extra_next_data__(self, html):
resp = self.r1(r'window\[\'SIGI_STATE\'\]=(.*?);window\[\'SIGI_RETRY\'\]', html) or \
self.r1(r'<script id="SIGI_STATE" type="application/json">(.*?)</script>', html)
return resp
def r1(self, pattern, text):
m = re.search(pattern, text)
if m:
return m.group(1)