Last active
January 16, 2023 09:39
-
-
Save spookyahell/69b7dd0c92e52d0f0ca2d4ee16e42e59 to your computer and use it in GitHub Desktop.
TDSM (stop thinking so dirty!) — Telegram Desktop Session (tdata) Manager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''TDSM (stop thinking so dirty!) — Telegram Desktop Session (tdata) Manager by @spookyahell-->Github.com''' | |
'''License: MIT with the following addition: The line above this one (or at the very top of the file) may not be removed at any point in any future version of the file and must for ever be included. Unless for a very good technical reason, the details of which could be discussed with [email protected], do NOT rename the file. You can probably go ahead and rename the file despite not having heard back. After 48h, probably don't expect an answer anymore. | |
Here are some alternative file name suggestions that don't require prior approval of any form: | |
tgsm.py, tg_sm.py, tdsm.py, td_sm.py | |
Please specifically refrain from calling the file profane names; | |
there's already too much profanity out there. No need for more.''' | |
from subprocess import check_output, call, Popen, PIPE | |
from time import sleep | |
from os import environ, rename, sep, listdir | |
from os.path import isdir | |
import locale, platform, sys | |
verbose_logging = False | |
if platform.system() == 'Windows': | |
app_data_loc = environ['APPDATA'] | |
user_profile_loc = environ['USERPROFILE'] | |
tg_app_loc = f'{app_data_loc}{sep}Telegram Desktop' | |
possible_sessions = [] | |
for possible_folder in listdir(tg_app_loc): | |
if isdir(f'{tg_app_loc}{sep}{possible_folder}'): | |
if possible_folder.startswith('tdata_'): | |
possible_sessions.append(possible_folder.split('_')[-1]) | |
asf = fr'{user_profile_loc}\Downloads\Python-Projects\StuffIwantInPath\TGSI' # needs to be within a folder that already exists | |
with open(asf) as f: | |
active_session = f.read() | |
print(f'Active session: {active_session!r}') | |
#~ lang, cp = locale.getlocale() | |
#~ languages = {'de_DE': | |
#~ {'string1': | |
#~ 'ERFOLGREICH: Ein Beendigungssignal wurde an den Prozess', | |
#~ 'string2': | |
#~ 'ERFOLGREICH: Der Prozess "Telegram.exe" mit PID', | |
#~ 'string3': | |
#~ 'wurde nicht gefunden.' | |
#~ } | |
#~ } | |
if verbose_logging is False: | |
x1 = call(['taskkill','-im', 'Telegram.exe'], stdout = PIPE) | |
else: | |
x1 = call(['taskkill','-im', 'Telegram.exe']) | |
if x1 == 128: | |
print('Hmm. Okay, looks like Telegram ain\'t runnin\' at all. - Very good!') | |
elif x1 == 0: | |
print('Waiting for Telegram to exit normally, then force-kill will occur', end = '...', flush = True) | |
sleep(5) | |
if verbose_logging is False: | |
x2 = call(['taskkill','-f', '-im', 'Telegram.exe'], stdout = PIPE) | |
else: | |
x2 = call(['taskkill','-f', '-im', 'Telegram.exe']) | |
print(' still waiting (for 2 more seconds)') | |
sleep(2) | |
else: | |
print(f'Eek. X1 has the following unexpected return code: {x1}') | |
def switchToSession(session_name): | |
if verbose_logging is True: | |
print(f'Saving active session elsewhere: Moving "tdata" dir to "tdata_{active_session}"') | |
rename(f'{tg_app_loc}{sep}tdata', f'{tg_app_loc}{sep}tdata_{active_session}') | |
if verbose_logging is True: | |
print(f'Placing requested session for Telegram to use: Moving "tdata_{session_name}" to "tdata"') | |
rename(f'{tg_app_loc}{sep}tdata_{session_name}', f'{tg_app_loc}{sep}tdata') | |
return True | |
if len(possible_sessions) == 1: | |
print(f'Only one session to switch to... switching to {possible_sessions[0]!r}...') | |
new_session = possible_sessions[0] | |
switchToSession(new_session) | |
else: | |
print('Available sessions: {}'.format(', '.join([f'{idx+1}: {session}' for idx, session in enumerate(possible_sessions)]))) | |
try: | |
x = int(input('Select a session by index: ')) | |
except ValueError: | |
print('ERROR: That was not a index number.') | |
sys.exit(1) | |
if x > len(possible_sessions): | |
print('ERROR: That index was too high.') | |
sys.exit(2) | |
new_session = possible_sessions[x-1] | |
switchToSession(new_session) | |
with open(asf, 'w') as f: | |
f.write(new_session) | |
print('Launching Telegram...') | |
Popen([f'{tg_app_loc}{sep}Telegram.exe'], cwd = tg_app_loc) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment