Skip to content

Commit

Permalink
telegram_backup: order messages by date and users/chats by id for det…
Browse files Browse the repository at this point in the history
…erminism
  • Loading branch information
karlicoss committed Mar 27, 2023
1 parent d2ef23f commit 74710b3
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions my/telegram/telegram_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def permalink(self) -> str:
Chats = Dict[str, Chat]
def _message_from_row(r: sqlite3.Row, *, chats: Chats) -> Message:
ts = r['time']
# desktop export uses UTC (checked by exporting in winter time vs summer time)
# and telegram_backup timestamps seem same as in desktop export
time = datetime.fromtimestamp(ts, tz=timezone.utc)
chat = chats[r['source_id']]
sender = chats[r['sender_id']]
Expand All @@ -78,12 +80,12 @@ def messages() -> Iterator[Message]:
with sqlite_connection(config.export_path, immutable=True, row_factory='row') as db:

chats: Chats = {}
for r in db.execute('SELECT * FROM chats'):
for r in db.execute('SELECT * FROM chats ORDER BY id'):
chat = Chat(id=r['id'], name=r['name'], handle=None)
assert chat.id not in chats
chats[chat.id] = chat

for r in db.execute('SELECT * FROM users'):
for r in db.execute('SELECT * FROM users ORDER BY id'):
first = r["first_name"]
last = r["last_name"]
name: Optional[str]
Expand All @@ -96,8 +98,7 @@ def messages() -> Iterator[Message]:
assert chat.id not in chats
chats[chat.id] = chat

# TODO order by? not sure
for r in db.execute('SELECT * FROM messages WHERE message_type NOT IN ("service_message", "empty_message")'):
for r in db.execute('SELECT * FROM messages WHERE message_type NOT IN ("service_message", "empty_message") ORDER BY time'):
# seems like the only remaining have message_type = 'message'
yield _message_from_row(r, chats=chats)

0 comments on commit 74710b3

Please sign in to comment.