-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.py
More file actions
91 lines (66 loc) · 3.22 KB
/
Copy pathstart.py
File metadata and controls
91 lines (66 loc) · 3.22 KB
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
import dotenv, os
from datetime import datetime
import src.setup as setup
import src.hash as hash
from telethon import TelegramClient
# searching and loading .env
dotenv.load_dotenv(dotenv.find_dotenv())
api_id = os.getenv("api_id")
api_hash = os.getenv("api_hash")
# creating a session to collect the data
session_name = "anon"
client = TelegramClient(session_name, api_id, api_hash)
event = input("Enter the name of the dataset: ")
async def main():
# get the file that contains the chanel's or group's ids
channels_file = input("Enter the name of the csv of channels: ")
channel_set = setup.read_chanels(channels_file)
# create a file to write the hash code
hash_file_name = input("Enter the name of the hash file: ")
hash_file = open(hash_file_name, "a")
hash_set = hash.hash_start(hash_file_name)
# retrieve start and end date of dataset
start_date = input("Enter the start date [dd/mm/yyyy]: ")
start_date = datetime.strptime(start_date, "%d/%m/%Y")
end_date = input("Enter the end date [dd/mm/yyyy]: ")
end_date = datetime.strptime(end_date, "%d/%m/%Y")
# creates a dicionary to save the amount found of each media
amount = {}
# creates a directory to store the media from channels and groups
if(not os.path.exists(f"./media-{event}/")):
os.mkdir(f"./media-{event}/")
total = 0
downloaded = 0
# iterates through each channel to collect the media
for channel in channel_set:
entity = await client.get_entity(channel)
media_path = f"./media-{event}/" + str(channel)
# creates a directory with the id of the channel to store the media
if(not os.path.exists(media_path)):
os.mkdir(media_path)
async for message in client.iter_messages(entity = entity, offset_date = end_date):
message_date = datetime.fromtimestamp(message.date.timestamp())
if message_date < start_date: break
if (message.photo or message.video or message.voice or message.audio or message.document.mime_type) and (not message.web_preview):
total+=1
if ".tgs" in str(message.file.name): continue
print(message.file.name)
if hash.new_media(hash_set, message):
media_path = f"./media-{event}/" + str(channel)
media_path = setup.organize_files(media_path, message)
path = await message.download_media(file = media_path)
dst = setup.rename_files(message, path, media_path)
hash.update_set(hash_set, message)
hash.write_hash(hash_file, message, dst)
downloaded+=1
# convert an image to webp format
#if message.photo:
# setup.convert_to_webp(dst)
hash.update_amount(message, amount)
amount_file = open(f"./{event}-amount.csv", "a")
hash.write_amount(amount_file, amount, total, downloaded)
amount_file.close()
hash_file.close()
with client:
client.loop.run_until_complete(main())
#await client.log_out()