Skip to content

Commit

Permalink
Update bilibili auth
Browse files Browse the repository at this point in the history
  • Loading branch information
15619 committed Aug 25, 2022
1 parent d93a83b commit 3b33430
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ Show your bili-fans-medals in a github gist
`https://gist.github.com/XXXX/`**`xxxxxxxxxxxxxxxx`**.

2. Create a token with the `gist` scope and copy it. (https://github.com/settings/tokens/new)
3. Login in BiliBili (https://www.bilibili.com) and copy the `cookie` with Developer Tools (`F12`)
3. Get BILI_ACCESS_KEY with [this tool](https://github.com/XiaoMiku01/fansMedalHelper/releases/tag/logintool)

### Setup
1. Fork this repo
2. Create `actions secrets`
- `GH_GIST_ID`: `XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX`
- `GH_TOKEN`: `XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX`
- `BILI_COOKIE`: `XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX`
- `BILI_ACCESS_KEY`: `XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX`
3. Enable GitHub Actions
50 changes: 45 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,55 @@
import os, re
import os
import time
import requests
import hashlib
import urllib.parse


appkey = '4409e2ce8ffd12b8'
appsec = '59b43e04ad6965f34319062b478f83dd'

BILI_ACCESS_KEY = os.environ.get('BILI_ACCESS_KEY', '')


def appsign(params):
params.update({'appkey': appkey})
params = dict(sorted(params.items())) # 重排序参数 key
query = urllib.parse.urlencode(params) # 序列化参数
sign = hashlib.md5((query + appsec).encode()).hexdigest() # 计算 api 签名
params.update({'sign': sign})
return params


def login():
url = "https://app.bilibili.com/x/v2/account/mine"
params = {
"access_key": BILI_ACCESS_KEY,
"ts": int(time.time()),
}
response = requests.get(url, params=appsign(params))
print(response.json())
return response.json()


login_info = login()
if login_info['code']:
print(login_info)
raise Exception("login error")
else:
uid = login_info['data']['mid']


def get_medal_list():
try:
cookie = os.environ.get('BILI_COOKIE', '')
api = 'https://api.live.bilibili.com/xlive/web-ucenter/user/MedalWall'
headers = {'cookie': cookie}
target_id = re.search(r'DedeUserID=(\d+)', cookie).group(1)
params = (('target_id', target_id),)
response = requests.get(api, headers=headers, params=params)
params = {
"access_key": BILI_ACCESS_KEY,
"ts": int(time.time()),
'target_id': uid,
}
response = requests.get(api, headers=headers, params=appsign(params))
print(response.json())
medal_list = response.json()['data']['list'][:5]
return medal_list
Expand Down Expand Up @@ -119,5 +159,5 @@ def update_gist(gist_id, token):
requests.patch(url, headers=headers, json=data)


update_gist(gist_id=GH_GIST_ID, token=GH_TOKEN)
print('\n'.join(l2))
# update_gist(gist_id=GH_GIST_ID, token=GH_TOKEN)

0 comments on commit 3b33430

Please sign in to comment.