Skip to content

Commit

Permalink
哔哩哔哩接口: 随机化 api.bilibili.com 相关业务域名
Browse files Browse the repository at this point in the history
inspired by nilaoda/BBDown#915
  • Loading branch information
Just-Prog committed Aug 5, 2024
1 parent afbd364 commit 11b25aa
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
5 changes: 3 additions & 2 deletions bili/bili_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import requests
from bili.bili_wbi import getWBI
from bili.bili_domain import randomDomain

bili_headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
Expand Down Expand Up @@ -207,7 +208,7 @@ def get_dynamic_list(self, host_mid, offset='') -> dict:
'host_mid': host_mid,
'offset': offset
}
resp = requests.get('https://api.bilibili.com/x/polymer/web-dynamic/v1/feed/space',
resp = requests.get('https://'+randomDomain()+'/x/polymer/web-dynamic/v1/feed/space',
headers=self.headers, params=params, cookies=self.cookies).json()
if resp.get('code') == -352:
raise Exception("哔哩哔哩接口风控")
Expand All @@ -221,7 +222,7 @@ def get_user_info(self, uid):
:return: 从哔哩哔哩获取的源数据,详见 https://socialsisteryi.github.io/bilibili-API-collect/docs/user/info.html
"""
params = getWBI({'mid': uid})
resp = requests.get('https://api.bilibili.com/x/space/wbi/acc/info', params=params,
resp = requests.get('https://'+randomDomain()+'/x/space/wbi/acc/info', params=params,
headers=self.headers, cookies=self.cookies).json()
if resp.get("code") != 0:
raise Exception(resp.get("message"))
Expand Down
10 changes: 10 additions & 0 deletions bili/bili_domain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import random

def randomDomain():
bili_api = [
'api.bilibili.com',
'api.biliapi.com',
'api.biliapi.net'
]
base_url = random.choice(bili_api)
return base_url
3 changes: 2 additions & 1 deletion bili/bili_wbi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import urllib.parse
import time
import requests
from bili.bili_domain import randomDomain

mixinKeyEncTab = [
46, 47, 18, 2, 53, 8, 23, 32, 15, 50, 10, 31, 58, 3, 45, 35, 27, 43, 5, 49,
Expand Down Expand Up @@ -50,7 +51,7 @@ def getWbiKeys() -> tuple[str, str]:
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
'Referer': 'https://www.bilibili.com/'
}
resp = requests.get('https://api.bilibili.com/x/web-interface/nav', headers=headers)
resp = requests.get('https://'+randomDomain()+'/x/web-interface/nav', headers=headers)
resp.raise_for_status()
json_content = resp.json()
img_url: str = json_content['data']['wbi_img']['img_url']
Expand Down

0 comments on commit 11b25aa

Please sign in to comment.