✨ Download manager
This commit is contained in:
parent
8077378510
commit
23c8f3884d
6 changed files with 76 additions and 94 deletions
|
@ -1,11 +1,15 @@
|
|||
from exceptions import *
|
||||
from .exceptions import *
|
||||
from bilibili_api import video, sync, HEADERS
|
||||
import httpx
|
||||
import os
|
||||
from httpx import stream
|
||||
from os import PathLike
|
||||
|
||||
|
||||
class Video:
|
||||
def __init__(self, video_number: str):
|
||||
self.title = None
|
||||
self.author = None
|
||||
self.received_bytes = 0
|
||||
self.length = 0
|
||||
video_number = video_number.strip()
|
||||
if len(video_number) <= 2:
|
||||
raise InvalidVideoNumberException(
|
||||
|
@ -17,23 +21,13 @@ class Video:
|
|||
f"Invalid AV video number {video_number}!"
|
||||
)
|
||||
else:
|
||||
self.v = video.Video(aid="AV" + video_number[2:])
|
||||
self.number = "AV" + video_number[2:]
|
||||
self.v = video.Video(aid=self.number)
|
||||
if video_number[:2].upper() != "BV":
|
||||
raise InvalidVideoNumberException(f"Invalid video number {video_number}!")
|
||||
else:
|
||||
self.v = video.Video(bvid="BV" + video_number[2:])
|
||||
|
||||
try:
|
||||
download_url_data = sync(self.v.get_download_url(0))
|
||||
detecter = video.VideoDownloadURLDataDetecter(data=download_url_data)
|
||||
streams = detecter.detect_best_streams()
|
||||
self.url = streams[1].url
|
||||
except:
|
||||
raise BiliBiliAPIException("Error happens with bilibili-api-python.")
|
||||
if detecter.check_flv_stream() == True:
|
||||
raise BadVideoException(
|
||||
f"This video ({self.title})) is only available in flv format."
|
||||
)
|
||||
self.number = "BV" + video_number[2:]
|
||||
self.v = video.Video(bvid=self.number)
|
||||
|
||||
def get_info(self):
|
||||
try:
|
||||
|
@ -41,7 +35,7 @@ class Video:
|
|||
except:
|
||||
raise BiliBiliAPIException("Error happens with bilibili-api-python.")
|
||||
self.title = info["title"]
|
||||
self.author = info["owner"]
|
||||
self.author = info["owner"]["name"]
|
||||
|
||||
def get_url(self):
|
||||
try:
|
||||
|
@ -54,10 +48,10 @@ class Video:
|
|||
raise BadVideoException(
|
||||
f"This video ({self.title})) is only available in flv format."
|
||||
)
|
||||
self.url = streams[1].url
|
||||
return streams[1].url
|
||||
|
||||
def download(self, file: int | str | bytes | os.PathLike):
|
||||
with httpx.stream("GET", self.url, headers=HEADERS) as r:
|
||||
def download(self, file: int | str | bytes | PathLike):
|
||||
with stream("GET", self.get_url(), headers=HEADERS) as r:
|
||||
self.length = int(r.headers["content-length"])
|
||||
self.received_bytes = 0
|
||||
with open(file, "wb") as f:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue