From 7918a58d433a2c031b1513cff25fddab0fd41467 Mon Sep 17 00:00:00 2001 From: Hykilpikonna Date: Wed, 10 Nov 2021 15:33:22 -0500 Subject: [PATCH] [+] Telegram avatar that doens't work --- telegram-history-analysis/telegram-avatars.py | 47 +++++++++++++++++++ telegram-history-analysis/time-animation.py | 4 +- 2 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 telegram-history-analysis/telegram-avatars.py diff --git a/telegram-history-analysis/telegram-avatars.py b/telegram-history-analysis/telegram-avatars.py new file mode 100644 index 0000000..dab2449 --- /dev/null +++ b/telegram-history-analysis/telegram-avatars.py @@ -0,0 +1,47 @@ +import io +import os + +import json5 +import requests + +if __name__ == '__main__': + """ + This method doesn't work + """ + token = os.environ.get('tg-bot-token') + + with io.open('userid_to_name.json5', mode='r', encoding='utf-8') as f: + userid_to_name = json5.loads(f.read()) + + for user_id in userid_to_name: + user = userid_to_name[user_id] + + # Get photo + r = requests.get(f'https://api.telegram.org/bot{token}/getUserProfilePhotos', + params={'user_id': user_id[4:]}) + + o = r.json() + if 'result' not in o: + print(f'Result not in o for {user}') + print(o) + continue + + o = o['result'] + if o['total_count'] == 0: + print(f'Total count is 0 for user {user}') + continue + + photo = max(o['photos'][0], key=lambda x: x['height']) + + # Get file + r = requests.get(f'https://api.telegram.org/bot{token}/getFile', + params={'file_id': photo['file_id']}) + o = r.json()['result'] + + # Download file + r = requests.get(f'https://api.telegram.org/file/bot{token}/{o["file_path"]}', + params={'file_id': photo['file_id']}) + + # Write file + with io.open(f'avatars/{userid_to_name[user_id]}.jpg', 'wb') as f: + f.write(r.content) diff --git a/telegram-history-analysis/time-animation.py b/telegram-history-analysis/time-animation.py index 6eb2fdf..6508484 100644 --- a/telegram-history-analysis/time-animation.py +++ b/telegram-history-analysis/time-animation.py @@ -12,10 +12,10 @@ if __name__ == '__main__': with io.open(file, mode='r', encoding="utf-8") as f: content = f.read() - obj = json5.loads(content) + obj = json.loads(content) with io.open('userid_to_name.json5', mode='r', encoding='utf-8') as f: - userid_to_name = json.loads(f.read()) + userid_to_name = json5.loads(f.read()) messages: list = obj['messages'] messages = [m for m in messages if True