Skip to content

Commit

Permalink
Merge pull request Jack-Cherish#73 from steven7851/patch-18
Browse files Browse the repository at this point in the history
重新使用网页 API
  • Loading branch information
Jack-Cherish authored Jan 29, 2019
2 parents 850fb01 + 2395256 commit db87fa6
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 345 deletions.
4 changes: 2 additions & 2 deletions douyin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@

## 使用说明

python douyin_appsign.py
python douyin.py

感谢 [AppSign](https://github.com/AppSign/douyin) 提供免费加签服务
关于重新链接次数: 用户视频通常重新链接30次以内会成功,而收藏视频目前链接成功机率极低,当然有耐心也能等他成功为止。。
76 changes: 58 additions & 18 deletions douyin/douyin.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self, width = 500, height = 300):
'X-Forwarded-For': str(rip),
}

def get_video_urls(self, user_id):
def get_video_urls(self, user_id, type_flag='f'):
"""
获得视频播放地址
Parameters:
Expand All @@ -40,40 +40,65 @@ def get_video_urls(self, user_id):
share_urls = []
max_cursor = 0
has_more = 1
share_user_url = 'https://www.amemv.com/share/user/%s' % user_id
i = 0
share_user_url = 'https://www.douyin.com/share/user/%s' % user_id
share_user = requests.get(share_user_url, headers=self.headers)
_dytk_re = re.compile(r"dytk:\s*'(.+)'")
while share_user.status_code != 200:
share_user = requests.get(share_user_url, headers=self.headers)
_dytk_re = re.compile(r"dytk\s*:\s*'(.+)'")
dytk = _dytk_re.search(share_user.text).group(1)
_nickname_re = re.compile(r'<p class="nickname">(.+?)<\/p>')
nickname = _nickname_re.search(share_user.text).group(1)
print('JS签名下载中')
urllib.request.urlretrieve('https://raw.githubusercontent.com/Jack-Cherish/python-spider/master/douyin/fuck-byted-acrawler.js', 'fuck-byted-acrawler.js')
try:
process = Popen(['node', 'fuck-byted-acrawler.js', str(user_id)], stdout=PIPE, stderr=PIPE)
Popen(['node', '-v'], stdout=PIPE, stderr=PIPE).communicate()
except (OSError, IOError) as err:
print('请先安装 node.js: https://nodejs.org/')
sys.exit()
sign = process.communicate()[0].decode().strip('\n').strip('\r')
user_url_prefix = 'https://www.douyin.com/aweme/v1/aweme/favorite' if type_flag == 'f' else 'https://www.douyin.com/aweme/v1/aweme/post'
print('解析视频链接中')
while has_more != 0:
user_url = 'https://www.amemv.com/aweme/v1/aweme/post/?user_id=%s&max_cursor=%s&count=21&aid=1128&_signature=%s&dytk=%s' % (user_id, max_cursor, sign, dytk)
process = Popen(['node', 'fuck-byted-acrawler.js', str(user_id)], stdout=PIPE, stderr=PIPE)
_sign = process.communicate()[0].decode().strip('\n').strip('\r')
user_url = user_url_prefix + '/?user_id=%s&max_cursor=%s&count=21&aid=1128&_signature=%s&dytk=%s' % (user_id, max_cursor, _sign, dytk)
req = requests.get(user_url, headers=self.headers)
while req.status_code != 200:
req = requests.get(user_url, headers=self.headers)
html = json.loads(req.text)
try:
while html['aweme_list'] == []:
i = i + 1
sys.stdout.write('已重新链接' + str(i) + '次 (若超过100次,请ctrl+c强制停止再重来)' + '\r')
sys.stdout.flush()
process = Popen(['node', 'fuck-byted-acrawler.js', str(user_id)], stdout=PIPE, stderr=PIPE)
_sign = process.communicate()[0].decode().strip('\n').strip('\r')
user_url = user_url_prefix + '/?user_id=%s&max_cursor=%s&count=21&aid=1128&_signature=%s&dytk=%s' % (user_id, max_cursor, _sign, dytk)
req = requests.get(user_url, headers=self.headers)
while req.status_code != 200:
req = requests.get(user_url, headers=self.headers)
html = json.loads(req.text)
except:
pass
i = 0
for each in html['aweme_list']:
try:
url = 'https://aweme.snssdk.com/aweme/v1/play/?video_id=%s&line=0&ratio=720p&media_type=4&vr_type=0&test_cdn=None&improve_bitrate=0'
uri = each['video']['play_addr']['uri']
video_url = url % uri
except:
continue
share_desc = each['share_info']['share_desc']
if os.name == 'nt':
for c in r'\/:*?"<>|':
nickname = nickname.replace(c, '').strip().strip('\.')
share_desc = share_desc.replace(c, '').strip()
share_id = each['aweme_id']
if share_desc in ['抖音-原创音乐短视频社区', 'TikTok']:
if share_desc in ['抖音-原创音乐短视频社区', 'TikTok', '']:
video_names.append(share_id + '.mp4')
else:
video_names.append(share_id + '-' + share_desc + '.mp4')
share_urls.append(each['share_info']['share_url'])
video_urls.append(each['video']['play_addr']['url_list'][0])
video_urls.append(video_url)
max_cursor = html['max_cursor']
has_more = html['has_more']

Expand All @@ -89,10 +114,10 @@ def get_download_url(self, video_url, watermark_flag):
"""
# 带水印视频
if watermark_flag == True:
download_url = video_url
download_url = video_url.replace('/play/', '/playwm/')
# 无水印视频
else:
download_url = video_url.replace('playwm', 'play')
download_url = video_url.replace('/playwm/', '/play/')

