import os
import requests
import time
import random
import string


def download_video(url):
response = requests.get(url)
data = response.content
paths = time.strftime("%Y-%m-%d", time.localtime())
if data:
try:
savePath = os.getcwd() + '/' + paths
if not os.path.exists(savePath): # 判断目标路径下是否包含目标文件夹
os.makedirs(savePath) # 没有则新建该文件夹
file_path = '{}/{}.{}'.format(savePath, ran(), 'mp4')
if not os.path.exists(file_path):
with open(file_path, 'wb') as f:
f.write(data)
f.close()
except Exception:
print('处理错误跳过')


# 生成随机8位文件名
def ran():
salt = ''.join(random.sample(string.ascii_letters + string.digits, 8)) # 随机输出8位由英文字符和数字组成的字符串
return salt


if __name__ == "__main__":
i = 1
while True:
url = requests.get("http://********哈哈坏不坏.com/get.php").text
download_video(url)
index = random.randint(3, 5)
i = (i+1)
print('处理完成第' + str(i) + '个视频')
time.sleep(index)