forked from proseltd/Telepathy-Community
-
Notifications
You must be signed in to change notification settings - Fork 0
/
forwards.py
160 lines (121 loc) · 5.28 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
from telethon import TelegramClient
import pandas as pd
import details as ds
import os
# Login details #
api_id = ds.apiID
api_hash = ds.apiHash
phone = ds.number
client = TelegramClient(phone, api_id, api_hash)
client.connect()
if not client.is_user_authorized():
client.send_code_request(phone)
client.sign_in(phone, input('Enter the code: '))
def inputChannelName():
while True:
try:
channelName = input("Please enter a Telegram channel name:\n")
print(f'You entered "{channelName}"')
answer = input('Is this correct? (y/n)')
if answer == 'y':
print('Scraping forwards from', channelName, 'This may take a while...')
return channelName
except():
continue
print('Welcome to channel forward scraper.\n'
'This tool will scrape a Telegram channel for all forwarded messages and '
'their original sources.')
channel_name = inputChannelName()
async def main():
lines = []
async for message in client.iter_messages(channel_name):
if message.forward is not None:
try:
from_id = message.forward.original_fwd.from_id
if from_id is not None:
ent = await client.get_entity(from_id)
date = str(message.date.year) + "/" + str(message.date.month) + "/" + str(message.date.day)
time = str(message.date.hour) + ":" + str(message.date.minute)
# print(ent.title,">>>",channel_name)
df = pd.DataFrame(lines, columns=['To', 'From', 'date', 'time'])
name_clean = channel_name
alphanumeric = ""
for character in name_clean:
if character.isalnum():
alphanumeric += character
directory = './data/edgelists/'
try:
os.makedirs(directory)
except FileExistsError:
pass
file = './data/edgelists/' + alphanumeric + '_edgelist.csv'
with open(file, 'w+') as f:
df.to_csv(f)
lines.append([channel_name, ent.title, date, time])
except():
# print("An exception occurred: Could be private, now deleted, or a group.")
pass
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':
# channel_name = inputChannelName()
print('Scraping forwards from channels discovered in', channel_name, '...')
async def new_main():
name_clean = channel_name
alphanumeric = ""
for character in name_clean:
if character.isalnum():
alphanumeric += character
df = pd.read_csv('./data/edgelists/' + alphanumeric + '_edgelist.csv')
df = df.From.unique()
lines = []
for i in df:
async for message in client.iter_messages(i):
if message.forward is not None:
try:
from_id = message.forward.original_fwd.from_id
if from_id is not None:
ent = await client.get_entity(from_id)
date = str(message.date.year) + "/" + str(message.date.month) + "/" + str(message.date.day)
time = str(message.date.hour) + ":" + str(message.date.minute)
# print(ent.title,">>>", i)
df = pd.DataFrame(lines, columns=['To', 'From', 'date', 'time'])
name_clean = channel_name
alphanumeric = ""
for character in name_clean:
if character.isalnum():
alphanumeric += character
directory = './data/edgelists/'
try:
os.makedirs(directory)
except FileExistsError:
pass
file1 = './data/edgelists/' + alphanumeric + '_net.csv'
with open(file1, 'w+') as f:
df.to_csv(f)
lines.append([i, ent.title, date, time])
except():
print("An exception occurred: Could be private, now deleted, or a group.")
pass
print("Scrape complete for:", i, )
df.to_json('./data/edgelists/' + alphanumeric + '_archive.json',
orient='split', compression='infer', index=True)
with client:
client.loop.run_until_complete(new_main())
print('Forwards scraped successfully.')
else:
pass
again = input('Do you want to scrape more chats? (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("telepathy.py").read())
else:
print('Thank you for using Telepathy.')