Skip to content

Commit

Permalink
merge #6781: [bilibili] add 'user-articles-favorite' extractor (#6725)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikf committed Feb 23, 2025
2 parents fe958ed + e4cc341 commit a9853cd
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/supportedsites.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ Consider all listed sites to potentially be NSFW.
<tr>
<td>Bilibili</td>
<td>https://www.bilibili.com/</td>
<td>Articles, User Articles</td>
<td>Articles, User Articles, User Article Favorites</td>
<td></td>
</tr>
<tr>
Expand Down
46 changes: 46 additions & 0 deletions gallery_dl/extractor/bilibili.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,27 @@ def items(self):
yield Message.Url, url, text.nameext_from_url(url, article)


class BilibiliUserArticlesFavoriteExtractor(BilibiliExtractor):
subcategory = "user-articles-favorite"
pattern = (r"(?:https?://)?space\.bilibili\.com"
r"/(\d+)/favlist\?fid=opus")
example = "https://space.bilibili.com/12345/favlist?fid=opus"
_warning = True

def _init(self):
BilibiliExtractor._init(self)
if self._warning:
if not self.cookies_check(("SESSDATA",)):
self.log.error("'SESSDATA' cookie required")
BilibiliUserArticlesFavoriteExtractor._warning = False

def items(self):
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 +143,28 @@ 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)["data"]

yield from data["items"]

if not data.get("has_more"):
break
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")
1 change: 1 addition & 0 deletions scripts/supportedsites.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@
},
"bilibili": {
"user-articles": "User Articles",
"user-articles-favorite": "User Article Favorites",
},
"bluesky": {
"posts": "",
Expand Down
6 changes: 6 additions & 0 deletions test/results/bilibili.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,10 @@
"#class" : bilibili.BilibiliUserArticlesExtractor,
},

{
"#url" : "https://space.bilibili.com/405279279/favlist?fid=opus",
"#class" : bilibili.BilibiliUserArticlesFavoriteExtractor,
"#auth" : True,
},

)

0 comments on commit a9853cd

Please sign in to comment.