Skip to content

Commit

Permalink
fix: add w_webid risk control check
Browse files Browse the repository at this point in the history
Signed-off-by: jingfelix <[email protected]>
  • Loading branch information
jingfelix committed Oct 27, 2024
1 parent 807eb59 commit b0b86fb
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
6 changes: 2 additions & 4 deletions src/bilifm/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ def season(
sea = Season(uid, sid)
audio_generator = sea.get_videos()
if not audio_generator:
typer.Exit(1)
return
raise typer.Exit(1)

if not os.path.isdir(sea.name):
os.makedirs(sea.name)
Expand Down Expand Up @@ -108,8 +107,7 @@ def series(
ser = Series(uid, sid)
audio_generator = ser.get_videos()
if not audio_generator:
typer.Exit(1)
return
raise typer.Exit(1)

for audios in audio_generator:
for id in audios:
Expand Down
29 changes: 25 additions & 4 deletions src/bilifm/user.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
import typer

from .util import request
from .util import get_w_webid, request


class User:
uidUrl: str = "https://api.bilibili.com/x/space/wbi/arc/search"
uid: str = ""
videos: list = []

def __init__(self, uid: str):
params = {"mid": uid, "ps": 1, "tid": 0, "pn": 1, "order": "pubdate"}
self.uid = uid

w_webid = get_w_webid(uid)

params = {
"mid": uid,
"ps": 1,
"tid": 0,
"pn": 1,
"order": "pubdate",
"web_location": "333.999",
"w_webid": w_webid,
}

response = request(
method="get", url=self.uidUrl, params=params, wbi=True, dm=True
Expand All @@ -18,7 +31,7 @@ def __init__(self, uid: str):

if code != 0:
typer.echo(f"Error: uid {uid} not found")
return
raise typer.Exit(1)

total = response.json()["data"]["page"]["count"]

Expand All @@ -29,7 +42,15 @@ def __init__(self, uid: str):
for i in range(1, max_pn + 2):
ps = 50 if i != max_pn + 1 else surpus

params = {"mid": uid, "ps": ps, "tid": 0, "pn": i, "order": "pubdate"}
params = {
"mid": uid,
"ps": ps,
"tid": 0,
"pn": i,
"order": "pubdate",
"web_location": "333.999",
"w_webid": w_webid,
}

response = request(
method="get", url=self.uidUrl, params=params, wbi=True, dm=True
Expand Down
40 changes: 40 additions & 0 deletions src/bilifm/util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import json
import os
import random
import re
import time
import urllib.parse
from enum import Enum
Expand Down Expand Up @@ -176,6 +178,44 @@ def gen_dm_args(params: dict):
return params


def get_w_webid(uid: str):
"""reference: https://github.com/SocialSisterYi/bilibili-API-collect/issues/1107#issuecomment-2424269364"""

dynamic_url = f"https://space.bilibili.com/{uid}/dynamic"

# TODO: 简化所需的 headers 设置
headers = {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"accept-language": "zh-CN,zh;q=0.9",
"cache-control": "no-cache",
"dnt": "1",
"pragma": "no-cache",
"priority": "u=0, i",
"sec-ch-ua": '"Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": '"macOS"',
"sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "none",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36",
}

text = requests.get(dynamic_url, headers=headers).text

# <script id="__RENDER_DATA__" type="application/json">xxx</script>
__RENDER_DATA__ = re.search(
r"<script id=\"__RENDER_DATA__\" type=\"application/json\">(.*?)</script>",
text,
re.S,
).group(1)

access_id = json.loads(urllib.parse.unquote(__RENDER_DATA__))["access_id"]

return access_id


def request(
method: str,
url: str,
Expand Down

0 comments on commit b0b86fb

Please sign in to comment.