-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathforwards.py
109 lines (92 loc) · 3.09 KB
/
forwards.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#Imports#
from telethon.sync import TelegramClient
from telethon.tl.functions.messages import GetDialogsRequest
from telethon.tl.types import InputPeerEmpty
import pandas as pd
import csv
import details as ds
#Login details#
api_id = ds.apiID
api_hash = ds.apiHash
phone = ds.number
client = TelegramClient(phone, api_id, api_hash)
#Check authorisation#
client.connect()
if not client.is_user_authorized():
client.send_code_request(phone)
client.sign_in(phone, input('Enter the code: '))
chats = []
last_date = None
chunk_size = 200
groups=[]
result = client(GetDialogsRequest(
offset_date=last_date,
offset_id=0,
offset_peer=InputPeerEmpty(),
limit=chunk_size,
hash = 0
))
chats.extend(result.chats)
for chat in chats:
groups.append(chat)
print('List of chats:')
i=0
for g in groups:
print(str(i) + '- ' + g.title)
i+=1
g_index = input("Enter a Number: ")
target_entity=groups[int(g_index)]
print('Fetching forwards...')
async def main():
l = []
async for message in client.iter_messages(target_entity):
if message.forward is not None:
try:
id = message.forward.original_fwd.from_id
if id is not None:
ent = await client.get_entity(id)
print(ent.title)
l.append([ent.title,id,target_entity.title,target_entity.id])
except:
print("An exception occurred")
df = pd.DataFrame(l, columns = ['From','From ID','To', 'To ID'])
df.to_csv('forwards_data.csv')
with client:
client.loop.run_until_complete(main())
print('Forwards scraped successfully.')
next1 = input('Do you also want to scrape forwards from the discovered channels? (y/n)')
if next1 == 'y':
print('Scraping forwards from channels discovered in', target_entity.title, '...')
async def new_main():
df = pd.read_csv('edgelist.csv')
df = df.To.unique()
l = []
for i in df:
async for message in client.iter_messages(i):
if message.forward is not None:
try:
id = message.forward.original_fwd.from_id
if id is not None:
ent = await client.get_entity(id)
print(ent.title)
l.append([i, ent.title])
except:
print("An exception occurred")
print("# # # # # # # # # # Next channel: ", i, "# # # # # # # # # #")
df = pd.DataFrame(l, columns = ['From', 'To'])
df.to_csv("net.csv")
with client:
client.loop.run_until_complete(new_main())
print('Forwards scraped successfully.')
again = input('Do you want to scrape more groups? (y/n)')
if again == 'y':
print('Restarting...')
exec(open("forwards.py").read())
else:
pass
launcher = input('Do you want to return to the launcher? (y/n)')
if launcher == 'y':
print('Restarting...')
exec(open("launcher.py").read())
else:
print('Thank you for using the Telegram scraper.')