Skip to content

Commit 9615b37

Browse files
authored
device_info 储存为永久使用来降低对 AppSign 的请求
1 parent 3b9b8cf commit 9615b37

File tree

1 file changed

+51
-34
lines changed

1 file changed

+51
-34
lines changed

douyin/douyin_appsign.py

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,11 @@ def __init__(self, width = 500, height = 300):
2727

2828
def getToken(self):
2929
req = requests.get('https://api.appsign.vip:2688/token/douyin/version/2.7.0').json()
30-
return self.save_json(req)
30+
return self.save_json('douyin_token.txt', req)
3131

3232
def getDevice(self):
3333
req = requests.get('https://api.appsign.vip:2688/douyin/device/new/version/2.7.0').json()
34-
device_info = req['data']
35-
return device_info
34+
return self.save_json('douyin_device.txt', req)
3635

3736
def getSign(self, token, query):
3837
req = requests.post('https://api.appsign.vip:2688/sign', json={'token': token, 'query': query}).json()
@@ -48,19 +47,41 @@ def getSign(self, token, query):
4847
sign = req['success']
4948
return sign
5049

50+
def getParams(self, device_info, APPINFO):
51+
params = {
52+
'iid': device_info['iid'],
53+
'idfa': device_info['idfa'],
54+
'vid': device_info['vid'],
55+
'device_id': device_info['device_id'],
56+
'openudid': device_info['openudid'],
57+
'device_type': device_info['device_type'],
58+
'os_version': device_info['os_version'],
59+
'os_api': device_info['os_api'],
60+
'screen_width': device_info['screen_width'],
61+
'device_platform': device_info['device_platform'],
62+
'version_code': APPINFO['version_code'],
63+
'channel': APPINFO['channel'],
64+
'app_name': APPINFO['app_name'],
65+
'build_number': APPINFO['build_number'],
66+
'app_version': APPINFO['app_version'],
67+
'aid': APPINFO['aid'],
68+
'ac': 'WIFI'
69+
}
70+
return params
71+
5172
def params2str(self, params):
5273
query = ''
5374
for k, v in params.items():
5475
query += '%s=%s&' % (k, v)
5576
query = query.strip('&')
5677
return query
5778

58-
def save_json(self, data):
59-
with open('douyin.txt', 'w') as f:
79+
def save_json(self, filename, data):
80+
with open(filename, 'w') as f:
6081
json.dump(data, f, ensure_ascii=False)
6182

62-
def load_json(self):
63-
with open('douyin.txt', 'r') as f:
83+
def load_json(self, filename):
84+
with open(filename, 'r') as f:
6485
data = json.load(f)
6586
return data
6687

@@ -80,7 +101,22 @@ def get_video_urls(self, user_id, type_flag='f'):
80101
unique_id = ''
81102
max_cursor = 0
82103
has_more = 1
83-
device_info = self.getDevice()
104+
if not os.path.isfile('douyin_device.txt'):
105+
self.getDevice()
106+
if not os.path.isfile('douyin_token.txt'):
107+
self.getToken()
108+
try:
109+
while self.load_json('douyin_device.txt')['message']:
110+
print('伺服器错误: %s 重试中' % self.load_json('douyin_device.txt')['message'])
111+
self.getDevice()
112+
except:
113+
pass
114+
try:
115+
while self.load_json('douyin_token.txt')['message']:
116+
print('伺服器错误: %s 重试中' % self.load_json('douyin_token.txt')['message'])
117+
self.getToken()
118+
except:
119+
pass
84120
APPINFO = {
85121
'version_code': '2.7.0',
86122
'app_version': '2.7.0',
@@ -89,37 +125,18 @@ def get_video_urls(self, user_id, type_flag='f'):
89125
'build_number': '27014',
90126
'aid': '1128'
91127
}
92-
params = {
93-
'iid': device_info['iid'],
94-
'idfa': device_info['idfa'],
95-
'vid': device_info['vid'],
96-
'device_id': device_info['device_id'],
97-
'openudid': device_info['openudid'],
98-
'device_type': device_info['device_type'],
99-
'os_version': device_info['os_version'],
100-
'os_api': device_info['os_api'],
101-
'screen_width': device_info['screen_width'],
102-
'device_platform': device_info['device_platform'],
103-
'version_code': APPINFO['version_code'],
104-
'channel': APPINFO['channel'],
105-
'app_name': APPINFO['app_name'],
106-
'build_number': APPINFO['build_number'],
107-
'app_version': APPINFO['app_version'],
108-
'aid': APPINFO['aid'],
109-
'ac': 'WIFI',
110-
'count': '12',
111-
'keyword': user_id,
112-
'offset': '0'
113-
}
114128
print('解析视频链接中')
129+
device_info = self.load_json('douyin_device.txt')['data']
130+
params = self.getParams(device_info, APPINFO)
131+
params['count'] = '12'
132+
params['keyword'] = user_id
133+
params['offset'] = '0'
115134
query = self.params2str(params)
116-
if not os.path.isfile('douyin.txt'):
117-
self.getToken()
118-
token = self.load_json()['token']
135+
token = self.load_json('douyin_token.txt')['token']
119136
sign = self.getSign(token, query)
120137
while not sign:
121138
self.getToken()
122-
token = self.load_json()['token']
139+
token = self.load_json('douyin_token.txt')['token']
123140
sign = self.getSign(token, query)
124141
params['mas'] = sign['mas']
125142
params['as'] = sign['as']

0 commit comments

Comments
 (0)