Skip to content

Commit

Permalink
👞v1.2.3更新
Browse files Browse the repository at this point in the history
1、解决了max_cursor回到首页时继续解析
2、完善页面输出信息
3、去除了部分无用注释
  • Loading branch information
Johnserf-Seed committed Jul 24, 2021
1 parent 12362d3 commit d66350b
Showing 1 changed file with 44 additions and 69 deletions.
113 changes: 44 additions & 69 deletions TikTokMulti.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
@Description:TikTokMulti.py
@Date :2021/05/25 00:14:28
@Author :JohnserfSeed
@version :1.2
@version :1.2.3
@License :(C)Copyright 2019-2021, Liugroup-NLPR-CASIA
@Mail :[email protected]
'''
Expand All @@ -26,7 +26,7 @@ def __init__(self):
print("#" * 110)
print(
"""
TikTokDownload V1.2.2
TikTokDownload V1.2.3
使用说明:
1、运行软件前先打开目录下 conf.ini 文件按照要求进行配置
2、批量下载可直接修改配置文件,单一视频下载请直接打开粘贴视频链接即可
Expand All @@ -39,6 +39,7 @@ def __init__(self):
print("#" * 110)
print('\r')

#检测配置文件
if os.path.isfile("conf.ini") == True:
pass
else:
Expand All @@ -58,9 +59,9 @@ def __init__(self):
self.cf.set("mode", "mode", "post")
with open("conf.ini","a+") as f:
self.cf.write(f)
print('----生成成功----')
print('----生成成功----\r')
except:
input('----生成失败,正在为您下载配置文件----')
input('----生成失败,正在为您下载配置文件----\r')
r =requests.get('https://gitee.com/johnserfseed/TikTokDownload/raw/main/conf.ini')
with open("conf.ini", "a+") as conf:
conf.write(r.content)
Expand Down Expand Up @@ -108,18 +109,17 @@ def judge_link(self):
#获取解码后原地址
r = requests.get(url = self.Find(self.uid)[0])
multi_url = 'https://www.douyin.com/user/'
#multi_url = 'https://www.iesdouyin.com/share/user/'
#multi_url = 'https://www.iesdouyin.com/share/user/' #旧

#判断输入的是不是用户主页
#if r.url[:27] == multi_url:
if r.url[:28] == multi_url:
print('----为您下载多个视频----\r')
#获取用户sec_uid
#key = re.findall('&sec_uid=(.*?)&',str(r.url))[0]
key = re.findall('/user/(.*?)\?',str(r.url))[0]
if not key:
key = r.url[28:83]
print('----'+'用户的sec_id='+key+'----')
print('----'+'用户的sec_id='+key+'----\r')
else:
print('----为您下载单个视频----\r')
print(r.url)
Expand All @@ -139,17 +139,14 @@ def judge_link(self):
def get_data(self,api_post_url,max_cursor):
#尝试次数
index = 0

#存储api数据
result = []
while result == []:
index += 1
print('----正在进行第 %d 次尝试----\r' % index)
time.sleep(0.3)
response = requests.get(url = api_post_url,headers=self.headers)
#print(api_post_url)
response = requests.get(url = api_post_url,headers = self.headers)
html = json.loads(response.content.decode())
#print(html)
if self.end == False:
#下一页值
self.nickname = html['aweme_list'][0]['author']['nickname']
Expand All @@ -165,13 +162,11 @@ def get_data(self,api_post_url,max_cursor):
self.next_data(max_cursor)
#self.end = True
print('----此页无数据,为您跳过----\r')


return result,max_cursor

#下一页
def next_data(self,max_cursor):

#获取解码后原地址
r = requests.get(url = self.Find(self.uid)[0])

Expand All @@ -182,10 +177,13 @@ def next_data(self,max_cursor):

#构造下一次访问链接
api_naxt_post_url = 'https://www.iesdouyin.com/web/api/v2/aweme/%s/?sec_uid=%s&count=%s&max_cursor=%s&aid=1128&_signature=RuMN1wAAJu7w0.6HdIeO2EbjDc&dytk=' % (self.mode,key,str(self.count),max_cursor)

index = 0
result = []
while result == []:
while self.end == False:
#回到首页,则结束
if max_cursor == 0:
self.end = True
return
index += 1
print('----正在对',max_cursor,'页进行第 %d 次尝试----\r' % index)
time.sleep(0.3)
Expand All @@ -201,12 +199,10 @@ def next_data(self,max_cursor):
else:
self.end == True
print('----',max_cursor,'页抓获数据失败----\r')
#print('----',max_cursor,'页抓获数据失败----\r')
#sys.exit()

#处理视频信息
def video_info(self,result,max_cursor):

#作者信息
author_list = []

Expand All @@ -220,107 +216,86 @@ def video_info(self,result,max_cursor):
nickname = []

#封面大图
dynamic_cover = []
#dynamic_cover = []

for i2 in range(self.count):
try:
author_list.append(str(result[i2]['desc']))
video_list.append(str(result[i2]['video']['play_addr']['url_list'][0]))
aweme_id.append(str(result[i2]['aweme_id']))
nickname.append(str(result[i2]['author']['nickname']))
dynamic_cover.append(str(result[i2]['video']['dynamic_cover']['url_list'][0]))
#dynamic_cover.append(str(result[i2]['video']['dynamic_cover']['url_list'][0]))
except Exception as error:
pass
#print(error)
#input('视频信息处理失败...')
#sys.exit()
self.videos_download(author_list,video_list,aweme_id,nickname,dynamic_cover,max_cursor)
return self,author_list,video_list,aweme_id,nickname,dynamic_cover,max_cursor
pass
self.videos_download(author_list,video_list,aweme_id,nickname,max_cursor)
return self,author_list,video_list,aweme_id,nickname,max_cursor

def videos_download(self,author_list,video_list,aweme_id,nickname,dynamic_cover,max_cursor):
def videos_download(self,author_list,video_list,aweme_id,nickname,max_cursor):
for i in range(self.count):
try:
#创建并检测下载目录是否存在
os.makedirs(self.save + self.mode + "\\" + nickname[i])
except:
#有目录不再创建
pass

try:
jx_url = f'https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids={aweme_id[i]}' #官方接口
js = json.loads(requests.get(url = jx_url,headers=self.headers).text)
music_url = str(js['item_list'][0]['music']['play_url']['url_list'][0])
music_title = str(js['item_list'][0]['music']['author'])
if self.musicarg == "yes":
#保留音频
music=requests.get(music_url)
#保存视频
start = time.time() #下载开始时间
size = 0 #初始化已下载大小
chunk_size = 1024 #每次下载的数据大小
if self.musicarg == "yes": #保留音频
music=requests.get(music_url) #保存音频
start = time.time() #下载开始时间
size = 0 #初始化已下载大小
chunk_size = 1024 #每次下载的数据大小
content_size = int(music.headers['content-length']) # 下载文件总大小
try:
if music.status_code == 200: #判断是否响应成功
if music.status_code == 200: #判断是否响应成功
print('[ 音频 ]:'+author_list[i]+'[文件 大小]:{size:.2f} MB'.format(size = content_size / chunk_size /1024)) #开始下载,显示下载文件大小
m_url = self.save + self.mode + "\\" + nickname[i] + '\\' + re.sub(r'[\\/:*?"<>|\r\n]+', "_", music_title) + '_' + author_list[i] + '.mp3'
with open(m_url,'wb') as file: #显示进度条
with open(m_url,'wb') as file: #显示进度条
for data in music.iter_content(chunk_size = chunk_size):
file.write(data)
size +=len(data)
print('\r'+'[下载进度]:%s%.2f%%' % ('>'*int(size*50/ content_size), float(size / content_size * 100)) ,end=' ')
end = time.time() #下载结束时间
end = time.time() #下载结束时间
print('\n' + '[下载完成]:耗时: %.2f秒\n' % (end - start)) #输出下载用时时间
except:
input('下载音频出错!')
#print('音频 ',music_title,'-',author_list[i],' 下载中\r')
#m_url = self.save + self.mode + "\\" + nickname[i] + '\\' + re.sub(r'[\\/:*?"<>|\r\n]+', "_", music_title) + '_' + author_list[i] + '.mp3'
#print(m_url)
#with open(m_url,'wb') as f:
# f.write(music.content)
input('下载音频出错!\r')
except Exception as error:
#print(error)
#if music_url == '':
print('该页视频没有'+str(self.count)+'个,已为您跳过')
print('该页音频没有'+str(self.count)+'个,已为您跳过\r')
break
#print('该音频目前不可用\r')
#else:
# pass

try:
video = requests.get(video_list[i])
#保存视频
start = time.time() #下载开始时间
size = 0 #初始化已下载大小
chunk_size = 1024 #每次下载的数据大小
video = requests.get(video_list[i]) #保存视频
start = time.time() #下载开始时间
size = 0 #初始化已下载大小
chunk_size = 1024 #每次下载的数据大小
content_size = int(video.headers['content-length']) # 下载文件总大小
try:
if video.status_code == 200: #判断是否响应成功
if video.status_code == 200: #判断是否响应成功
print('[ 视频 ]:'+author_list[i]+'[文件 大小]:{size:.2f} MB'.format(size = content_size / chunk_size /1024)) #开始下载,显示下载文件大小
v_url = self.save + self.mode + "\\" + nickname[i] + '\\' + re.sub(r'[\\/:*?"<>|\r\n]+', "_", author_list[i]) + '.mp4'
with open(v_url,'wb') as file: #显示进度条
with open(v_url,'wb') as file: #显示进度条
for data in video.iter_content(chunk_size = chunk_size):
file.write(data)
size +=len(data)
print('\r'+'[下载进度]:%s%.2f%%' % ('>'*int(size*50/ content_size), float(size / content_size * 100)) ,end=' ')
end = time.time() #下载结束时间
end = time.time() #下载结束时间
print('\n' + '[下载完成]:耗时: %.2f秒\n' % (end - start)) #输出下载用时时间
except:
input('下载视频出错!')
#print('视频 ',author_list[i],' 下载中\r')
#v_url = self.save + self.mode + "\\" + nickname[i] + '\\' + re.sub(r'[\\/:*?"<>|\r\n]+', "_", author_list[i]) + '.mp4'
#with open(v_url,'wb') as f:
# f.write(video.content)

#保存视频动态封面
#dynamic = requests.get(dynamic_cover[i])
#with open(self.save + self.mode + '\\'+ nickname[i] + '\\' + re.sub(r'[\\/:*?"<>|\r\n]+', "_", author_list[i]) + '.webp','wb') as f:
# f.write(dynamic.content)
input('下载视频出错!\r')
except Exception as error:
#pass
print(error)
input('缓存失败,请检查!')
#sys.exit()
#print(error)
print('该页视频没有'+str(self.count)+'个,已为您跳过\r')
break
self.next_data(max_cursor)

#主模块执行
if __name__ == "__main__":
RTK = TikTok()
input('[ 完成 ]:已完成批量下载,输入任意键后退出')
input('[ 完成 ]:已完成批量下载,输入任意键后退出:')
sys.exit()

0 comments on commit d66350b

Please sign in to comment.