bilibili.py 8.0 KB
Newer Older
H
Hsury 已提交
1
#!/usr/bin/env python3.7
H
Hsury 已提交
2 3 4 5 6 7 8 9
# -*- coding: utf-8 -*-

import base64
import hashlib
import random
import requests
import rsa
import time
W
wizardforcel 已提交
10
import re
H
Hsury 已提交
11
from urllib import parse
W
wizardforcel 已提交
12
from BiliDriveEx.util import *
H
Hsury 已提交
13 14 15 16

class Bilibili:
    app_key = "1d8b6e7d45233436"

W
wizardforcel 已提交
17
    default_hdrs = {'User-Agent': "Mozilla/5.0 BiliDroid/5.51.1 (bbcallen@gmail.com)"}
W
wizardforcel 已提交
18 19 20 21
    
    default_url = lambda self, sha1: f"http://i0.hdslb.com/bfs/album/{sha1}.png"
    meta_string = lambda self, url: ("bdex://" + re.findall(r"[a-fA-F0-9]{40}", url)[0]) if re.match(r"^http(s?)://i0.hdslb.com/bfs/album/[a-fA-F0-9]{40}.png$", url) else url
    
W
wizardforcel 已提交
22
    get_cookies = lambda self: self.cookies
W
wizardforcel 已提交
23
    
H
Hsury 已提交
24
    def __init__(self):
W
wizardforcel 已提交
25
        self.cookies = {}
W
wizardforcel 已提交
26
        self.load_cookies()
H
Hsury 已提交
27

W
wizardforcel 已提交
28 29 30 31 32 33 34 35 36
    def set_cookies(self, cookie_str):
        self.cookies = {}
        for kv in cookie_str.split('; '):
            kv = kv.split('=')
            if len(kv) != 2: continue
            self.cookies[kv[0]] = kv[1]
        self.save_cookies()
            
        
H
Hsury 已提交
37 38 39
    def _solve_captcha(self, image):
        url = "https://bili.dev:2233/captcha"
        payload = {'image': base64.b64encode(image).decode("utf-8")}
W
wizardforcel 已提交
40 41 42 43
        response = request_retry("post", url, 
            headers=Bilibili.default_hdrs,
            json=payload
        ).json()
H
Hsury 已提交
44 45 46 47 48 49 50 51 52
        return response['message'] if response and response.get("code") == 0 else None

    @staticmethod
    def calc_sign(param):
        salt = "560c52ccd288fed045859ed18bffd973"
        sign_hash = hashlib.md5()
        sign_hash.update(f"{param}{salt}".encode())
        return sign_hash.hexdigest()

W
wizardforcel 已提交
53 54 55 56 57 58 59
        
    def get_key(self):
        url = f"https://passport.bilibili.com/api/oauth2/getKey"
        payload = {
            'appkey': Bilibili.app_key,
            'sign': self.calc_sign(f"appkey={Bilibili.app_key}"),
        }
W
wizardforcel 已提交
60 61 62 63 64 65 66
        r = request_retry("post", url, data=payload, 
            headers=Bilibili.default_hdrs,
            cookies=self.cookies, retry=999999
        )
        for k, v in r.cookies.items(): self.cookies[k] = v
        j = r.json()
        if j and j['code'] == 0:
W
wizardforcel 已提交
67
            return {
W
wizardforcel 已提交
68 69
                'key_hash': j['data']['hash'],
                'pub_key': rsa.PublicKey.load_pkcs1_openssl_pem(j['data']['key'].encode()),
W
wizardforcel 已提交
70 71 72 73 74 75 76 77 78 79 80 81
            }
        
    def login_once(self, username, password, captcha=None):
        key = self.get_key()
        key_hash, pub_key = key['key_hash'], key['pub_key']
        username = parse.quote_plus(username)
        password = parse.quote_plus(base64.b64encode(rsa.encrypt(f'{key_hash}{password}'.encode(), pub_key)))
        url = f"https://passport.bilibili.com/api/v2/oauth2/login"
        param = f"appkey={Bilibili.app_key}"
        if captcha: param += f'&captcha={captcha}'
        param += f"&password={password}&username={username}"
        payload = f"{param}&sign={self.calc_sign(param)}"
W
wizardforcel 已提交
82 83
        headers = Bilibili.default_hdrs.copy()
        headers.update({'Content-type': "application/x-www-form-urlencoded"})
W
wizardforcel 已提交
84 85 86 87 88
        j = request_retry("POST", url, data=payload, 
            headers=headers, 
            cookies=self.cookies
        ).json()
        return j
W
wizardforcel 已提交
89 90 91
        
    def get_captcha(self):
        url = f"https://passport.bilibili.com/captcha"
W
wizardforcel 已提交
92 93 94 95 96
        data = request_retry('GET', url, 
            headers=Bilibili.default_hdrs, 
            cookies=self.cookies
        ).content
        return data
W
wizardforcel 已提交
97
        
H
Hsury 已提交
98
    # 登录
H
Hsury 已提交
99 100
    def login(self, username, password):

W
wizardforcel 已提交
101
        captcha = None
H
Hsury 已提交
102
        while True:
