bilibili.py 7.4 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 28 29 30

    def _solve_captcha(self, image):
        url = "https://bili.dev:2233/captcha"
        payload = {'image': base64.b64encode(image).decode("utf-8")}
W
wizardforcel 已提交
31 32 33 34
        response = request_retry("post", url, 
            headers=Bilibili.default_hdrs,
            json=payload
        ).json()
H
Hsury 已提交
35 36 37 38 39 40 41 42 43
        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 已提交
44 45 46 47 48 49 50
        
    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 已提交
51 52 53 54 55 56 57
        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 已提交
58
            return {
W
wizardforcel 已提交
59 60
                'key_hash': j['data']['hash'],
                'pub_key': rsa.PublicKey.load_pkcs1_openssl_pem(j['data']['key'].encode()),
W
wizardforcel 已提交
61 62 63 64 65 66 67 68 69 70 71 72
            }
        
    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 已提交
73 74
        headers = Bilibili.default_hdrs.copy()
        headers.update({'Content-type': "application/x-www-form-urlencoded"})
W
wizardforcel 已提交
75 76 77 78 79
        j = request_retry("POST", url, data=payload, 
            headers=headers, 
            cookies=self.cookies
        ).json()
        return j
W
wizardforcel 已提交
80 81 82
        
    def get_captcha(self):
        url = f"https://passport.bilibili.com/captcha"
W
wizardforcel 已提交
83 84 85 86 87
        data = request_retry('GET', url, 
            headers=Bilibili.default_hdrs, 
            cookies=self.cookies
        ).content
        return data
W
wizardforcel 已提交
88
        
H
Hsury 已提交
89
    # 登录
H
Hsury 已提交
90 91
    def login(self, username, password):

W
wizardforcel 已提交
92
        captcha = None
H
Hsury 已提交
93
        while True:
W
wizardforcel 已提交
94
            response = self.login_once(username, password, captcha)
W
wizardforcel 已提交
95
            # print(response, self.cookies)
W
wizardforcel 已提交
96 97 98 99 100 101 102 103 104 105 106
            
            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 已提交
107
                else:
W
wizardforcel 已提交
108 109 110 111 112 113 114 115 116 117
                    log(f"登录验证码识别服务暂时不可用, 10秒后重试")
                    time.sleep(10)
                continue
                
            if response['code'] == -449:
                time.sleep(1)
                continue
            
            if response['code'] == 0 and response['data']['status'] == 0:
                for cookie in response['data']['cookie_info']['cookies']:
W
wizardforcel 已提交
118
                    self.cookies[cookie['name']] = cookie['value']
W
wizardforcel 已提交
119
                log("登录成功")
W
wizardforcel 已提交
120
                self.save_cookies()
W
wizardforcel 已提交
121 122 123 124 125
                return True
            
            log(f"登录失败 {response}")
            return False

H
Hsury 已提交
126 127 128

    # 获取用户信息
    def get_user_info(self):
H
Hsury 已提交
129
        url = f"https://api.bilibili.com/x/space/myinfo?jsonp=jsonp"
H
Hsury 已提交
130
        headers = {
W
wizardforcel 已提交
131
            'Referer': f"https://space.bilibili.com",
H
Hsury 已提交
132
        }
W
wizardforcel 已提交
133 134 135 136 137 138
        response = request_retry("get", url, 
            headers=headers, 
            cookies=self.cookies
        ).json()
        
        if not response or response.get("code") != 0:
H
Hsury 已提交
139
            return False
W
wizardforcel 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
        
        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']
        return info
            
W
wizardforcel 已提交
163 164 165 166 167 168 169 170 171 172
    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 已提交
173 174
            
    def exist(self, sha1):
W
wizardforcel 已提交
175 176 177 178 179 180 181 182 183 184 185
        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:
            return None
                
W
wizardforcel 已提交
186
        
W
wizardforcel 已提交
187
    def image_upload(self, data):
W
wizardforcel 已提交
188 189 190
        sha1 = calc_sha1(data)
        url = self.exist(sha1)
        if url: return {'code': 0, 'data': {'image_url': url}}
W
wizardforcel 已提交
191
        
W
wizardforcel 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205
        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 已提交
206 207 208 209 210
            response = requests.post(url, data=data, 
                headers=headers, 
                cookies=self.cookies, 
                files=files, timeout=300
            ).json()
W
wizardforcel 已提交
211 212 213
        except:
            response = None
        return response