forked from pravdakotchestno/telegram-channel-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcb.py
executable file
·67 lines (62 loc) · 2.37 KB
/
tcb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from telethon.sync import TelegramClient
from texttable import Texttable
from telethon.errors.rpcerrorlist import MessageIdInvalidError
from argparse import ArgumentParser
from timeit import default_timer as timer
parser = ArgumentParser(description = 'Simple telegram channel backup utility.')
parser.add_argument('--api_key')
parser.add_argument('--session')
parser.add_argument('--from_channel', default = None)
parser.add_argument('--to_channel', default = None)
parser.add_argument('action', choices = ['retrieve_channels', 'forward_posts'])
args = parser.parse_args()
print('Connecting to the Telegram...')
try:
api_key = args.api_key.split(':')
api_id = int(api_key[0])
api_hash = api_key[1]
session = args.session
client = TelegramClient(session, api_id, api_hash)
client.start()
print('Logged in successfuly as @' + client.get_me().username)
except Exception as e:
print("Couldn't connect to the Telegram:", e)
exit(1)
def retrieve_channels():
table = Texttable()
table = table.header(['ID', 'Channel title', 'Username'])
table = table.set_cols_width([12, 32, 33])
table = table.set_cols_dtype(['t', 't', 't'])
print('Retrieving dialogs...')
for dialog in client.iter_dialogs():
if dialog.is_channel:
_id = str(dialog.entity.id)
title = dialog.title
username = '@' + dialog.entity.username
table.add_row([_id, title, username])
print(table.draw())
def get_channel_entity(channel_id):
for dialog in client.iter_dialogs():
if dialog.entity.id == channel_id:
return dialog.entity, dialog.message.id
def forward_posts():
from_channel_id, to_channel_id = int(args.from_channel), int(args.to_channel)
from_channel, posts_amount = get_channel_entity(from_channel_id)
to_channel, _ = get_channel_entity(to_channel_id)
print('Forwarding posts...')
time1 = timer()
posts_forwarded = 0
for post in client.iter_messages(from_channel, reverse = True):
try:
post.forward_to(entity = to_channel)
posts_forwarded += 1
except MessageIdInvalidError:
pass
print('About {0:6.2f}% have been forwarded'.format(post.id / posts_amount * 100), end='\r')
time2 = timer()
print()
print('=============================================')
print('{0} posts have been forwarded in {1:.2f} seconds'.format(posts_forwarded, (time2 - time1)))
print('=============================================')
if __name__ == '__main__':
exec(args.action + '()')