vk_messages_backup: more correct handling of group chats & better chat ids
This commit is contained in:
parent
02c98143d5
commit
a7099e2efc
1 changed files with 28 additions and 11 deletions
|
@ -58,17 +58,26 @@ def users() -> Users:
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
||||||
# USERCHAT_TITLE = " ... "
|
GROUP_CHAT_MIN_ID = 2000000000
|
||||||
def _parse_chat(*, msg: Json, udict: Users) -> Chat:
|
def _parse_chat(*, msg: Json, udict: Users) -> Chat:
|
||||||
group_chat_id = msg.get('chat_id')
|
# exported with newer api, peer_id is a proper identifier both for users and chats
|
||||||
if group_chat_id is not None:
|
peer_id = msg.get('peer_id')
|
||||||
chat_id = group_chat_id
|
if peer_id is not None:
|
||||||
|
chat_id = peer_id
|
||||||
|
else:
|
||||||
|
group_chat_id = msg.get('chat_id')
|
||||||
|
if group_chat_id is not None:
|
||||||
|
chat_id = GROUP_CHAT_MIN_ID + group_chat_id
|
||||||
|
else:
|
||||||
|
chat_id = msg['user_id']
|
||||||
|
|
||||||
|
is_group_chat = chat_id >= GROUP_CHAT_MIN_ID
|
||||||
|
if is_group_chat:
|
||||||
title = msg['title']
|
title = msg['title']
|
||||||
else:
|
else:
|
||||||
user_id = msg.get('user_id') or msg.get('from_id')
|
user_id = msg.get('user_id') or msg.get('from_id')
|
||||||
assert user_id is not None
|
assert user_id is not None
|
||||||
user = udict[user_id]
|
user = udict[user_id]
|
||||||
chat_id = user_id
|
|
||||||
title = f'{user.first_name} {user.last_name}'
|
title = f'{user.first_name} {user.last_name}'
|
||||||
return Chat(
|
return Chat(
|
||||||
chat_id=chat_id,
|
chat_id=chat_id,
|
||||||
|
@ -112,12 +121,20 @@ def messages() -> Iterable[Res[Message]]:
|
||||||
list(sorted(config.storage_path.glob('groupchat_*.json')))
|
list(sorted(config.storage_path.glob('groupchat_*.json')))
|
||||||
for f in uchats:
|
for f in uchats:
|
||||||
j = json.loads(f.read_text())
|
j = json.loads(f.read_text())
|
||||||
# extract chat from last message
|
# ugh. very annoying, sometimes not possible to extract title from last message
|
||||||
try:
|
# due to newer api...
|
||||||
last = j[-1]
|
# so just do in defensively until we succeed...
|
||||||
chat = _parse_chat(msg=last, udict=udict)
|
chat = None
|
||||||
except Exception as e:
|
ex = None
|
||||||
yield e
|
for m in reversed(j):
|
||||||
|
try:
|
||||||
|
chat = _parse_chat(msg=m, udict=udict)
|
||||||
|
except Exception as e:
|
||||||
|
ex = e
|
||||||
|
continue
|
||||||
|
if chat is None:
|
||||||
|
assert ex is not None
|
||||||
|
yield ex
|
||||||
continue
|
continue
|
||||||
|
|
||||||
for msg in j:
|
for msg in j:
|
||||||
|
|
Loading…
Add table
Reference in a new issue