-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmds.py
386 lines (353 loc) · 10.5 KB
/
cmds.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import requests, json
import errno
import os
import sys, random, datetime, time, re
import tempfile
import urllib
import acc
import pafy
from threading import Timer
from sesuatu import *
from acc import (namaBot, google_key, line_bot_api, handler, owner)
from linebot.exceptions import LineBotApiError
from linebot.models import *
from linebot.exceptions import (
InvalidSignatureError
)
perintah = {} # menyimpan perintah agar dapat menerima inputan
#sesuatu = {} # digunakan untuk menyimpan result
videos = {}
@handler.add(PostbackEvent)
def handle_postback(event):
if isinstance(event.source, SourceGroup):
kirim = event.source.sender_id
elif isinstance(event.source, SourceRoom):
kirim = event.source.room_id
else:
kirim = event.source.user_id
try:
if event.postback.data == 'test':
line_bot_api.reply_message(event.reply_token, TextSendMessage(text="Test berhasil"))
if event.postback.data[0] == '/':
data = event.postback.data[1:].split(" ",1)
if len(data) > 1:
cmd, args = data[0].lower(), data[1]
else:
cmd, args = data[0].lower(), ""
if cmd == 'ytdownload':
thumbnails, link = args.split(' ')
download_ini = download(thumbnails, link)
line_bot_api.reply_message(event.reply_token, download_ini)
elif cmd == 'yt':
data = args.split(" ",1)
if len(data) > 1:
mau, cari = data[0], data[1]
else:
mau, cari = data[0], ""
if mau == 'iya':
perintah.update({event.source.user_id:['Cari lagi', time.time()]})
try:
del videos[cari.replace('+',' ')]
except:pass
elif mau == 'tidak':
line_bot_api.reply_message(event.reply_token, TextSendMessage(text="Terima kasih ;)"))
try:
del videos[cari.replace('+',' ')]
except:pass
else:
cari, nomor = args.split(" ")
try:
video = videos[cari.replace('+',' ')]
batas = int(nomor) + 9
except:
videos.update({cari:[]})
for i in youtube(cari):
videos[cari].append(i)
video = videos[cari.replace('+',' ')]
batas = 19
jvideo = 50 - int(nomor)
res = list()
if jvideo > 10:
for i in video[int(nomor)+1:batas]:
res.append(i)
res.append(
BubbleContainer(
body=BoxComponent(
layout='vertical',
spacing='sm',
contents=[
ButtonComponent(
action=PostbackAction(
label='Selanjutnya',
text='Selain itu?',
data='/yt '+cari+' '+str(batas)
),
flex=1,
gravity='center'
)
]
)
)
)
video = res
else:
video = video[int(nomor)+1:]
line_bot_api.reply_message(event.reply_token, [
FlexSendMessage(
alt_text='Hasil pencarian: '+cari,
contents=CarouselContainer(
contents=video
)
),
TemplateSendMessage(
alt_text='Mau cari video lagi?',
template=ConfirmTemplate(
text='Mau cari video lagi?',
actions=[
PostbackAction(
label='Iya',
text='Iya',
data='/yt iya '+cari
),
PostbackAction(
label='Tidak',
text='Tidak',
data='/yt tidak '+cari
)
]
)
)
])
except Exception as e:
try:
et, ev, tb = sys.exc_info()
lineno = tb.tb_lineno
fn = tb.tb_frame.f_code.co_filename
if sender != owner:
line_bot_api.reply_message(event.reply_token, [
TextSendMessage(text='Oopps.. '+namaBot.capitalize()+' ada kesalahan kak :('),
TextSendMessage(text='Tapi tenang kak, laporan kesalahan ini terkirim ke owner untuk diperbaiki ;D')
]
)
line_bot_api.push_message(owner, TextSendMessage(text="[Expectation Failed] %s Line %i - %s"% (fn, lineno, str(e))))
except:
line_bot_api.push_message(owner, TextSendMessage(text="Undescribeable error detected!!"))
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
try:
if isinstance(event.source, SourceGroup):
kirim = event.source.sender_id
elif isinstance(event.source, SourceRoom):
kirim = event.source.room_id
else:
kirim = event.source.user_id
sender = event.source.user_id
def balas(args):
line_bot_api.reply_message(event.reply_token, TextSendMessage(text=args))
def cbalas(args):
line_bot_api.reply_message(event.reply_token,
TextSendMessage(
text=args,
quick_reply=[
QuickReplyButton(
action=MessageAction(
label="YouTube Search",
text="YouTube"
)
),
QuickReplyButton(
action=MessageAction(
label="Google Image Search",
text="Google Image"
)
)
]
)
)
def message(args):
line_bot_api.push_message(kirim, TextSendMessage(text=args))
def cmessage(args):
line_bot_api.push_message(kirim,
TextSendMessage(
text=args,
quick_reply=[
QuickReplyButton(
action=MessageAction(
label="YouTube Search",
text="YouTube"
)
),
QuickReplyButton(
action=MessageAction(
label="Google Image Search",
text="Google Image"
)
)
]
)
)
def img(args):
line_bot_api.push_message(kirim, ImageSendMessage(
original_content_url=args,
preview_image_url=args))
text = event.message.text
if sender in perintah:
komando, waktu = perintah[sender]
if komando == "Mau nonton YouTube":
try:
line_bot_api.reply_message(event.reply_token,
TextSendMessage(text='Tunggu sebentar ya kak '+panggil(sender)+', '+namaBot.capitalize()+' sedang mengumpulkan data.. ;)')
)
videos.update({text:[]})
for i in youtube(text):
videos[text].append(i)
hasil = videos[text][:9]
cari = text.split()
res = list()
for i in hasil:
res.append(i)
res.append(
BubbleContainer(
body=BoxComponent(
layout='vertical',
spacing='sm',
contents=[
ButtonComponent(
action=PostbackAction(
label='Selanjutnya',
text='Selain itu?',
data='/yt '+'+'.join(cari)+' 9'
),
flex=1,
gravity='center'
)
]
)
)
)
line_bot_api.push_message(kirim, [
FlexSendMessage(
alt_text='Hasil pencarian: '+text,
contents=CarouselContainer(
contents=res
)
),
TemplateSendMessage(
alt_text='Mau cari video lagi?',
template=ConfirmTemplate(
text='Mau cari video lagi?',
actions=[
PostbackAction(
label='Iya',
text='Iya',
data='/yt iya '+text
),
PostbackAction(
label='Tidak',
text='Tidak',
data='/yt tidak '+text
)
]
)
)
])
except Exception as e:
try:
et, ev, tb = sys.exc_info()
lineno = tb.tb_lineno
fn = tb.tb_frame.f_code.co_filename
if sender != owner:
line_bot_api.reply_message(event.reply_token, [
TextSendMessage(text='Oopps.. '+namaBot.capitalize()+' ada kesalahan kak :('),
TextSendMessage(text='Tapi tenang kak, laporan kesalahan ini terkirim ke owner untuk diperbaiki ;D')
]
)
line_bot_api.push_message(owner, TextSendMessage(text="[Expectation Failed] %s Line %i - %s"% (fn, lineno, str(e))))
except:
line_bot_api.push_message(owner, TextSendMessage(text="Undescribeable error detected!!"))
del perintah[sender]
elif komando == 'Cari lagi':
perintah.update({sender:['Mau nonton YouTube', time.time()]})
line_bot_api.reply_message(event.reply_token, TextSendMessage(text='Mau cari apa di YouTube?'))
elif komando == "Google Image":
try:
line_bot_api.reply_message(event.reply_token, gis(text))
except Exception as e:
try:
et, ev, tb = sys.exc_info()
lineno = tb.tb_lineno
fn = tb.tb_frame.f_code.co_filename
if sender != owner:
line_bot_api.reply_message(event.reply_token, [
TextSendMessage(text='Oopps.. '+namaBot.capitalize()+' ada kesalahan kak :('),
TextSendMessage(text='Tapi tenang kak, laporan kesalahan ini terkirim ke owner untuk diperbaiki ;D')
]
)
line_bot_api.push_message(owner, TextSendMessage(text="[Expectation Failed] %s Line %i - %s"% (fn, lineno, str(e))))
except:
line_bot_api.push_message(owner, TextSendMessage(text="Undescribeable error detected!!"))
del perintah[sender]
elif text == "Mau nonton YouTube":
perintah.update({sender:[text, time.time()]})
balas("Mau cari apa di YouTube?")
elif text == "=cmds" or text.lower() == "perintah":
buttons_template = TemplateSendMessage(
alt_text='Perintah-perintah',
template=ButtonsTemplate(
title='PERINTAH',
text="Ini pilihan perintah yang bisa kamu pakai",
actions=[
MessageTemplateAction(
label='Youtube Search',
text='YouTube'
),
MessageTemplateAction(
label='Google Image Search',
text='Google Image'
),
MessageTemplateAction(
label='Owner',
text='Owner'
)
]
)
)
line_bot_api.reply_message(event.reply_token, buttons_template)
elif text[0] == "=" or "rion" in text:
data = text[1:].split(" ",1)
if len(data) > 1:
cmd, args = data[0].lower(), data[1]
else:
cmd, args = data[0].lower(), ""
############ COMMAND STARTS HERE ############
if cmd == "yt":
if args == "":
balas("Type =yt <your keyword> to search a youtube video.")
return
try:
line_bot_api.reply_message(event.reply_token, youtube(args))
except:cbalas(args+" tidak ketemu di youtube :<")
elif cmd == "gis":
try:
if args == "":
balas("Type =gis <your keyword> to search an image.")
return
line_bot_api.reply_message(event.reply_token, gis(args))
except Exception as e:
try:
et, ev, tb = sys.exc_info()
lineno = tb.tb_lineno
fn = tb.tb_frame.f_code.co_filename
if sender != owner:
line_bot_api.reply_message(event.reply_token, [
TextSendMessage(text='Oopps.. '+namaBot.capitalize()+' ada kesalahan kak :('),
TextSendMessage(text='Tapi tenang kak, laporan kesalahan ini terkirim ke owner untuk diperbaiki ;D')
]
)
line_bot_api.push_message(owner, TextSendMessage(text="[Expectation Failed] %s Line %i - %s"% (fn, lineno, str(e))))
except:
line_bot_api.push_message(owner, TextSendMessage(text="Undescribeable error detected!!"))
except LineBotApiError as e:
print(e.status_code)
print(e.error.message)
print(e.error.details)