-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
"""download bilibili video series, 视频列表""" | ||
|
||
import typer | ||
from .util import request | ||
|
||
headers: dict[str, str] = { | ||
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36", | ||
"Referer": "https://www.bilibili.com", | ||
} | ||
|
||
|
||
class Series: | ||
series_url: str = "https://api.bilibili.com/x/series/archives" | ||
retry: int = 3 | ||
|
||
def __init__(self, uid: str, series_id: str, page_size=30) -> None: | ||
self.uid = uid | ||
self.series_id = series_id | ||
self.page_size = page_size | ||
self.videos = [] | ||
self.total = 0 | ||
|
||
def get_videos(self): | ||
params = { | ||
"mid": self.uid, | ||
"series_id": self.series_id, | ||
"pn": 1, | ||
"ps": self.page_size, | ||
"current_id": self.uid, | ||
} | ||
|
||
def wrapped_request(): | ||
"""wrap request with retry""" | ||
for _ in range(self.retry): | ||
res = request( | ||
method="get", url=self.series_url, params=params, headers=headers | ||
).json() | ||
if self.__response_succeed(res): | ||
return res | ||
self.__handle_error_response(res) | ||
return None | ||
|
||
res = wrapped_request() | ||
if res is None: | ||
return False | ||
|
||
self.total = res["data"]["page"]["total"] | ||
|
||
for i in range(1, self.total // self.page_size + 2): | ||
params["pn"] = i | ||
res = wrapped_request() | ||
if res: | ||
bvids = [ar["bvid"] for ar in res["data"]["archives"]] | ||
self.videos.extend(bvids) | ||
else: | ||
typer.echo( | ||
f"skip audios from {(i-1)* self.page_size} to {i * self.page_size}" | ||
) | ||
|
||
return True | ||
|
||
def __handle_error_response(self, response): | ||
try: | ||
archives = response["data"]["archives"] | ||
except KeyError: | ||
archives = 0 # something null not none | ||
if archives is None: | ||
typer.echo(f"Error: uid {self.uid} or sid {self.series_id} error.") | ||
else: | ||
typer.echo("Error: Unknown problem.") | ||
typer.echo(f"resp: {response}") | ||
|
||
def __response_succeed(self, response) -> bool: | ||
try: | ||
return response["data"]["archives"] is not None | ||
except KeyError: | ||
return False |