-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbot.py
More file actions
91 lines (73 loc) · 3.28 KB
/
bot.py
File metadata and controls
91 lines (73 loc) · 3.28 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
from telethon import TelegramClient, events
from download_from_url import download_file, get_size
import os
import time
import datetime
import aiohttp
api_id = int("1304050")
api_hash = "cb772803bc9c8407ab1d79a5a317039c"
bot_token = "1375924630:AAHVZMm-gOCAxSwe5Thp_9ftOo_fFD5Q8yw"
download_path = "Downloads/"
bot = TelegramClient('Uploader bot', api_id, api_hash).start(bot_token=bot_token)
def get_date_in_two_weeks():
"""
get maximum date of storage for file
:return: date in two weeks
"""
today = datetime.datetime.today()
date_in_two_weeks = today + datetime.timedelta(days=14)
return date_in_two_weeks.date()
async def send_to_transfersh_async(file):
size = os.path.getsize(file)
size_of_file = get_size(size)
final_date = get_date_in_two_weeks()
file_name = os.path.basename(file)
print("\nSending file: {} (size of the file: {})".format(file_name, size_of_file))
url = 'https://transfer.sh/'
with open(file, 'rb') as f:
async with aiohttp.ClientSession() as session:
async with session.post(url, data={str(file): f}) as response:
download_link = await response.text()
print("Link to download file(will be saved till {}):\n{}".format(final_date, download_link))
return download_link, final_date, size_of_file
@bot.on(events.NewMessage(pattern='/start'))
async def start(event):
"""Send a message when the command /start is issued."""
await event.respond('Hi!\nSent any file or direct download url to get the transfer.sh download link')
raise events.StopPropagation
@bot.on(events.NewMessage)
async def echo(update):
"""Echo the user message."""
msg = await update.respond("Processing")
try:
if not os.path.isdir(download_path):
os.mkdir(download_path)
start = time.time()
if not update.message.message.startswith("/") and not update.message.message.startswith("http") and update.message.media:
file_path = await bot.download_media(update.message, download_path)
else:
url = update.text
filename = os.path.join(download_path, os.path.basename(url))
file_path = await download_file(update.text, filename, msg, start, bot)
print(f"file downloaded to {file_path}")
try:
await msg.edit("Download finish!\n\n**Now uploading...**")
# download_link, final_date, size = send_to_transfersh(file_path)
download_link, final_date, size = await send_to_transfersh_async(file_path)
name = os.path.basename(file_path)
await msg.edit(f"**Name: **`{name}`\n**Size:** `{size}`\n**Link:** {download_link}")
except Exception as e:
print(e)
await msg.edit(f"Uploading Failed\n\n**Error:** {e}")
finally:
os.remove(file_path)
print("Deleted file :", file_path)
except Exception as e:
print(e)
await msg.edit(f"Download link is invalid or not accessable\n\n**Error:** {e}")
def main():
"""Start the bot."""
print("\nBot started..\n")
bot.run_until_disconnected()
if __name__ == '__main__':
main()