2018-05-29 08:33:37 +02:00
|
|
|
#!/usr/bin/env python3
|
2018-06-04 10:57:21 +02:00
|
|
|
import configparser
|
|
|
|
import os
|
2018-06-07 21:09:49 +02:00
|
|
|
import sys
|
|
|
|
from datetime import timedelta
|
|
|
|
from telethon import TelegramClient, events
|
|
|
|
from telethon.tl.types import User, Channel
|
2018-05-29 08:33:37 +02:00
|
|
|
|
|
|
|
|
2018-06-07 21:09:49 +02:00
|
|
|
def strip(str):
|
|
|
|
return ''.join(e for e in str if e.isalnum())
|
2018-05-29 08:33:37 +02:00
|
|
|
|
2018-06-07 21:09:49 +02:00
|
|
|
def isBot(dialog):
|
|
|
|
if not isinstance(d.entity, User):
|
|
|
|
return False
|
|
|
|
return d.entity.bot
|
2018-06-04 10:57:21 +02:00
|
|
|
|
2018-06-07 21:09:49 +02:00
|
|
|
def isChannel(dialog):
|
|
|
|
return isinstance(dialog, Channel)
|
2018-05-29 08:33:37 +02:00
|
|
|
|
2018-06-07 21:09:49 +02:00
|
|
|
def printDialog(id, d):
|
|
|
|
#print(d)
|
|
|
|
# print (d.id, " ", d.name," ", d.pinned)
|
|
|
|
print('{0:2d} | {1:14d} | {2:30} | {3:1}'.format(id, d.id, d.name, d.pinned))
|
2018-06-04 10:57:21 +02:00
|
|
|
|
|
|
|
|
2018-06-07 21:09:49 +02:00
|
|
|
def saveMessage(message, file, dlid, ownid):
|
|
|
|
out = None
|
|
|
|
if message.message:
|
|
|
|
# print(message)
|
|
|
|
file.write(' <message>\n <date>')
|
|
|
|
file.write(message.date.strftime("%s"))
|
|
|
|
file.write('</date>\n <msg>')
|
|
|
|
file.write(message.message)
|
|
|
|
file.write('</msg>\n <me>')
|
|
|
|
file.write(str(int(ownid == message.from_id)))
|
|
|
|
file.write('</me>\n')
|
|
|
|
# dl and write media if exist
|
|
|
|
if message.media != None:
|
|
|
|
file.write(' <media>')
|
|
|
|
file.write(str(dlid))
|
|
|
|
file.write('</media>\n')
|
|
|
|
out = message
|
|
|
|
|
|
|
|
file.write(' </message>\n')
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
def createFolder(path):
|
|
|
|
if not os.path.exists(path):
|
|
|
|
os.makedirs(path)
|
|
|
|
|
|
|
|
|
|
|
|
class DelayedDownload:
|
|
|
|
"""A class to store the data, for other methods to download media later"""
|
|
|
|
message = None
|
|
|
|
id = 1
|
|
|
|
|
|
|
|
def __init__(self, _id, msg):
|
|
|
|
self.id = _id
|
|
|
|
self.message = msg
|
|
|
|
|
|
|
|
|
|
|
|
def downloadChat(dialog, filename, downloadmedia):
|
|
|
|
print ("selected: ", dialog.name, 'retriving chat!')
|
|
|
|
|
|
|
|
chat = client.get_messages(dialog, limit=2000000)
|
|
|
|
print("retrived ", len(chat), " Messages.")
|
|
|
|
|
|
|
|
toDL = [] # list of DelayedDownload
|
|
|
|
|
|
|
|
createFolder('out/')
|
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
fout = open('out/' + filename + '.xml', 'x')
|
|
|
|
break
|
|
|
|
except FileExistsError:
|
|
|
|
get = input('Override out/' + filename + '.xml [y/n]?')
|
|
|
|
if get == 'y' or get == 'Y':
|
|
|
|
# delete
|
|
|
|
os.remove('out/' + filename + '.xml')
|
|
|
|
elif get == 'n' or get == 'N':
|
|
|
|
# exit
|
|
|
|
print ("Bye.")
|
|
|
|
exit(2)
|
|
|
|
|
|
|
|
fout.write('<chat>\n')
|
|
|
|
for c in chat:
|
|
|
|
dl = saveMessage(c, fout, len(toDL), me.id)
|
|
|
|
if dl != None:
|
|
|
|
if dl.media != None:
|
|
|
|
# here is something to download later
|
|
|
|
toDL.append(DelayedDownload(len(toDL), c))
|
|
|
|
fout.write('</chat>\n')
|
|
|
|
fout.close()
|
|
|
|
if(downloadmedia):
|
|
|
|
createFolder('out/media/')
|
|
|
|
print('Chat structure stored. Downloading Media - this may take a while!')
|
|
|
|
for dl in toDL:
|
|
|
|
print ('.', end='')
|
|
|
|
client.download_media(dl.message, "out/media/" + str(dl.id))
|
|
|
|
print('\n', len(toDL), " Media Files Downloaded.")
|
|
|
|
|
|
|
|
# ==================================
|
|
|
|
# MAIN program
|
|
|
|
|
|
|
|
# read config
|
2018-06-04 10:57:21 +02:00
|
|
|
try:
|
2018-06-07 21:09:49 +02:00
|
|
|
config = configparser.ConfigParser()
|
|
|
|
config.read('config.ini')
|
|
|
|
api_id = int(config.get('Main', 'api_id'))
|
|
|
|
api_hash = config.get('Main', 'api_hash')
|
|
|
|
workers = int(config.get('Main', 'workers'))
|
|
|
|
session_name = config.get('Main', 'user')
|
2018-06-04 10:57:21 +02:00
|
|
|
except (configparser.NoSectionError, configparser.NoOptionError, ValueError):
|
2018-06-07 21:09:49 +02:00
|
|
|
print('invalid config.ini')
|
|
|
|
exit(3)
|
2018-06-04 10:57:21 +02:00
|
|
|
|
2018-05-29 08:33:37 +02:00
|
|
|
# create connection
|
|
|
|
client = TelegramClient(session_name, api_id, api_hash, update_workers=workers, spawn_read_thread=True)
|
|
|
|
client.start()
|
|
|
|
|
|
|
|
me = client.get_me()
|
2018-06-07 21:09:49 +02:00
|
|
|
# get dialogs
|
2018-05-29 08:33:37 +02:00
|
|
|
dialogs = client.get_dialogs(limit=100)
|
|
|
|
|
2018-06-07 21:09:49 +02:00
|
|
|
print ("chats loaded. (", len(dialogs), ")")
|
|
|
|
# print chats
|
2018-06-04 10:57:21 +02:00
|
|
|
id = 0
|
|
|
|
|
2018-06-07 21:09:49 +02:00
|
|
|
# table header
|
|
|
|
print (
|
|
|
|
'ID | Internal ID | Username | pinned\n———+————————————————+————————————————————————————————+———————')
|
|
|
|
# content
|
2018-05-29 08:33:37 +02:00
|
|
|
for d in dialogs:
|
2018-06-07 21:09:49 +02:00
|
|
|
printDialog(id, d)
|
|
|
|
id = id + 1
|
|
|
|
get = input("Please Enter Chat ID or all: ")
|
|
|
|
if get == 'all':
|
|
|
|
for d in dialogs:
|
|
|
|
if not isBot(d) and d.entity.id != me.id and not isChannel(d):
|
|
|
|
downloadChat(d, strip(d.name), False)
|
|
|
|
elif int(get) < 0 or int(get) >= id:
|
|
|
|
print ("Unknown Chat ID!")
|
|
|
|
exit(1)
|
|
|
|
else:
|
|
|
|
downloadChat(dialogs[int(get)], 'chat', True)
|
|
|
|
|
2018-05-29 08:33:37 +02:00
|
|
|
print ('End.')
|