-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbot.py
89 lines (80 loc) · 3.15 KB
/
bot.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
#!/usr/bin/env python3
####
from os import remove
import subprocess
import sys
from time import sleep
import telebot;
import yt_dlp;
import datetime
import time
def help():
print('Create bot in https://t.me/BotFather and put token to script or with -t argument')
print('-t <token_string> - puts telegram bot token to script')
token = ''
argIdx = 1
while(argIdx < len(sys.argv)):
if sys.argv[argIdx] == '-t':
if (argIdx + 1 >= len(sys.argv)):
print('token expected after -t')
quit()
else:
token = sys.argv[argIdx + 1]
argIdx += 1
elif sys.argv[argIdx] == '-h':
help()
quit()
else:
print('{} - unknown arg'.format(sys.argv[argIdx]))
help()
quit()
argIdx += 1
bot = telebot.TeleBot(token)
counter = 0
with open('log.txt', 'a') as f:
f.write("launched {}\n".format(datetime.datetime.now()))
@bot.message_handler(content_types=['text'])
def get_text_messages(message):
if (message.text == '/start'):
bot.send_message(message.from_user.id, 'Отправь ссылку на тикток или ютуб')
elif (message.text == '/help'):
bot.send_message(message.from_user.id, 'Отправь ссылку на тикток или ютуб')
else:
global counter
counter += 1
current_counter = counter
try:
print('{} try load: {}'.format(current_counter, message.text))
bot.send_message(message.from_user.id, 'Работаем')
try:
yt_dlp.YoutubeDL({
'no_warnings': True,
'quiet': True,
'outtmpl': 'video{}.mp4'.format(current_counter)
}).download(message.text)
except:
with open('log.txt', 'a') as f:
f.write("{} failed {} request {}\n".format(current_counter, message.from_user.id, message.text))
bot.send_message(message.from_user.id, 'Нерабочая ссылка')
print('{} loaded::: {}'.format(current_counter, message.text))
bot.send_video(message.from_user.id, video=open('video{}.mp4'.format(current_counter), 'rb'))
print('{} sended::: {}'.format(current_counter, message.text))
remove('video{}.mp4'.format(current_counter))
with open('log.txt', 'a') as f:
f.write("{} success {} request {}\n".format(current_counter, message.from_user.id, message.text))
except:
with open('log.txt', 'a') as f:
f.write("{} bot failed {} request {}\n".format(current_counter, message.from_user.id, message.text))
print('telegram bot died')
print('{} done::::: {}'.format(current_counter, message.text))
while(True):
try:
bot.polling(none_stop=True, interval=0)
print('telegram bot restart')
with open('log.txt', 'a') as f:
f.write("{} bot restart\n".format(counter))
except:
print('telegram bot died in loop')
with open('log.txt', 'a') as f:
f.write("{} bot died. Restarting\n".format(counter))
sleep(15)