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