33 lines
1.1 KiB
Python
33 lines
1.1 KiB
Python
"""Parse Douyin IM Response protobuf for conversation metadata."""
|
|
from __future__ import annotations
|
|
|
|
|
|
def extract_conv_meta_from_response_bytes(raw: bytes) -> dict[str, dict]:
|
|
"""Return {conversation_id: {conversation_short_id, ticket}} from IM API protobuf body."""
|
|
if not raw:
|
|
return {}
|
|
try:
|
|
from .static import Response_pb2 as ResponseProto
|
|
|
|
response_proto = ResponseProto.Response()
|
|
response_proto.ParseFromString(raw)
|
|
body = response_proto.body
|
|
out: dict[str, dict] = {}
|
|
for field in (
|
|
"create_conversation_v2_body",
|
|
"get_conversation_info_list_v2_response_body",
|
|
):
|
|
if not body.HasField(field):
|
|
continue
|
|
conv_body = getattr(body, field)
|
|
for conv in conv_body.conversation_info_list:
|
|
if not conv.conversation_id:
|
|
continue
|
|
out[conv.conversation_id] = {
|
|
"conversation_short_id": str(conv.conversation_short_id),
|
|
"ticket": conv.ticket,
|
|
}
|
|
return out
|
|
except Exception:
|
|
return {}
|