Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bilibili] bilibili User Articles FavList support #6781

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions gallery_dl/extractor/bilibili.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,31 @@ def items(self):
yield Message.Url, url, text.nameext_from_url(url, article)


class BilibiliUserArticleFavlistExtractor(BilibiliExtractor):
subcategory = "user-article-favlist"
pattern = (r"(?:https?://)?(?:space.bilibili.com)"
r"/(\d+)/(?:favlist\?fid=opus)"
r"(?:&ftype=opus)?")
example = "https://space.bilibili.com/123456/favlist?fid=opus"
_warning = True

def _init(self):
BilibiliExtractor._init(self)
if self._warning:
if not self.cookies_check(("SESSDATA",)):
self.log.warning("no 'SESSDATA' cookie set")
BilibiliUserArticleFavlistExtractor._warning = False

def items(self):
mid = self.api.login_user_id()
if str(mid) != self.groups[0]:
raise exception.StopExtraction("This is not you favlist url!")
for article in self.api.user_favlist():
article["_extractor"] = BilibiliArticleExtractor
url = "{}/opus/{}".format(self.root, article["opus_id"])
yield Message.Queue, url, article


class BilibiliAPI():
def __init__(self, extractor):
self.extractor = extractor
Expand Down Expand Up @@ -122,3 +147,30 @@ def article(self, article_id):
raise exception.StopExtraction(
"%s: Unable to extract INITIAL_STATE data", article_id)
self.extractor.wait(seconds=300)

def user_favlist(self):
endpoint = "/opus/feed/fav"
params = {"page": 1, "page_size": 20}

while True:
data = self._call(endpoint, params)

for item in data["data"]["items"]:
yield item

if not data["data"]["has_more"]:
break

params["page"] = params["page"] + 1

def login_user_id(self):
url = "https://api.bilibili.com/x/space/v2/myinfo"
data = self.extractor.request(url).json()

if data["code"] != 0:
self.extractor.log.debug("Server response: %s", data)
raise exception.StopExtraction("API request failed,Are you login?")
try:
return data["data"]["profile"]["mid"]
except Exception:
raise exception.StopExtraction("API request failed")
Loading