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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import telegram
|
2 |
+
from telegram.ext import Updater,CommandHandler,MessageHandler,Filters,ConversationHandler
|
3 |
+
import os, shutil
|
4 |
+
from telegram import ReplyKeyboardRemove, ReplyKeyboardMarkup
|
5 |
+
from youtube import *
|
6 |
+
import threading
|
7 |
+
|
8 |
+
# find port of server
|
9 |
+
PORT = int(os.environ.get('PORT',5000))
|
10 |
+
token = '5728368003:AAFGFs7r1LAJB-flrJ9et5J7Okzgw1_4s_Y'
|
11 |
+
|
12 |
+
START_CO, GET_WORD, GET_NUMBER,GET_CHANNEL_URL, GET_URL, CONFIRMATION = range(1, 7)
|
13 |
+
|
14 |
+
reply_keyboeard_start = [['Download entire channel'],['Download with searching word'], ['Download one video'], ['See processes'], ['exit']]
|
15 |
+
markup_start = ReplyKeyboardMarkup(reply_keyboeard_start,resize_keyboard=True, one_time_keyboard=True)
|
16 |
+
|
17 |
+
reply_keyboeard_back = [['back', 'π home', 'exit']]
|
18 |
+
markup_back = ReplyKeyboardMarkup(reply_keyboeard_back,resize_keyboard=True, one_time_keyboard=True)
|
19 |
+
|
20 |
+
reply_keyboeard_confirmation = [['I confirm'], ['π home', 'exit']]
|
21 |
+
markup_confirmation = ReplyKeyboardMarkup(reply_keyboeard_confirmation,resize_keyboard=True, one_time_keyboard=True)
|
22 |
+
|
23 |
+
|
24 |
+
|
25 |
+
def start(update,context):
|
26 |
+
update.message.reply_text('Choose between options : ', reply_markup = markup_start)
|
27 |
+
return(START_CO)
|
28 |
+
|
29 |
+
def start_co(update, context):
|
30 |
+
user = update.message.from_user
|
31 |
+
text = update.message.text
|
32 |
+
|
33 |
+
remake_folder(str(user.id))
|
34 |
+
|
35 |
+
if text == 'Download entire channel':
|
36 |
+
update.message.reply_text('Enter URL of one video on channel you want to download all of that videos.', reply_markup = markup_back)
|
37 |
+
return(GET_CHANNEL_URL)
|
38 |
+
|
39 |
+
elif text == 'Download with searching word':
|
40 |
+
update.message.reply_text('Enter word you want to search.', reply_markup = markup_back)
|
41 |
+
return(GET_WORD)
|
42 |
+
|
43 |
+
elif text == 'Download one video':
|
44 |
+
update.message.reply_text('Enter link of that video.', reply_markup = markup_back)
|
45 |
+
return(GET_URL)
|
46 |
+
|
47 |
+
def get_channel_url(update,context):
|
48 |
+
user_data = context.user_data
|
49 |
+
text = update.message.text
|
50 |
+
|
51 |
+
if text == 'back':
|
52 |
+
update.message.reply_text('Choose ...', reply_markup = markup_start)
|
53 |
+
return(START_CO)
|
54 |
+
id = find_channel_id(text)
|
55 |
+
if id :
|
56 |
+
list_of_urls = get_videos_from_channel(id)
|
57 |
+
if list_of_urls:
|
58 |
+
user_data['list_of_urls'] = list_of_urls
|
59 |
+
update.message.reply_text(f'There is {len(list_of_urls)} videos on this channel', reply_markup = markup_confirmation)
|
60 |
+
return(CONFIRMATION)
|
61 |
+
else:
|
62 |
+
update.message.reply_text('Can not find id of channel', reply_markup = markup_start)
|
63 |
+
return(START_CO)
|
64 |
+
|
65 |
+
def get_word_for_search(update, context):
|
66 |
+
user_data = context.user_data
|
67 |
+
text = update.message.text
|
68 |
+
|
69 |
+
if text == 'back':
|
70 |
+
update.message.reply_text('Choose ...', reply_markup = markup_start)
|
71 |
+
return(START_CO)
|
72 |
+
|
73 |
+
user_data['search_word'] = text
|
74 |
+
update.message.reply_text('How many videos you wanna download ?', reply_markup = markup_back)
|
75 |
+
return(GET_NUMBER)
|
76 |
+
|
77 |
+
def get_number_of_videos(update, context):
|
78 |
+
user_data = context.user_data
|
79 |
+
number = update.message.text
|
80 |
+
|
81 |
+
if number == 'back':
|
82 |
+
update.message.reply_text('Enter word you want to search.', reply_markup = markup_back)
|
83 |
+
return(GET_WORD)
|
84 |
+
|
85 |
+
try:
|
86 |
+
number = int(number)
|
87 |
+
except:
|
88 |
+
update.message.reply_text('Wrong input', reply_markup = markup_back)
|
89 |
+
return(GET_NUMBER)
|
90 |
+
|
91 |
+
list_of_urls = find_videos_with_search(user_data['search_word'], number)
|
92 |
+
if list_of_urls:
|
93 |
+
user_data['list_of_urls'] = list_of_urls
|
94 |
+
|
95 |
+
text = f'''
|
96 |
+
Search word : {user_data['search_word']}
|
97 |
+
Number of videos : {number}
|
98 |
+
If it is ok pleas confirm.'''
|
99 |
+
update.message.reply_text(text, reply_markup = markup_confirmation)
|
100 |
+
return(CONFIRMATION)
|
101 |
+
|
102 |
+
def one_video_download(update, context):
|
103 |
+
user = update.message.from_user
|
104 |
+
text = update.message.text
|
105 |
+
url = text.strip()
|
106 |
+
|
107 |
+
if text == 'back':
|
108 |
+
update.message.reply_text('Choose ...', reply_markup = markup_start)
|
109 |
+
return(START_CO)
|
110 |
+
|
111 |
+
try:
|
112 |
+
status = Download(url, user.id)
|
113 |
+
if status:
|
114 |
+
update.message.reply_video(video = open(status, 'rb'), reply_markup = markup_start)
|
115 |
+
# os.remove(status)
|
116 |
+
return(START_CO)
|
117 |
+
else:
|
118 |
+
update.message.reply_text(f"could not download the video {url}", reply_markup = markup_start)
|
119 |
+
return(START_CO)
|
120 |
+
except:
|
121 |
+
update.message.reply_text(f"could not download {url}", reply_markup = markup_start)
|
122 |
+
return(START_CO)
|
123 |
+
|
124 |
+
|
125 |
+
# test
|
126 |
+
def do_downloading(user_data, user, update):
|
127 |
+
|
128 |
+
for url in user_data['list_of_urls']:
|
129 |
+
try:
|
130 |
+
status = Download(url['url'], user.id)
|
131 |
+
if status:
|
132 |
+
update.message.reply_video(video = open(status, 'rb'), caption = url['title'])
|
133 |
+
# os.remove(status)
|
134 |
+
else:
|
135 |
+
update.message.reply_text(f"could not download the video {url['url']}")
|
136 |
+
continue
|
137 |
+
except:
|
138 |
+
update.message.reply_text(f"could not download {url['url']}", reply_markup = ReplyKeyboardRemove())
|
139 |
+
continue
|
140 |
+
|
141 |
+
def how_many_thread_is_alive(update, context):
|
142 |
+
user_data = context.user_data
|
143 |
+
|
144 |
+
counter = 0
|
145 |
+
if user_data.get('thread'):
|
146 |
+
for i in user_data['thread']:
|
147 |
+
if i.is_alive():
|
148 |
+
counter += 1
|
149 |
+
|
150 |
+
update.message.reply_text(f'there is {counter} process is going.', reply_markup = markup_start)
|
151 |
+
return(START_CO)
|
152 |
+
|
153 |
+
def confirmation(update, context):
|
154 |
+
user_data = context.user_data
|
155 |
+
user = update.message.from_user
|
156 |
+
text = update.message.text
|
157 |
+
|
158 |
+
if text != 'I confirm':
|
159 |
+
update.message.reply_text('Choose ...', reply_markup = markup_start)
|
160 |
+
return(START_CO)
|
161 |
+
|
162 |
+
t = threading.Thread(target=do_downloading, args=(user_data, user, update))
|
163 |
+
t.start()
|
164 |
+
user_data['thread'].append(t)
|
165 |
+
|
166 |
+
|
167 |
+
update.message.reply_text('finish proces', reply_markup = markup_start)
|
168 |
+
return(START_CO)
|
169 |
+
|
170 |
+
|
171 |
+
def stop_conversation(update,context):
|
172 |
+
update.message.reply_text('goodbye' , reply_markup = ReplyKeyboardRemove())
|
173 |
+
return(ConversationHandler.END)
|
174 |
+
|
175 |
+
def cancle(update,context):
|
176 |
+
update.message.reply_text('bye' , reply_markup = ReplyKeyboardRemove())
|
177 |
+
return(ConversationHandler.END)
|
178 |
+
|
179 |
+
def timeout(update, context):
|
180 |
+
user = update.message.from_user
|
181 |
+
try:
|
182 |
+
remake_folder(str(user.id))
|
183 |
+
except:
|
184 |
+
pass
|
185 |
+
|
186 |
+
update.message.reply_text('the time is out.',reply_markup = ReplyKeyboardRemove())
|
187 |
+
|
188 |
+
def error(update,context):
|
189 |
+
print(update,context.error)
|
190 |
+
|
191 |
+
|
192 |
+
|
193 |
+
|
194 |
+
|
195 |
+
|
196 |
+
|
197 |
+
|
198 |
+
|
199 |
+
|
200 |
+
|
201 |
+
|
202 |
+
|
203 |
+
|
204 |
+
|
205 |
+
# info of bot and chanal
|
206 |
+
bot = telegram.Bot(token=token)
|
207 |
+
|
208 |
+
def main():
|
209 |
+
updater = Updater(token, use_context=True)
|
210 |
+
dp = updater.dispatcher
|
211 |
+
|
212 |
+
# ---------------------------------------------->>>> User Bot Handler
|
213 |
+
conv_handler = ConversationHandler(
|
214 |
+
entry_points = [CommandHandler('start', start)],
|
215 |
+
|
216 |
+
|
217 |
+
states = states,
|
218 |
+
|
219 |
+
fallbacks = [CommandHandler('cancle', start), CommandHandler('start', start), MessageHandler(Filters.regex('^exit$'), stop_conversation),
|
220 |
+
MessageHandler(Filters.regex('^π home$'), start_co)],
|
221 |
+
|
222 |
+
conversation_timeout = 50000,
|
223 |
+
)
|
224 |
+
|
225 |
+
|
226 |
+
dp.add_handler(conv_handler)
|
227 |
+
|
228 |
+
dp.add_error_handler(error)
|
229 |
+
|
230 |
+
print('trying to connect to telegram api ...')
|
231 |
+
|
232 |
+
updater.start_polling()
|
233 |
+
|
234 |
+
|
235 |
+
# updater.start_webhook(listen='0.0.0.0',port=PORT,url_path=TOKEN)
|
236 |
+
# updater.bot.set_webhook('https://clashbazar.com/' + TOKEN )
|
237 |
+
|
238 |
+
print('connected to telegram api : 200 ')
|
239 |
+
|
240 |
+
updater.idle()
|
241 |
+
|
242 |
+
|
243 |
+
|
244 |
+
def remake_folder(folder_name):
|
245 |
+
|
246 |
+
folder_name = f'Downloads/{folder_name}'
|
247 |
+
|
248 |
+
if os.path.exists(folder_name):
|
249 |
+
for filename in os.listdir(folder_name):
|
250 |
+
file_path = os.path.join(folder_name, filename)
|
251 |
+
try:
|
252 |
+
if os.path.isfile(file_path) or os.path.islink(file_path):
|
253 |
+
os.unlink(file_path)
|
254 |
+
elif os.path.isdir(file_path):
|
255 |
+
shutil.rmtree(file_path)
|
256 |
+
except Exception as e:
|
257 |
+
print('Failed to delete %s. Reason: %s' % (file_path, e))
|
258 |
+
|
259 |
+
else:
|
260 |
+
os.mkdir(folder_name)
|
261 |
+
|
262 |
+
|
263 |
+
|
264 |
+
if __name__ == '__main__':
|
265 |
+
|
266 |
+
|
267 |
+
same = [CommandHandler('cancle', cancle), MessageHandler(Filters.regex('^exit$'), stop_conversation), MessageHandler(Filters.regex('^π home$'), start)]
|
268 |
+
|
269 |
+
|
270 |
+
states = {
|
271 |
+
START_CO : [CommandHandler('start', start),
|
272 |
+
MessageHandler(Filters.regex('^Download entire channel$'), start_co),
|
273 |
+
MessageHandler(Filters.regex('^Download with searching word$'), start_co),
|
274 |
+
MessageHandler(Filters.regex('^Download one video$'), start_co),
|
275 |
+
MessageHandler(Filters.regex('^See processes$'), how_many_thread_is_alive),
|
276 |
+
],
|
277 |
+
|
278 |
+
GET_WORD : same + [CommandHandler('start', start), MessageHandler(Filters.text , get_word_for_search)],
|
279 |
+
|
280 |
+
GET_NUMBER : same + [CommandHandler('start', start), MessageHandler(Filters.text , get_number_of_videos)],
|
281 |
+
|
282 |
+
GET_CHANNEL_URL : same + [CommandHandler('start', start), MessageHandler(Filters.text , get_channel_url)],
|
283 |
+
|
284 |
+
GET_URL : same + [CommandHandler('start', start), MessageHandler(Filters.text , one_video_download)],
|
285 |
+
|
286 |
+
CONFIRMATION : [CommandHandler('start', start), MessageHandler(Filters.regex('^I confirm$'), confirmation)],
|
287 |
+
|
288 |
+
|
289 |
+
|
290 |
+
}
|
291 |
+
|
292 |
+
main()
|