return download_url

Expand Down Expand Up @@ -132,11 +157,25 @@ def run(self):
None
"""
self.hello()
user_id = input('请输入UID(例如60388937600):')
watermark_flag = int(input('是否下载带水印的视频(0-否,1-是):'))
video_names, video_urls, share_urls, nickname = self.get_video_urls(user_id)
if nickname not in os.listdir():
os.mkdir(nickname)
print('搜索api需要登录,暂时使用UID下载\n分享用户页面,用浏览器打开短链接,原始链接中/share/user/后的数字即是UID')
user_id = input('请输入ID (例如95006183):')
user_id = user_id if user_id else '95006183'
watermark_flag = input('是否下载带水印的视频 (0-否(默认), 1-是):')
watermark_flag = watermark_flag if watermark_flag!='' else '0'
watermark_flag = bool(int(watermark_flag))
type_flag = input('f-收藏的(默认), p-上传的:')
type_flag = type_flag if type_flag!='' else 'f'
save_dir = input('保存路径 (例如"E:/Download/", 默认"./Download/"):')
save_dir = save_dir if save_dir else "./Download/"
video_names, video_urls, share_urls, nickname = self.get_video_urls(user_id, type_flag)
nickname_dir = os.path.join(save_dir, nickname)
if not os.path.exists(save_dir):
os.makedirs(save_dir)
if nickname not in os.listdir(save_dir):
os.mkdir(nickname_dir)
if type_flag == 'f':
if 'favorite' not in os.listdir(nickname_dir):
os.mkdir(os.path.join(nickname_dir, 'favorite'))
print('视频下载中:共有%d个作品!\n' % len(video_urls))
for num in range(len(video_urls)):
print(' 解析第%d个视频链接 [%s] 中,请稍后!\n' % (num + 1, share_urls[num]))
Expand All @@ -146,10 +185,11 @@ def run(self):
video_name = video_names[num].replace('/', '')
else:
video_name = video_names[num]
if os.path.isfile(os.path.join(nickname, video_name)):
video_path = os.path.join(nickname_dir, video_name) if type_flag!='f' else os.path.join(nickname_dir, 'favorite', video_name)
if os.path.isfile(video_path):
print('视频已存在')
else:
self.video_downloader(video_urls[num], os.path.join(nickname, video_name), watermark_flag)
self.video_downloader(video_urls[num], video_path, watermark_flag)
print('\n')
print('下载完成!')

Expand Down
Loading

0 comments on commit db87fa6

Please sign in to comment.