彈幕
douyin.proto文件的內(nèi)容如下:
syntax = "proto3";
package douyin;
message HeadersList {
? ? string key = 1;
? ? string value = 2;
}
message PushFrame {
? ? uint64 seqId = 1;
? ? uint64 logId = 2;
? ? uint64 service = 3;
? ? uint64 method = 4;
? ? repeated HeadersList headersList = 5;
? ? string payloadEncoding = 6;
? ? string payloadType = 7;
? ? bytes payload = 8;
}
message Message {
? ? string method = 1;
? ? bytes payload = 2;
? ? int64 msgId = 3;
? ? int32 msgType = 4;
? ? int64 offset = 5;
? ? bool needWrdsStore = 6;
? ? int64 wrdsVersion = 7;
? ? string wrdsSubKey = 8;
}
message Response {
? ? repeated Message messagesList = 1;
? ? string cursor = 2;
? ? uint64 fetchInterval = 3;
? ? uint64 now = 4;
? ? string internalExt = 5;
? ? uint32 fetchType = 6;
? ? map<string, string> routeParams = 7;
? ? uint64 heartbeatDuration = 8;
? ? bool needAck = 9;
? ? string pushServer = 10;
? ? string liveCursor = 11;
? ? bool historyNoMore = 12;
}
message ChatMessage {
? ? User user = 2;
? ? string content = 3;
? ? bool visibleToSender = 4;
}
message User {
? ? uint64 id = 1;
? ? uint64 shortId = 2;
? ? string nickName = 3;
? ? uint32 gender = 4;
? ? string Signature = 5;
? ? uint32 Level = 6;
? ? uint64 Birthday = 7;
? ? string Telephone = 8;
? ? string city = 14;
}
douyin_pb2.py的內(nèi)容如下:
# -*- coding: utf-8 -*-
# Generated by the protocol buffer compiler.? DO NOT EDIT!
# source: douyin.proto
"""Generated protocol buffer code."""
from google.protobuf import descriptor as _descriptor
from google.protobuf import descriptor_pool as _descriptor_pool
from google.protobuf import symbol_database as _symbol_database
from google.protobuf.internal import builder as _builder
# @@protoc_insertion_point(imports)
_sym_db = _symbol_database.Default()
DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x0c\x64ouyin.proto\x12\x06\x64ouyin\")\n\x0bHeadersList\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\t\"\xb3\x01\n\tPushFrame\x12\r\n\x05seqId\x18\x01 \x01(\x04\x12\r\n\x05logId\x18\x02 \x01(\x04\x12\x0f\n\x07service\x18\x03 \x01(\x04\x12\x0e\n\x06method\x18\x04 \x01(\x04\x12(\n\x0bheadersList\x18\x05 \x03(\x0b\x32\x13.douyin.HeadersList\x12\x17\n\x0fpayloadEncoding\x18\x06 \x01(\t\x12\x13\n\x0bpayloadType\x18\x07 \x01(\t\x12\x0f\n\x07payload\x18\x08 \x01(\x0c\"\x9a\x01\n\x07Message\x12\x0e\n\x06method\x18\x01 \x01(\t\x12\x0f\n\x07payload\x18\x02 \x01(\x0c\x12\r\n\x05msgId\x18\x03 \x01(\x03\x12\x0f\n\x07msgType\x18\x04 \x01(\x05\x12\x0e\n\x06offset\x18\x05 \x01(\x03\x12\x15\n\rneedWrdsStore\x18\x06 \x01(\x08\x12\x13\n\x0bwrdsVersion\x18\x07 \x01(\x03\x12\x12\n\nwrdsSubKey\x18\x08 \x01(\t\"\xe4\x02\n\x08Response\x12%\n\x0cmessagesList\x18\x01 \x03(\x0b\x32\x0f.douyin.Message\x12\x0e\n\x06\x63ursor\x18\x02 \x01(\t\x12\x15\n\rfetchInterval\x18\x03 \x01(\x04\x12\x0b\n\x03now\x18\x04 \x01(\x04\x12\x13\n\x0binternalExt\x18\x05 \x01(\t\x12\x11\n\tfetchType\x18\x06 \x01(\r\x12\x36\n\x0brouteParams\x18\x07 \x03(\x0b\x32!.douyin.Response.RouteParamsEntry\x12\x19\n\x11heartbeatDuration\x18\x08 \x01(\x04\x12\x0f\n\x07needAck\x18\t \x01(\x08\x12\x12\n\npushServer\x18\n \x01(\t\x12\x12\n\nliveCursor\x18\x0b \x01(\t\x12\x15\n\rhistoryNoMore\x18\x0c \x01(\x08\x1a\x32\n\x10RouteParamsEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\t:\x02\x38\x01\"S\n\x0b\x43hatMessage\x12\x1a\n\x04user\x18\x02 \x01(\x0b\x32\x0c.douyin.User\x12\x0f\n\x07\x63ontent\x18\x03 \x01(\t\x12\x17\n\x0fvisibleToSender\x18\x04 \x01(\x08\"\x9a\x01\n\x04User\x12\n\n\x02id\x18\x01 \x01(\x04\x12\x0f\n\x07shortId\x18\x02 \x01(\x04\x12\x10\n\x08nickName\x18\x03 \x01(\t\x12\x0e\n\x06gender\x18\x04 \x01(\r\x12\x11\n\tSignature\x18\x05 \x01(\t\x12\r\n\x05Level\x18\x06 \x01(\r\x12\x10\n\x08\x42irthday\x18\x07 \x01(\x04\x12\x11\n\tTelephone\x18\x08 \x01(\t\x12\x0c\n\x04\x63ity\x18\x0e \x01(\tb\x06proto3')
_globals = globals()
_builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals)
_builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'douyin_pb2', _globals)
if _descriptor._USE_C_DESCRIPTORS == False:
? DESCRIPTOR._options = None
? _RESPONSE_ROUTEPARAMSENTRY._options = None
? _RESPONSE_ROUTEPARAMSENTRY._serialized_options = b'8\001'
? _globals['_HEADERSLIST']._serialized_start=24
? _globals['_HEADERSLIST']._serialized_end=65
? _globals['_PUSHFRAME']._serialized_start=68
? _globals['_PUSHFRAME']._serialized_end=247
? _globals['_MESSAGE']._serialized_start=250
? _globals['_MESSAGE']._serialized_end=404
? _globals['_RESPONSE']._serialized_start=407
? _globals['_RESPONSE']._serialized_end=763
? _globals['_RESPONSE_ROUTEPARAMSENTRY']._serialized_start=713
? _globals['_RESPONSE_ROUTEPARAMSENTRY']._serialized_end=763
? _globals['_CHATMESSAGE']._serialized_start=765
? _globals['_CHATMESSAGE']._serialized_end=848
? _globals['_USER']._serialized_start=851
? _globals['_USER']._serialized_end=1005
# @@protoc_insertion_point(module_scope)
vdy.py的內(nèi)容如下:
from websocket import WebSocketApp
import json
import re
import gzip
from urllib.parse import unquote_plus
import requests
from douyin_pb2 import PushFrame, Response, ChatMessage
def fetch_live_room_info(url):
? ? res = requests.get(
? ? ? ? url=url,
? ? ? ? headers={
? ? ? ? ? ? "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36",
? ? ? ? },
? ? ? ? cookies={
? ? ? ? ? ? "__ac_nonce": "063abcffa00ed8507d599"? # 可以是任意值
? ? ? ? }
? ? )
? ? data_string = re.findall(r'<script id="RENDER_DATA" type="application/json">(.*?)</script>', res.text)[0]
? ? data_dict = json.loads(unquote_plus(data_string))
? ? room_id = data_dict['app']['initialState']['roomStore']['roomInfo']['roomId']
? ? room_title = data_dict['app']['initialState']['roomStore']['roomInfo']["room"]['title']
? ? room_user_count = data_dict['app']['initialState']['roomStore']['roomInfo']["room"]['user_count_str']
? ? # print(room_id)
? ? wss_url = f"wss://webcast5-ws-web-hl.douyin.com/webcast/im/push/v2/?app_name=douyin_web&version_code=180800&webcast_sdk_version=1.0.7&update_version_code=1.0.7&compress=gzip&device_platform=web&cookie_enabled=true&screen_width=1920&screen_height=1080&browser_language=zh-CN&browser_platform=Win32&browser_name=Mozilla&browser_version=5.0%20(Windows%20NT%2010.0;%20Win64;%20x64)%20AppleWebKit/537.36%20(KHTML,%20like%20Gecko)%20Chrome/114.0.0.0%20Safari/537.36&browser_online=true&tz_name=Asia/Shanghai&cursor=u-1_h-1_t-1690179738544_r-1_d-1&internal_ext=internal_src:dim|wss_push_room_id:7259245469392472887|wss_push_did:7229964158304273932|dim_log_id:20230724142218926A75BA70A000B944B6|first_req_ms:1690179738465|fetch_time:1690179738544|seq:1|wss_info:0-1690179738545-0-0|wrds_kvs:WebcastRoomStatsMessage-1690179732991702471_WebcastRoomRankMessage-1690179655006985747_HighlightContainerSyncData-13&host=https://live.douyin.com&aid=6383&live_id=1&did_rule=3&endpoint=live_pc&support_wrds=1&user_unique_id=&im_path=/webcast/im/fetch/&room_id=7259245469392472887&identity=audience&heartbeatDuration=0&signature=R4Ueb30lVefZpYOv"
? ? ttwid = res.cookies.get_dict()['ttwid']
? ? return room_id, room_title, room_user_count, wss_url, ttwid
def on_open(ws):
? ? print('on_open')
def on_message(ws, content):
? ? frame = PushFrame()
? ? frame.ParseFromString(content)
? ? # 對(duì)PushFrame的 payload 內(nèi)容進(jìn)行g(shù)zip解壓
? ? origin_bytes = gzip.decompress(frame.payload)
? ? # 根據(jù)Response+gzip解壓數(shù)據(jù),生成數(shù)據(jù)對(duì)象
? ? response = Response()
? ? response.ParseFromString(origin_bytes)
? ? if response.needAck:
? ? ? ? s = PushFrame()
? ? ? ? s.payloadType = "ack"
? ? ? ? s.payload = response.internalExt.encode('utf-8')
? ? ? ? s.logId = frame.logId
? ? ? ? ws.send(s.SerializeToString())
? ? # 獲取數(shù)據(jù)內(nèi)容(需根據(jù)不同method,使用不同的結(jié)構(gòu)對(duì)象對(duì) 數(shù)據(jù) 進(jìn)行解析)
? ? #? ?注意:此處只處理 WebcastChatMessage ,其他處理方式都是類似的。
? ? for item in response.messagesList:
? ? ? ? if item.method != "WebcastChatMessage":
? ? ? ? ? ? continue
? ? ? ? message = ChatMessage()
? ? ? ? message.ParseFromString(item.payload)
? ? ? ? info = f"【{message.user.nickName}】{message.content} "
? ? ? ? print(info)
def on_error(ws, content):
? ? print(content)
? ? print("on_error")
def on_close(*args, **kwargs):
? ? print(args, kwargs)
? ? print("on_close")
def run():
? ? web_url = "https://live.douyin.com/933039431393"
? ? room_id, room_title, room_user_count, wss_url, ttwid = fetch_live_room_info(web_url)
? ? print(room_id, room_title, room_user_count, wss_url, ttwid)
? ? ws = WebSocketApp(
? ? ? ? url=wss_url,
? ? ? ? header={
? ? ? ? ? ? "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
? ? ? ? },
? ? ? ? cookie=f"ttwid={ttwid}",
? ? ? ? on_open=on_open,
? ? ? ? on_message=on_message,
? ? ? ? on_error=on_error,
? ? ? ? on_close=on_close,
? ? )
? ? ws.run_forever()
if __name__ == '__main__':
? ? run()