🎨 Pass download path in parameter

This commit is contained in:
zhbaor 2023-02-20 19:24:48 +08:00
parent 65d75815f1
commit 02a1fbc554

View file

@ -1,6 +1,7 @@
from exceptions import *
from bilibili_api import video, sync, HEADERS
import httpx
import os
class Video:
@ -54,14 +55,13 @@ class Video:
)
self.url = streams[1].url
def download_video(self):
def download(self, file: int | str | bytes | os.PathLike):
with httpx.stream("GET", self.url, headers=HEADERS) as r:
self.length = int(r.headers["content-length"])
self.received_bytes = 0
with open("demo.m4a", "wb") as f:
with open(file, "wb") as f:
for chunk in r.iter_bytes(1024):
if not chunk:
break
self.received_bytes += len(chunk)
f.write(chunk)
print(f"downloaded {int(self.received_bytes / self.length * 100)}%")