11# -*- coding:utf-8 -*-
22from contextlib import closing
3- import requests , json , time , re , os , sys , time
3+ import requests , json , re , os , sys
44from datetime import datetime , timezone
55
66class DouYin (object ):
@@ -29,12 +29,13 @@ def get_video_urls(self, user_id):
2929"""
3030video_names = []
3131video_urls = []
32+ share_urls = []
3233unique_id = ''
3334while unique_id != user_id :
3435search_url = 'https://api.amemv.com/aweme/v1/discover/search/?cursor=0&keyword=%s&count=10&type=1&retry_type=no_retry&iid=17900846586&device_id=34692364855&ac=wifi&channel=xiaomi&aid=1128&app_name=aweme&version_code=162&version_name=1.6.2&device_platform=android&ssmix=a&device_type=MI+5&device_brand=Xiaomi&os_api=24&os_version=7.0&uuid=861945034132187&openudid=dc451556fc0eeadb&manifest_version_code=162&resolution=1080*1920&dpi=480&update_version_code=1622' % user_id
3536req = requests .get (search_url , headers = self .headers )
3637html = json .loads (req .text )
37- aweme_count = html ['user_list' ][0 ]['user_info' ]['aweme_count' ]
38+ aweme_count = 32767 # html['user_list'][0]['user_info']['aweme_count']
3839uid = html ['user_list' ][0 ]['user_info' ]['uid' ]
3940nickname = html ['user_list' ][0 ]['user_info' ]['nickname' ]
4041unique_id = html ['user_list' ][0 ]['user_info' ]['unique_id' ]
@@ -54,9 +55,10 @@ def get_video_urls(self, user_id):
5455video_names .append (tc + '.mp4' )
5556else :
5657video_names .append (tc + '-' + share_desc + '.mp4' )
57- video_urls .append (each ['share_info' ]['share_url' ])
58+ share_urls .append (each ['share_info' ]['share_url' ])
59+ video_urls .append (each ['video' ]['play_addr' ]['url_list' ][0 ])
5860
59- return video_names , video_urls , nickname
61+ return video_names , video_urls , share_urls , nickname
6062
6163def get_download_url (self , video_url , watermark_flag ):
6264"""
@@ -66,15 +68,13 @@ def get_download_url(self, video_url, watermark_flag):
6668Returns:
6769download_url: 带水印的视频下载地址
6870"""
69- req = requests .get (video_url )
70- _playaddr_re = re .compile (r'playAddr: "(.+)",' )
71- playaddr = _playaddr_re .search (req .text )
7271# 带水印视频
7372if watermark_flag == True :
74- download_url = playaddr . group ( 1 )
73+ download_url = video_url
7574# 无水印视频
7675else :
77- download_url = playaddr .group (1 ).replace ('playwm' ,'play' )
76+ download_url = video_url .replace ('playwm' , 'play' )
77+
7878return download_url
7979
8080def video_downloader (self , video_url , video_name , watermark_flag = False ):
@@ -91,11 +91,11 @@ def video_downloader(self, video_url, video_name, watermark_flag=False):
9191video_url = self .get_download_url (video_url , watermark_flag = watermark_flag )
9292with closing (requests .get (video_url , headers = self .headers , stream = True )) as response :
9393chunk_size = 1024
94- content_size = int (response .headers ['content-length' ])
94+ content_size = int (response .headers ['content-length' ])
9595if response .status_code == 200 :
9696sys .stdout .write (' [文件大小]:%0.2f MB\n ' % (content_size / chunk_size / 1024 ))
9797
98- with open (video_name , "wb" ) as file :
98+ with open (video_name , 'wb' ) as file :
9999for data in response .iter_content (chunk_size = chunk_size ):
100100file .write (data )
101101size += len (data )
@@ -115,12 +115,12 @@ def run(self):
115115self .hello ()
116116user_id = input ('请输入ID(例如145651081):' )
117117watermark_flag = int (input ('是否下载带水印的视频(0-否,1-是):' ))
118- video_names , video_urls , nickname = self .get_video_urls (user_id )
118+ video_names , video_urls , share_urls , nickname = self .get_video_urls (user_id )
119119if nickname not in os .listdir ():
120120os .mkdir (nickname )
121121print ('视频下载中:共有%d个作品!\n ' % len (video_urls ))
122122for num in range (len (video_urls )):
123- print (' 解析第%d个视频链接 [%s] 中,请稍后!\n ' % (num + 1 , video_urls [num ]))
123+ print (' 解析第%d个视频链接 [%s] 中,请稍后!\n ' % (num + 1 , share_urls [num ]))
124124if '\\ ' in video_names [num ]:
125125video_name = video_names [num ].replace ('\\ ' , '' )
126126elif '/' in video_names [num ]:
0 commit comments