Skip to content

Commit f59a178

Browse files
authored
simple fix
1 parent 01c82a7 commit f59a178

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

douyin_pro_2.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
# -*- coding:utf-8 -*-
2-
from splinter.driver.webdriver.chrome import Options, Chrome
3-
from splinter.browser import Browser
42
from contextlib import closing
53
import requests, json, time, re, os, sys, time
6-
from bs4 import BeautifulSoup
4+
from datetime import datetime, timezone
75

86
class DouYin(object):
97
def __init__(self, width = 500, height = 300):
@@ -35,23 +33,25 @@ def get_video_urls(self, user_id):
3533
}
3634
while unique_id != user_id:
3735
search_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
38-
req = requests.get(url = search_url, verify = False)
36+
req = requests.get(search_url, headers=headers)
3937
html = json.loads(req.text)
4038
aweme_count = html['user_list'][0]['user_info']['aweme_count']
4139
uid = html['user_list'][0]['user_info']['uid']
4240
nickname = html['user_list'][0]['user_info']['nickname']
4341
unique_id = html['user_list'][0]['user_info']['unique_id']
44-
user_url = 'https://www.douyin.com/aweme/v1/aweme/post/?user_id=%s&max_cursor=0&count=%s' % (uid, aweme_count)
45-
req = requests.get(url = user_url, headers=headers, verify = False)
42+
user_url = 'https://www.amemv.com/aweme/v1/aweme/post/?user_id=%s&max_cursor=0&count=%s' % (uid, aweme_count)
43+
req = requests.get(user_url, headers=headers)
4644
html = json.loads(req.text)
47-
i = 1
4845
for each in html['aweme_list']:
4946
share_desc = each['share_info']['share_desc']
47+
unix_timestamp = each['create_time']
48+
utc_time = datetime.fromtimestamp(unix_timestamp, timezone.utc)
49+
local_time = utc_time.astimezone()
50+
tc = local_time.strftime('%Y-%m-%d-%H-%M-%S')
5051
if '抖音-原创音乐短视频社区' == share_desc:
51-
video_names.append(str(i) + '.mp4')
52-
i += 1
52+
video_names.append(tc + '.mp4')
5353
else:
54-
video_names.append(share_desc + '.mp4')
54+
video_names.append(tc + '-' + share_desc + '.mp4')
5555
video_urls.append(each['share_info']['share_url'])
5656

5757
return video_names, video_urls, nickname
@@ -64,17 +64,15 @@ def get_download_url(self, video_url, watermark_flag):
6464
Returns:
6565
download_url: 带水印的视频下载地址
6666
"""
67-
req = requests.get(url = video_url, verify = False)
68-
bf = BeautifulSoup(req.text, 'lxml')
69-
script = bf.find_all('script')[-1]
70-
video_url_js = re.findall('var data = \[(.+)\];', str(script))[0]
71-
video_html = json.loads(video_url_js)
67+
req = requests.get(video_url)
68+
_playaddr_re = re.compile(r'playAddr: "(.+)",')
69+
playaddr = _playaddr_re.search(req.text)
7270
# 带水印视频
7371
if watermark_flag == True:
74-
download_url = video_html['video']['play_addr']['url_list'][0]
72+
download_url = playaddr.group(1)
7573
# 无水印视频
7674
else:
77-
download_url = video_html['video']['play_addr']['url_list'][0].replace('playwm','play')
75+
download_url = playaddr.group(1).replace('playwm','play')
7876
return download_url
7977

8078
def video_downloader(self, video_url, video_name, watermark_flag=False):
@@ -89,7 +87,7 @@ def video_downloader(self, video_url, video_name, watermark_flag=False):
8987
"""
9088
size = 0
9189
video_url = self.get_download_url(video_url, watermark_flag=watermark_flag)
92-
with closing(requests.get(video_url, stream=True, verify = False)) as response:
90+
with closing(requests.get(video_url, stream=True)) as response:
9391
chunk_size = 1024
9492
content_size = int(response.headers['content-length'])
9593
if response.status_code == 200:
@@ -127,7 +125,10 @@ def run(self):
127125
video_name = video_names[num].replace('/', '')
128126
else:
129127
video_name = video_names[num]
130-
self.video_downloader(video_urls[num], os.path.join(nickname, video_name), watermark_flag)
128+
if os.path.isfile(os.path.join(nickname, video_name)):
129+
print('视频已存在')
130+
else:
131+
self.video_downloader(video_urls[num], os.path.join(nickname, video_name), watermark_flag)
131132
print('\n')
132133
print('下载完成!')
133134

0 commit comments

Comments
 (0)