added config, nice format, numbered chats
This commit is contained in:
parent
d90db02e7f
commit
16a6f4e7d7
|
@ -0,0 +1,5 @@
|
||||||
|
[Main]
|
||||||
|
api_id = 1234
|
||||||
|
api_hash = asdfasdf
|
||||||
|
workers = 4
|
||||||
|
user = username
|
|
@ -1,23 +1,13 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
from telethon import TelegramClient, events
|
from telethon import TelegramClient, events
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
import configparser
|
||||||
import sys
|
import sys
|
||||||
|
import os
|
||||||
|
|
||||||
# EDIT HERE ↓
|
def printDialog(id , d):
|
||||||
api_id = api_id
|
# print (d.id, " ", d.name," ", d.pinned)
|
||||||
api_hash = 'api_hash'
|
print('{0:2d} | {1:14d} | {2:30} | {3:1}'.format(id, d.id,d.name,d.pinned))
|
||||||
workers = 4
|
|
||||||
session_name = 'user_name'
|
|
||||||
# EDIT HERE ↑
|
|
||||||
|
|
||||||
def printDialog(d):
|
|
||||||
print (d.id, " ", d.name," ", d.pinned)
|
|
||||||
|
|
||||||
def findDialog(dialogs, id):
|
|
||||||
for d in dialogs:
|
|
||||||
if d.id == id:
|
|
||||||
return d
|
|
||||||
return None
|
|
||||||
|
|
||||||
def saveMessage(message, file, dlid, ownid):
|
def saveMessage(message, file, dlid, ownid):
|
||||||
# print(message)
|
# print(message)
|
||||||
|
@ -39,6 +29,10 @@ def saveMessage(message, file, dlid, ownid):
|
||||||
file.write(' </message>\n')
|
file.write(' </message>\n')
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
def createFolder(path):
|
||||||
|
if not os.path.exists(path):
|
||||||
|
os.makedirs(path)
|
||||||
|
|
||||||
class DelayedDownload:
|
class DelayedDownload:
|
||||||
"""A class to store the data, for other methods to download media later"""
|
"""A class to store the data, for other methods to download media later"""
|
||||||
message=None
|
message=None
|
||||||
|
@ -47,6 +41,22 @@ class DelayedDownload:
|
||||||
self.id = _id
|
self.id = _id
|
||||||
self.message = msg
|
self.message = msg
|
||||||
|
|
||||||
|
|
||||||
|
#==================================
|
||||||
|
#MAIN program
|
||||||
|
|
||||||
|
#read config
|
||||||
|
try:
|
||||||
|
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')
|
||||||
|
except (configparser.NoSectionError, configparser.NoOptionError, ValueError):
|
||||||
|
print('invalid config.ini')
|
||||||
|
exit(3)
|
||||||
|
|
||||||
# create connection
|
# create connection
|
||||||
client = TelegramClient(session_name, api_id, api_hash, update_workers=workers, spawn_read_thread=True)
|
client = TelegramClient(session_name, api_id, api_hash, update_workers=workers, spawn_read_thread=True)
|
||||||
client.start()
|
client.start()
|
||||||
|
@ -57,22 +67,44 @@ dialogs = client.get_dialogs(limit=100)
|
||||||
|
|
||||||
print ("chats loaded. (" , len(dialogs), ")")
|
print ("chats loaded. (" , len(dialogs), ")")
|
||||||
#print chats
|
#print chats
|
||||||
|
id = 0
|
||||||
|
|
||||||
|
#table header
|
||||||
|
print ('ID | Internal ID | Username | pinned\n———+————————————————+————————————————————————————————+———————')
|
||||||
|
#content
|
||||||
for d in dialogs:
|
for d in dialogs:
|
||||||
printDialog(d)
|
printDialog(id,d)
|
||||||
|
id = id+1
|
||||||
get = int(input("Please Enter Chat ID: "))
|
get = int(input("Please Enter Chat ID: "))
|
||||||
selectedDialog = findDialog(dialogs, get)
|
if get < 0 or get >= id:
|
||||||
if selectedDialog == None:
|
|
||||||
print ("Unknown Chat ID!")
|
print ("Unknown Chat ID!")
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
|
selectedDialog = dialogs[get]
|
||||||
|
|
||||||
print ("selected: ", selectedDialog.name, 'retriving chat!')
|
print ("selected: ", selectedDialog.name, 'retriving chat!')
|
||||||
|
|
||||||
chat = client.get_messages(selectedDialog, limit=2000000)
|
chat = client.get_messages(selectedDialog, limit=2000000)
|
||||||
print("retrived ", len(chat), " Messages.")
|
print("retrived ", len(chat), " Messages.")
|
||||||
|
|
||||||
toDL = []#list of messages, where media should be downloaded
|
toDL = []#list of DelayedDownload
|
||||||
|
|
||||||
|
createFolder('out/')
|
||||||
|
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
fout = open('out/chat.xml','x')
|
||||||
|
break
|
||||||
|
except FileExistsError:
|
||||||
|
get = input("Override out/chat.xml [y/n]?")
|
||||||
|
if get == 'y' or get == 'Y':
|
||||||
|
#delete
|
||||||
|
os.remove('out/chat.xml')
|
||||||
|
elif get == 'n' or get == 'N':
|
||||||
|
#exit
|
||||||
|
print ("Bye.")
|
||||||
|
exit(2)
|
||||||
|
|
||||||
fout = open('out/chat.xml','x')
|
|
||||||
fout.write('<chat>\n')
|
fout.write('<chat>\n')
|
||||||
for c in chat:
|
for c in chat:
|
||||||
dl = saveMessage(c, fout, len(toDL),me.id)
|
dl = saveMessage(c, fout, len(toDL),me.id)
|
||||||
|
@ -83,6 +115,7 @@ for c in chat:
|
||||||
fout.write('</chat>\n')
|
fout.write('</chat>\n')
|
||||||
fout.close()
|
fout.close()
|
||||||
|
|
||||||
|
createFolder('out/media/')
|
||||||
print('Chat structure stored. Downloading Media - this may take a while!')
|
print('Chat structure stored. Downloading Media - this may take a while!')
|
||||||
for dl in toDL:
|
for dl in toDL:
|
||||||
print ('.', end='')
|
print ('.', end='')
|
||||||
|
|
Loading…
Reference in New Issue