1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| import requests from tqdm import tqdm import os import base64 from cryptography.fernet import Fernet import aiohttp import asyncio import uuid import uvloop from pymysql import * try: import uvloop asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) except ImportError: pass
def downloadByUrl(url, downloadPath, fileName): dst = downloadPath + fileName '''同步''' response = requests.get(url, stream=True) file_size = int(response.headers['content-length']) if os.path.exists(dst): first_byte = os.path.getsize(dst) else: first_byte = 0 if first_byte >= file_size: return file_size header = {"Range": f"bytes={first_byte}-{file_size}"} pbar = tqdm( total=file_size, initial=first_byte, unit='B', unit_scale=True, desc=dst) req = requests.get(url, headers=header, stream=True) with(open(dst, 'ab')) as f: for chunk in req.iter_content(chunk_size=1024): if chunk: f.write(chunk) pbar.update(1024) pbar.close() return file_size
async def async_download_from_url(url, downloadPath, fileName): totalPath = downloadPath + fileName '''异步''' async with aiohttp.connector.TCPConnector(limit=300, force_close=True, enable_cleanup_closed=True) as tc: async with aiohttp.ClientSession(connector=tc) as session: req = await fetch(session, url, totalPath) file_size = int(req.headers['content-length']) print(f"获取视频总长度:{file_size}") if os.path.exists(totalPath): first_byte = os.path.getsize(totalPath) else: first_byte = 0 if first_byte >= file_size: return file_size header = {"Range": f"bytes={first_byte}-{file_size}"} pbar = tqdm( total=file_size, initial=first_byte, unit='B', unit_scale=True, desc=totalPath) await fetch(session, url, totalPath, pbar=pbar, headers=header) async def fetch(session, url, dst, pbar=None, headers=None): if headers: async with session.get(url, headers=headers) as req: with(open(dst, 'ab')) as f: while True: chunk = await req.content.read(1024) if not chunk: break f.write(chunk) pbar.update(1024) pbar.close() else: async with session.get(url) as req: return req if __name__ == '__main__': downloadPath = "/Users/renyuxin/Downloads/" urlData = [ "https://ryx-smart.oss-cn-beijing.aliyuncs.com/app/video/girl-video/31350AAACBA0460B81E06E8C5D40A04D.mp4", "https://ryx-smart.oss-cn-beijing.aliyuncs.com/app/video/girl-video/45F972192C1E4332B575DD22FB197306.mp4" ] for item in urlData: fileName = f"{uuid.uuid1()}.mp4" downloadByUrl(urlData[0], downloadPath, fileName); print(" =============================================== ") for index in range(len(urlData)): fileName = f"{uuid.uuid1()}.mp4" task = [asyncio.ensure_future(async_download_from_url(urlData[index], downloadPath, fileName))] try: loop = asyncio.get_event_loop() loop.run_until_complete(asyncio.wait(task)) except: loop.run_until_complete(loop.shutdown_asyncgens()) loop.close();
|