1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
| import datetime import uuid import base64 import hashlib import time import requests import re import json from Crypto.Cipher import AES from Crypto.Util.Padding import pad from urllib.parse import quote_plus import string import random import time import hashlib import ctypes import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
class BiliBili(object): def __init__(self, aid, bvid, cid, duration, proxies): self.aid, self.bvid, self.cid, self.duration = aid, bvid, cid, duration
self.wifi_mac = self.create_random_mac().upper() self.device_id = self.create_device_id(self.wifi_mac) self.buvid = self.create_buvid_by_wifi() self.session_id = self.create_session_id()
self.build_brand = "HUAWEI" self.build_model = 'Mate 10 Pro'
self.fp_local = self.create_local(self.buvid, self.build_model, "") self.fp_remote = self.create_local(self.buvid, self.build_model, "")
self.app_first_run_time = str(int(time.time()) - random.randint(0, 24 * 60 * 60)) self.ts = str(int(time.time() - 10))
self.session = requests.Session() self.start_ts = None self.heart_beat_session_id = None
def create_random_mac(self, sep=":"): """ 随机生成mac地址 """ data_list = [] for i in range(1, 7): part = "".join(random.sample("0123456789ABCDEF", 2)) data_list.append(part) mac = sep.join(data_list)
return mac
def create_device_id(self, mac): """ 根据mac地址生成 3.device_id :param mac: 传入参数的格式是 00:00:00:00:00 :return: """
def gen_sn(): return "".join(random.sample("123456789" + string.ascii_lowercase, 10))
def base64_encrypt(data_string): data_bytes = bytearray(data_string.encode('utf-8')) data_bytes[0] = data_bytes[0] ^ (len(data_bytes) & 0xFF) for i in range(1, len(data_bytes)): data_bytes[i] = (data_bytes[i - 1] ^ data_bytes[i]) & 0xFF res = base64.encodebytes(bytes(data_bytes)) return res.strip().strip(b"==").decode('utf-8')
mac_str = mac
mac_str = re.sub("[^0-9A-Fa-f]", "", mac_str) mac_str = mac_str.lower()
sn = gen_sn()
total_string = "{}|||{}".format(mac_str, sn) return base64_encrypt(total_string)
def create_buvid_by_wifi(self): """ 基于wifi mac地址生成buvid ( B站app中有四种获取buvid的方式:设备ID、wifi mac地址、3.device_id、uuid ) """ md5 = hashlib.md5() md5.update(self.wifi_mac.encode('utf-8')) v0_1 = md5.hexdigest() return "XY{}{}{}{}".format(v0_1[2], v0_1[12], v0_1[22], v0_1).upper()
def create_session_id(self): return "".join([hex(random.randint(1, 255))[2:] for i in range(4)])
def create_local(self, buvid, phone_model, phone_band): """ fp_local和fp_remote都是用这个算法来生成的,在手机初始化阶段生成 fp_local, :param buvid: 根据算法生成的buvid,例如:"XYBA4F3B2789A879EA8AEEDBE2E4118F78303" :param phone_model: 手机型号modal,例如:"Mate 10 Pro" :param phone_band: 手机品牌band,在模拟器上是空字符串(我猜是程序员想要写成 brand )哈哈哈哈 :return: """
def a_b(arg8): v3 = 0 v4 = 60 v0_1 = 2 v5 = 0 while True: v6 = arg8[v3:v3 + 2] v5 += int(v6, base=16) if v3 != v4: v3 += v0_1 continue break xx = "%02x" % (v5 % 0x100,) return xx
def misc_helper_kt(data_bytes): data_list = [] v7 = len(data_bytes) v0 = 0 while v0 < v7: v2 = data_bytes[v0] data_list.append("%02x" % v2) v0 += 1 return ''.join(data_list)
data_string = "{}{}{}".format(buvid, phone_model, phone_band) hash_object = hashlib.md5() hash_object.update(data_string.encode('utf-8')) data = hash_object.digest()
arg1 = misc_helper_kt(data) arg2 = datetime.datetime.now().strftime("%Y%m%d%H%M%S") arg3 = misc_helper_kt([random.randint(1, 255) for i in range(8)])
str2 = "{}{}{}".format(arg1, arg2, arg3) local = str2 + a_b(str2) return local
def x_report_click_android2(self): SALT = "9cafa6466a028bfb" KEY = "fd6b639dbcff0c2a1b03b389ec763c4b" IV = "77b07a672d57d64c"
def sha_256_encrypt(data_string): sha = hashlib.sha256() sha.update(data_string.encode('utf-8')) sha.update(SALT.encode('utf-8')) return sha.hexdigest()
def aes_encrypt(data_string): aes = AES.new( key=KEY.encode('utf-8'), mode=AES.MODE_CBC, iv=IV.encode('utf-8') ) raw = pad(data_string.encode('utf-8'), 16) return aes.encrypt(raw)
ctime = int(time.time()) info = { 'aid': self.aid, 'cid': self.cid, 'part': 1, 'mid': 0, 'lv': 0, 'ftime': ctime - random.randint(100, 1000), 'stime': ctime, 'did': self.device_id, 'type': 3, 'sub_type': 0, 'sid': '0', 'epid': '', 'auto_play': 0, 'build': 6240300, 'mobi_app': 'android', 'spmid': 'main.ugc-video-detail.0.0', 'from_spmid': 'search.search-result.0.0' } data = "&".join(["{}={}".format(key, info[key]) for key in sorted(info.keys())]) sign = sha_256_encrypt(data).lower() data = "{}&sign={}".format(data, sign) aes_string = aes_encrypt(data)
res = self.session.post( url="https://api.bilibili.com/x/report/click/android2", headers={ "accept-length": "gzip", "content-type": "application/json; charset=utf-8", "app-key": "android", "User-Agent": "Mozilla/5.0 BiliDroid/6.24.0 ([email protected]) os/android model/Mate 10 Pro mobi_app/android build/6240300 channel/bili innerVer/6240300 osVer/6.0.1 network/2", "env": "prod", "buvid": self.buvid, "device-id": self.device_id, "session_id": self.session_id, "fp_local": self.fp_local, "fp_remote": self.fp_remote, }, data=aes_string, timeout=10, verify=False
) res.close()
def get_param_sign_s(self, param_dict): """ :param param_dict: 要签名的参数字典 :return: """ ordered_string = "&".join(["{}={}".format(key, param_dict[key]) for key in sorted(param_dict.keys())]) encrypt_string = ordered_string + "560c52ccd288fed045859ed18bffd973" obj = hashlib.md5(encrypt_string.encode('utf-8')) sign = obj.hexdigest()
return "{}&sign={}".format(ordered_string, sign)
def create_heart_beat_session_id(self): arg0 = str(int(time.time() * 1000)) + str(random.randint(1, 1000000)) hash_object = hashlib.sha1() hash_object.update(arg0.encode('utf-8')) arg7 = hash_object.hexdigest() return arg7
def heart_beat_start(self, aid, cid, duration): self.start_ts = ts = int(time.time() - 10) self.heart_beat_session_id = self.create_heart_beat_session_id() form_dict = { "actual_played_time": "0", "aid": aid, "appkey": "1d8b6e7d45233436", "auto_play": "0", "build": "6240300", "c_locale": "zh_CN", "channel": "bili", "cid": cid, "epid": "0", "epid_status": "", "from": "2", "from_spmid": "main.ugc-video-detail.0.0", "last_play_progress_time": "0", "list_play_time": "0", "max_play_progress_time": "0", "mid": "0", "miniplayer_play_time": "0", "mobi_app": "android", "network_type": "1", "paused_time": "0", "platform": "android", "play_status": "0", "play_type": "1", "played_time": "0", "quality": "32", "s_locale": "zh_CN", "session": self.heart_beat_session_id, "sid": "0", "spmid": "main.ugc-video-detail.0.0", "start_ts": "0", "statistics": quote_plus( json.dumps({"appId": 1, "platform": 3, "version": "6.24.0", "abtest": ""}, separators=(',', ':'))), "sub_type": "0", "total_time": "0", "type": "3", "user_status": "0", "video_duration": duration, "ts": ts, }
total_body_string = self.get_param_sign_s(form_dict)
res = self.session.post( url="https://api.bilibili.com/x/report/heartbeat/mobile", data=total_body_string.encode('utf-8'), headers={ "accept-length": "gzip", "content-type": "application/x-www-form-urlencoded; charset=utf-8", "app-key": "android", "User-Agent": "Mozilla/5.0 BiliDroid/6.24.0 ([email protected]) os/android model/Mate 10 Pro mobi_app/android build/6240300 channel/bili innerVer/6240300 osVer/6.0.1 network/2", "env": "prod", "buvid": self.buvid, "device-id": self.device_id, "session_id": self.session_id, "fp_local": self.fp_local, "fp_remote": self.fp_remote, }, verify=False ) res.close()
def heart_beat_end(self, aid, cid, duration): current_ts = int(time.time())
form_dict = { "actual_played_time": duration, "aid": aid, "appkey": "1d8b6e7d45233436", "auto_play": "0", "build": "6240300", "c_locale": "zh_CN", "channel": "bili", "cid": cid, "epid": "0", "epid_status": "", "from": "2", "from_spmid": "main.ugc-video-detail.0.0", "last_play_progress_time": duration, "list_play_time": "0", "max_play_progress_time": duration, "mid": "0", "miniplayer_play_time": "0", "mobi_app": "android", "network_type": "1", "paused_time": current_ts - self.start_ts - duration, "platform": "android", "play_status": "0", "play_type": "1", "played_time": duration, "quality": "32", "s_locale": "zh_CN", "session": self.heart_beat_session_id, "sid": "0", "spmid": "main.ugc-video-detail.0.0", "start_ts": self.start_ts, "statistics": quote_plus( json.dumps({"appId": 1, "platform": 3, "version": "6.24.0", "abtest": ""}, separators=(',', ':'))), "sub_type": "0", "total_time": current_ts - self.start_ts, "type": "3", "user_status": "0", "video_duration": self.duration, "ts": current_ts, }
total_body_string = self.get_param_sign_s(form_dict)
res = self.session.post( url="https://api.bilibili.com/x/report/heartbeat/mobile", headers={ "accept-length": "gzip", "content-type": "application/x-www-form-urlencoded; charset=utf-8", "app-key": "android", "User-Agent": "Mozilla/5.0 BiliDroid/6.24.0 ([email protected]) os/android model/Mate 10 Pro mobi_app/android build/6240300 channel/bili innerVer/6240300 osVer/6.0.1 network/2", "env": "prod", "buvid": self.buvid, "device-id": self.device_id, "session_id": self.session_id, "fp_local": self.fp_local, "fp_remote": self.fp_remote, }, data=total_body_string.encode('utf-8'), verify=False ) res.close()
def get_video_id_info(exec_url, proxies): session = requests.Session() bvid = exec_url.rsplit('/')[-1] header = { 'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36' } res = session.get( url="https://api.bilibili.com/x/player/pagelist?bvid={}&jsonp=jsonp".format(bvid), headers=header, verify=False )
cid = res.json()['data'][0]['cid']
res = session.get( url="https://api.bilibili.com/x/web-interface/view?cid={}&bvid={}".format(cid, bvid), headers=header, verify=False ) res_json = res.json() aid = res_json['data']['aid'] view_count = res_json['data']['stat']['view'] duration = res_json['data']['duration'] session.close() return aid, bvid, cid, duration, int(view_count)
def get_proxy_dict(): key = "..." passwd = "..." host = "..." return { "http": 'http://{}:{}@{}'.format(key, passwd, host), "https": 'http://{}:{}@{}'.format(key, passwd, host) }
def run(): url = "https://www.bilibili.com/video/BV1Mu4y1Y7Ho" while True: try: proxies = {}
aid, bvid, cid, duration, view_count = get_video_id_info(url, proxies) print("\n播放量为:{}".format(view_count))
bili = BiliBili(aid, bvid, cid, duration, proxies=proxies)
bili.x_report_click_android2()
bili.heart_beat_start(aid, cid, duration)
time.sleep(10)
bili.heart_beat_end(aid, cid, duration)
bili.session.close() except Exception as e: print("异常:", e)
if __name__ == '__main__': run()
|