-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTReporter.py
104 lines (77 loc) · 3.33 KB
/
TReporter.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
"""
Author: Swapnil Shinde <Username:AtmegaBuzz>
Vscale Consulting LLP
"""
from telethon import TelegramClient
from telethon.errors import SessionPasswordNeededError
from telethon.tl.functions.channels import JoinChannelRequest
from telethon.tl.functions.messages import GetMessagesRequest
from telethon.tl.types import PeerChat
import gspread
import config
import csv
# url = config.spreadsheet_url
gc = gspread.service_account(filename=config.spread_user_cred_json_file_addr)
gsheet = gc.open_by_url(url)
worksheet = gsheet.worksheets()[0]
client = TelegramClient(config.name,config.api_id,config.api_hash)
def insert_spread(values=[]):
if len(values)!=0:
worksheet.append_row(values)
print("values appended")
return
print("empty values passed")
async def filter_and_submit_report(usernames,message):
try:
username_entity = await client.get_entity(message.from_id.user_id)
message_posted_by_username = username_entity.username
if(message_posted_by_username is not None):
for usr in usernames:
if usr.lower()==message_posted_by_username.lower():
message_text = message.message
message_channel = await client.get_entity(message.peer_id.channel_id)
message_date = message.date.date()
# print("text: ",message_text)
# print("username: ",message_posted_by_username.username)
# print("channel: ",message_channel.username)
# print("date: ",message_date)
# print("-----------------")
to_append = [f"{message_date}",usr,message_channel.username,message_text]
with open('report.csv','a') as f:
writer = csv.writer(f)
writer.writerow(to_append)
except Exception as e:
print(e,"no worries, its a log of error")
async def t_scrapper(starting_link,ending_link,usernames=[]):
print(starting_link,ending_link)
channel_name,starting_message_id = starting_link.split("/")[-2],starting_link.split("/")[-1]
ending_message_id = ending_link.split("/")[-1]
await client(JoinChannelRequest(channel_name))
channel_entity = await client.get_entity(channel_name)
messages = client.iter_messages(
channel_entity,
limit=5000,
min_id=int(starting_message_id),
max_id=int(ending_message_id),
reverse=True
)
count = 0
async for message in messages:
print(count)
count+=1
await filter_and_submit_report(usernames,message)
def get_links(url):
gc = gspread.service_account(filename=config.spread_user_cred_json_file_addr)
gsheet = gc.open_by_url(url)
in_worksheet = gsheet.sheet1
return in_worksheet.col_values(1) , in_worksheet.col_values(2)
async def main():
starting_links,ending_links = get_links(config.fetch_spreadsheet_url)
usernames = config.usernames
for i in range(len(starting_links)):
try:
await t_scrapper(starting_links[i], ending_links[i],usernames)
except Exception as e:
print(e,"exception handeled")
with client:
client.loop.run_until_complete(main())