-
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.
Browse files
Browse the repository at this point in the history
* fix: avoid error -352 by replace new header; add retry * feat: add download series * refactor: use retry decorator for request handling * refactor: use generator to prevent excessive collection requests at once * fix: description in README.md Signed-off-by: jingfelix <[email protected]> * fix: help info of `series` command Signed-off-by: jingfelix <[email protected]> --------- Signed-off-by: jingfelix <[email protected]> Co-authored-by: jingfelix <[email protected]>
- Loading branch information
Showing
5 changed files
with
193 additions
and
31 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
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,70 @@ | ||
"""download bilibili video series, 视频列表""" | ||
|
||
import typer | ||
|
||
from .util import Retry, request | ||
|
||
|
||
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): | ||
"""return a generator that contain page_size videos""" | ||
params = { | ||
"mid": self.uid, | ||
"series_id": self.series_id, | ||
"pn": 1, | ||
"ps": self.page_size, | ||
"current_id": self.uid, | ||
} | ||
|
||
@Retry(self.__response_succeed, self.__handle_error_response) | ||
def wrapped_request(): | ||
"""wrap request with retry""" | ||
return request(method="get", url=self.series_url, params=params).json() | ||
|
||
res = wrapped_request() | ||
if res is None: | ||
return 0 | ||
|
||
self.total = res["data"]["page"]["total"] | ||
|
||
def bvid_generator(): | ||
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) | ||
yield bvids | ||
else: | ||
typer.echo( | ||
f"skip audios from {(i-1)* self.page_size} to {i * self.page_size}" | ||
) | ||
|
||
return bvid_generator() | ||
|
||
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 |
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