W
wizardforcel 已提交
103
            response = self.login_once(username, password, captcha)
W
wizardforcel 已提交
104
            # print(response, self.cookies)
W
wizardforcel 已提交
105 106 107 108 109 110 111 112 113 114 115
            
            if not response or 'code' not in response:
                log(f"当前IP登录过于频繁, 1分钟后重试")
                time.sleep(60)
                continue
                
            if response['code'] == -105:
                response = self.get_captcha()
                captcha = self._solve_captcha(response)
                if captcha:
                    log(f"登录验证码识别结果: {captcha}")
H
Hsury 已提交
116
                else:
W
wizardforcel 已提交
117 118 119 120 121 122 123 124 125
                    log(f"登录验证码识别服务暂时不可用, 10秒后重试")
                    time.sleep(10)
                continue
                
            if response['code'] == -449:
                time.sleep(1)
                continue
            
            if response['code'] == 0 and response['data']['status'] == 0:
W
wizardforcel 已提交
126
                self.cookies = {}
W
wizardforcel 已提交
127
                for cookie in response['data']['cookie_info']['cookies']:
W
wizardforcel 已提交
128
                    self.cookies[cookie['name']] = cookie['value']
W
wizardforcel 已提交
129
                log("登录成功")
W
wizardforcel 已提交
130
                self.save_cookies()
W
wizardforcel 已提交
131 132 133 134 135
                return True
            
            log(f"登录失败 {response}")
            return False

H
Hsury 已提交
136 137

    # 获取用户信息
W
wizardforcel 已提交
138
    def get_user_info(self, fmt=True):
W
wizardforcel 已提交
139
        url = f"https://api.bilibili.com/x/space/myinfo"
H
Hsury 已提交
140
        headers = {
W
wizardforcel 已提交
141
            'Referer': f"https://space.bilibili.com",
H
Hsury 已提交
142
        }
W
wizardforcel 已提交
143 144 145 146 147 148
        response = request_retry("get", url, 
            headers=headers, 
            cookies=self.cookies
        ).json()
        
        if not response or response.get("code") != 0:
W
wizardforcel 已提交
149
            return
W
wizardforcel 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
        
        info = {
            'ban': False,
            'coins': 0,
            'experience': {
                'current': 0,
                'next': 0,
            },
            'face': "",
            'level': 0,
            'nickname': "",
            'uid': 0,
        }
        info['ban'] = bool(response['data']['silence'])
        info['coins'] = response['data']['coins']
        info['experience']['current'] = response['data']['level_exp']['current_exp']
        info['experience']['next'] = response['data']['level_exp']['next_exp']
        info['face'] = response['data']['face']
        info['level'] = response['data']['level']
        info['nickname'] = response['data']['name']
        info['uid'] = response['data']['mid']
W
wizardforcel 已提交
171 172 173 174 175
        
        if fmt:
            return f"{info['nickname']}(UID={info['uid']}), Lv.{info['level']}({info['experience']['current']}/{info['experience']['next']}), 拥有{info['coins']}枚硬币, 账号{'状态正常' if not info['ban'] else '被封禁'}"
        else:
            return info
W
wizardforcel 已提交
176
            
W
wizardforcel 已提交
177 178 179 180 181 182 183 184 185 186
    def save_cookies(self):
        with open(os.path.join(bundle_dir, "cookies.json"), "w", encoding="utf-8") as f:
            f.write(json.dumps(self.cookies, ensure_ascii=False, indent=2))
            
    def load_cookies(self):
        try:
            with open(os.path.join(bundle_dir, "cookies.json"), "r", encoding="utf-8") as f:
                self.cookies = json.loads(f.read())
        except:
            pass
W
wizardforcel 已提交
187 188
            
    def exist(self, sha1):
W
wizardforcel 已提交
189 190 191 192 193 194 195 196 197
        try:
            url = self.default_url(sha1)
            headers = {
                'Referer': "http://t.bilibili.com/",
                'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36",
            }
            res = request_retry('HEAD', url, headers=headers, timeout=10)
            return url if res.status_code == 200 else None
        except:
W
wizardforcel 已提交
198
            return
W
wizardforcel 已提交
199
                
W
wizardforcel 已提交
200
        
W
wizardforcel 已提交
201
    def image_upload(self, data):
W
wizardforcel 已提交
202 203 204
        sha1 = calc_sha1(data)
        url = self.exist(sha1)
        if url: return {'code': 0, 'data': {'image_url': url}}
W
wizardforcel 已提交
205
        
W
wizardforcel 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219
        url = "https://api.vc.bilibili.com/api/v1/drawImage/upload"
        headers = {
            'Origin': "https://t.bilibili.com",
            'Referer': "https://t.bilibili.com/",
            'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36",
        }
        files = {
            'file_up': (f"{int(time.time() * 1000)}.png", data),
        }
        data = {
            'biz': "draw",
            'category': "daily",
        }
        try:
W
wizardforcel 已提交
220 221 222 223 224
            response = requests.post(url, data=data, 
                headers=headers, 
                cookies=self.cookies, 
                files=files, timeout=300
            ).json()
W
wizardforcel 已提交
225 226 227
        except:
            response = None
        return response