|
| 1 | +from time import time |
| 2 | +from urllib.parse import quote |
| 3 | + |
| 4 | +from PySide6.QtCore import Slot |
| 5 | + |
| 6 | +# local package import |
| 7 | +import app_state |
| 8 | +from constant import * |
| 9 | +from models.log import get_logger |
| 10 | +from models.workers.base import BaseWorker, run_wrapper |
| 11 | +from sign import ticket_hmac_sha256 |
| 12 | + |
| 13 | + |
| 14 | +class TicketFetchWorker(BaseWorker): |
| 15 | + def __init__(self): |
| 16 | + super().__init__(name="ticket获取", headers_type=HeadersType.WEB) |
| 17 | + self.logger = get_logger(self.__class__.__name__) |
| 18 | + |
| 19 | + @Slot() |
| 20 | + @run_wrapper |
| 21 | + def run(self, /): |
| 22 | + if int(app_state.cookies_dict.get("bili_ticket_expires", 0)) < int( |
| 23 | + time()): |
| 24 | + self.logger.info("buvid_ticket Request") |
| 25 | + ticket_param = { |
| 26 | + "key_id": "ec02", |
| 27 | + "hexsign": ticket_hmac_sha256(int(time())), |
| 28 | + "context[ts]": int(time()), |
| 29 | + "csrf": app_state.cookies_dict.get("bili_jct", "") |
| 30 | + } |
| 31 | + response = self._session.post( |
| 32 | + "https://api.bilibili.com/bapis/bilibili.api.ticket.v1.Ticket/GenWebTicket", |
| 33 | + params=ticket_param) |
| 34 | + self.logger.info("buvid_ticket Response") |
| 35 | + response = response.json() |
| 36 | + app_state.cookies_dict["bili_ticket"] = response["data"]["ticket"] |
| 37 | + app_state.cookies_dict["bili_ticket_expires"] = str( |
| 38 | + response["data"][ |
| 39 | + "created_at"] + \ |
| 40 | + response["data"][ |
| 41 | + "ttl"]) |
| 42 | + |
| 43 | + if not app_state.cookies_dict.get( |
| 44 | + "buvid3") or not app_state.cookies_dict.get("buvid4"): |
| 45 | + self.logger.info("buvid3 Request") |
| 46 | + response = self._session.get( |
| 47 | + "https://api.bilibili.com/x/frontend/finger/spi") |
| 48 | + self.logger.info("buvid3 Response") |
| 49 | + response = response.json() |
| 50 | + app_state.cookies_dict["buvid3"] = response["data"]["b_3"] |
| 51 | + app_state.cookies_dict["buvid4"] = quote(response["data"]["b_4"]) |
| 52 | + |
| 53 | + @Slot() |
| 54 | + def on_finished(self): |
| 55 | + from models.workers.credentials.credential_manager import \ |
| 56 | + CredentialManagerWorker |
| 57 | + |
| 58 | + CredentialManagerWorker.add_cookie(True) |
| 59 | + self._session.close() |
0 commit comments