forked from jielundong/douyin-gg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoyin.py
181 lines (153 loc) · 6.04 KB
/
doyin.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
try:
from BeautifulSoup import BeautifulSoup
except ImportError:
from bs4 import BeautifulSoup
import requests
import urllib.request
import urllib
import json
import re
import os
headers = {
'accept-encoding': 'deflate',
'accept-language': 'zh-CN,zh;q=0.9',
'pragma': 'no-cache',
'cache-control': 'no-cache',
'upgrade-insecure-requests': '1',
'user-agent': "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1",
}
HEADERS = {'user-agent': "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1"}
mapCode2Name = {"0xe602":"num_","0xe605":"num_3","0xe606":"num_4","0xe603":"num_1","0xe604":"num_2","0xe618":"num_","0xe619":"num_4","0xe60a":"num_8","0xe60b":"num_9","0xe60e":"num_","0xe60f":"num_5","0xe60c":"num_4", \
"0xe60d":"num_1","0xe612":"num_6","0xe613":"num_8","0xe610":"num_3","0xe611":"num_2","0xe616":"num_1","0xe617":"num_3","0xe614":"num_9","0xe615":"num_7","0xe609":"num_7","0xe607":"num_5","0xe608":"num_6","0xe61b":"num_5",\
"0xe61c":"num_8","0xe61a":"num_2","0xe61f":"num_6","0xe61d":"num_9","0xe61e":"num_7"}
mapCode2Font = {"num_9":8,"num_5":5,"num_6":6,"num_":1,"num_7":9,"num_8":7,"num_1":0,"num_2":3,"num_3":2,"num_4":4}
def getUserInfo(shared_url, **headers):
real_url = getRealAddress(shared_url)
parsed = urllib.parse.urlparse(real_url)
hostname = parsed.hostname
sec_uid = urllib.parse.parse_qs(parsed.query)['sec_uid']
user_info_url = "https://%s/web/api/v2/user/info/" % hostname
user_info_params = { 'sec_uid': sec_uid }
res = requests.get(user_info_url, headers=headers,
params=user_info_params).json()
user_info = res['user_info']
user_avatar = user_info['avatar_larger']['url_list'][2]
user_nickname = user_info['nickname']
user_sign = user_info['signature']
user_id = user_info['unique_id']
count_of_videos = user_info['aweme_count']
follower_count = user_info['follower_count']
following_count = user_info['following_count']
zan_count = user_info['total_favorited']
like_count = user_info['favoriting_count']
return {'user_avatar':user_avatar, 'user_nickname':user_nickname, 'user_sign':user_sign, 'user_id':user_id,
'count_of_videos':count_of_videos, 'follower_count':follower_count, 'following_count':following_count,
'zan_count':zan_count, 'like_count':like_count, 'like_count':like_count}
def getUserVideos(url):
number = re.findall(r'share/user/(\d+)', url)
if not len(number):
return
dytk = get_dytk(url)
# hostname = urllib.parse.urlparse(url).hostname
# if hostname != 't.tiktok.com' and not dytk:
# return
user_id = number[0]
return getUserMedia(user_id, dytk, url)
def getRealAddress(url):
if url.find('v.douyin.com') < 0:
return url
res = requests.get(url, headers=headers, allow_redirects=False)
return res.headers['Location'] if res.status_code == 302 else None
def get_dytk(url):
res = requests.get(url, headers=headers)
if not res:
return None
dytk = re.findall("dytk: '(.*)'", res.content.decode('utf-8'))
if len(dytk):
return dytk[0]
return None
def getUserMedia(user_id, dytk, url):
videos = []
parsed = urllib.parse.urlparse(url)
hostname = parsed.hostname
sec_uid = urllib.parse.parse_qs(parsed.query)['sec_uid']
#signature = generateSignature(str(user_id))
user_video_url = "https://%s/web/api/v2/aweme/post/" % hostname
user_video_params = {
'sec_uid': sec_uid,
'count': '21',
'max_cursor': '0',
'aid': '1128',
'_signature': '2Vx9mxAZh0o-K4Wdv7NFKNlcfY',
'dytk': dytk
}
if hostname == 't.tiktok.com':
user_video_params.pop('dytk')
user_video_params['aid'] = '1180'
max_cursor, video_count = None, 0
while True:
if max_cursor:
user_video_params['max_cursor'] = str(max_cursor)
res = requests.get(user_video_url, headers=headers,
params=user_video_params)
contentJson = json.loads(res.content.decode('utf-8'))
aweme_list = contentJson.get('aweme_list', [])
for aweme in aweme_list:
video_count += 1
aweme['hostname'] = hostname
video = {
'addr': aweme['video']['play_addr']['url_list'][0],
'desc': aweme['desc'],
'duration': aweme['video']['duration'],
'cover': aweme['video']['cover']['url_list'][0],
'statistics': aweme['statistics']
}
videos.append(video)
if contentJson.get('has_more'):
max_cursor = contentJson.get('max_cursor')
else:
break
if video_count == 0:
print("There's no video in number %s." % user_id)
return videos
def getHtml(url,**headers):
try:
req = urllib.request.Request(url,headers=headers)
resp = urllib.request.urlopen(req)
return str(resp.read(), 'utf-8')
except urllib.error.HTTPError as e:
print(e.msg)
return ''
def woff2tff(ls):
res = ''
for s in ls:
res = res + formatNum(s)
return res
def splitByChinese(s):
p = re.compile("[\u4e00-\u9fa5]", re.U)
return p.split(s)
def isChinese(s):
p = re.compile("[\u4e00-\u9fa5]", re.U)
result = p.match(s)
if result :
return True
return False
def formatNum(s):
if isChinese(s):
return ''
if len(s)<8 or s.find("hzsdxe6") < 0 :
return s
s1 = '0'+s[4:-1]
res = mapCode2Font[mapCode2Name[s1]]
return str(res)
def getUserAll(shared_url):
profile = getUserInfo(shared_url, **HEADERS)
if profile:
videos = getUserVideos(getRealAddress(shared_url))
profile['videos'] = videos
return profile
if __name__ == '__main__':
userInfo = getUserAll("https://v.douyin.com/JrN1GLV")
print(json.dumps(userInfo))