-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
255 lines (218 loc) · 11.7 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
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
import telebot
from telebot import types
from telebot.types import InlineKeyboardMarkup
from config import TELEGRAM_TOKEN
from logger import logger
from downloader import youtubeDownloader, InstagramDownloader, FacebookDownloader, TiktokDownloader, download_video
import threading
import uuid
import os
bot = telebot.TeleBot(TELEGRAM_TOKEN)
zenn_state = {}
url_map = {}
# Event to stop the monitoring thread
monitoring_stop_event = threading.Event()
# function untuk menghasilkan ID
def generate_id():
return str(uuid.uuid4())
# simpan logs
def save_logs(message, platform, ):
chat_id = message.chat.id
username = message.from_user.username
nama_depan = message.from_user.first_name
nama_belakang = message.from_user.last_name
text = message.text
logger.info(f"Berhasil Download : {nama_depan}{nama_belakang} - @{username}({chat_id}): url: {text} , Platform : {platform}")
# /start menu
@bot.message_handler(commands=['start'])
def mulai(message):
if message.chat.id in zenn_state:
del zenn_state[message.chat.id]
logger.info(f"Pesan /start dari {message.from_user.first_name} - {message.from_user.username}({message.chat.id})")
markup = types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=True)
markup.add("Youtube", "Instagram")
markup.add("Facebook", "Tiktok")
bot.reply_to(message, "Selamat datang di Zenn Downloader. Silakan pilih menu:", reply_markup=markup)
# destroy sesi
@bot.message_handler(commands=['exit'])
def keluar_sesi(message):
if message.chat.id in zenn_state:
del zenn_state[message.chat.id]
bot.reply_to(message, "Sesi telah dihentikan. Silakan pilih menu lain.")
else:
bot.reply_to(message, "Tidak ada sesi yang sedang berjalan.")
# Handler untuk URL
@bot.message_handler(func=lambda message: message.text in ["Youtube", "Instagram", "Facebook", "Tiktok"])
def masukan_url(message):
if message.chat.id in zenn_state:
sesi_aktif = zenn_state[message.chat.id]
bot.reply_to(message, f"Kamu sedang dalam sesi '{sesi_aktif}'. Selesaikan dulu atau ketik /exit untuk keluar.\n\n*Masukan URL {sesi_aktif}:*" , parse_mode="Markdown")
return
zenn_state[message.chat.id] = message.text.strip()
bot.reply_to(message, "Masukkan URL:")
# Handler untuk mendownload file
@bot.message_handler(func=lambda message: message.chat.id in zenn_state)
def download_file(message):
markup = types.InlineKeyboardMarkup()
platform = zenn_state[message.chat.id]
match platform:
# Youtube Downloader
case "Youtube":
pesan_downloading = bot.reply_to(message, "Downloading YouTube...")
data = youtubeDownloader(message.text.strip())
if data.get("result"):
try:
judul_video = data["result"]["title"]
video_kualitas = data["resultUrl"]["video"]
audio_kualitas = data["resultUrl"]["audio"]
durasi_video = data["result"]["duration"]
bot.reply_to(message, f"*Judul:* {judul_video}\n*Durasi:* {durasi_video}", parse_mode="Markdown")
for kualitas in video_kualitas:
id_unik = generate_id()
url_map[id_unik] = kualitas['download']
kualitas_button = types.InlineKeyboardButton(
text=f"{kualitas['quality']} - {kualitas['size']}",
callback_data=id_unik
)
markup.add(kualitas_button)
for kualitas in audio_kualitas:
id_unik = generate_id()
url_map[id_unik] = kualitas['download']
kualitas_button_audio = types.InlineKeyboardButton(
text=f"{kualitas['quality']} - {kualitas['size']}",
callback_data=id_unik
)
markup.add(kualitas_button_audio)
bot.delete_message(chat_id=message.chat.id, message_id=pesan_downloading.message_id)
bot.reply_to(message, "Pilih kualitas video atau audio:", reply_markup=markup)
save_logs(message, "Youtube")
except Exception as e:
bot.delete_message(chat_id=message.chat.id, message_id=pesan_downloading.message_id)
bot.reply_to(message, f"Terjadi kesalahan saat mengambil data: {str(e)}")
else:
bot.reply_to(message, "Ada yang salah, coba lagi")
# Instagram Downloader
case "Instagram":
pesan_downloading = bot.reply_to(message, "Downloading Instagram...")
try:
data = InstagramDownloader(message.text.strip())
if data.get("data"):
judul = data["data"]["meta"]["title"]
url_original = data["data"]["meta"]["source"]
download = data["data"]["url"][0]["url"]
caption = f"*Judul:* {judul}\n*URL:* {url_original}"
nama_file = download_video(download)
if nama_file.lower().endswith('.html'):
bot.send_message(message.chat.id, "Terjadi kesalahan saat mengunduh video.")
bot.delete_message(chat_id=message.chat.id, message_id=pesan_downloading.message_id)
else:
with open(nama_file, 'rb') as video_file:
bot.send_video(message.chat.id, video_file, caption=caption, parse_mode="Markdown")
bot.delete_message(chat_id=message.chat.id, message_id=pesan_downloading.message_id)
os.remove(nama_file)
del zenn_state[message.chat.id]
save_logs(message, "Instagram")
else:
bot.delete_message(chat_id=message.chat.id, message_id=pesan_downloading.message_id)
bot.reply_to(message, "Ada yang salah, coba lagi")
del zenn_state[message.chat.id]
return
except Exception as e:
bot.delete_message(chat_id=message.chat.id, message_id=pesan_downloading.message_id)
bot.reply_to(message, f"Terjadi kesalahan saat mengambil data: {str(e)}")
del zenn_state[message.chat.id]
return
# Facebook Downloader
case "Facebook":
pesan_downloading = bot.reply_to(message, "Downloading Facebook...")
try:
data = FacebookDownloader(message.text.strip())
if data.get("data"):
download = data["data"][0]["url"]
# print(download)
nama_file = download_video(download)
if nama_file.lower().endswith('.html'):
bot.send_message(message.chat.id, "Terjadi kesalahan saat mengunduh video.")
bot.delete_message(chat_id=message.chat.id, message_id=pesan_downloading.message_id)
else:
with open(nama_file, 'rb') as video_file:
bot.send_video(message.chat.id, video_file)
bot.delete_message(chat_id=message.chat.id, message_id=pesan_downloading.message_id)
os.remove(nama_file)
del zenn_state[message.chat.id]
save_logs(message, "Facebook")
else:
bot.delete_message(chat_id=message.chat.id, message_id=pesan_downloading.message_id)
bot.reply_to(message, "Ada yang salah, coba lagi")
del zenn_state[message.chat.id]
return
except Exception as e:
bot.delete_message(chat_id=message.chat.id, message_id=pesan_downloading.message_id)
bot.reply_to(message, f"Terjadi kesalahan saat mengambil data: {str(e)}")
# Tiktok Downloader
case "Tiktok":
pesan_downloading = bot.reply_to(message, "Downloading Tiktok..")
try:
data = TiktokDownloader(message.text.strip())
# print(data)
if data.get("data"):
judul = data["data"]["title"]
uploader = data["data"]["author"]
download = data["data"]["nowm"]
caption = f"*Judul:* {judul}\n*Uploader:* {uploader}"
nama_file = download_video(download)
if nama_file.lower().endswith('.html'):
bot.send_message(message.chat.id, "Terjadi kesalahan saat mengunduh video.")
bot.delete_message(chat_id=message.chat.id, message_id=pesan_downloading.message_id)
else:
with open(nama_file, 'rb') as video_file:
bot.send_video(message.chat.id, video_file, caption=caption, parse_mode="Markdown")
bot.delete_message(chat_id=message.chat.id, message_id=pesan_downloading.message_id)
os.remove(nama_file)
del zenn_state[message.chat.id]
save_logs(message, "Tiktok")
else:
bot.delete_message(chat_id=message.chat.id, message_id=pesan_downloading.message_id)
bot.reply_to(message, "Ada yang salah, coba lagi")
del zenn_state[message.chat.id]
return
except Exception as e:
print(e)
bot.delete_message(chat_id=message.chat.id, message_id=pesan_downloading.message_id)
bot.reply_to(message, f"Terjadi kesalahan saat mengambil data: {str(e)}")
del zenn_state[message.chat.id]
return
case _:
bot.reply_to(message, "Platform tidak dikenali.")
# Callback untuk youtube
@bot.callback_query_handler(func=lambda call: True)
def handle_callback(call):
try:
url = url_map.get(call.data)
if url:
pesan_memproses = bot.send_message(call.message.chat.id, "Sedang memproses...")
bot.delete_message(chat_id=call.message.chat.id, message_id=call.message.message_id)
nama_file = download_video(url)
print(f"File path: {nama_file}")
if nama_file.lower().endswith('.html'):
bot.send_message(call.message.chat.id, "Terjadi kesalahan saat mengunduh video.")
else:
with open(nama_file, 'rb') as video_file:
bot.send_video(call.message.chat.id, video_file)
bot.delete_message(chat_id=call.message.chat.id, message_id=pesan_memproses.message_id)
del url_map[call.data]
del zenn_state[call.message.chat.id]
os.remove(nama_file)
else:
bot.answer_callback_query(call.id, "URL tidak ditemukan.")
del zenn_state[call.message.chat.id]
except Exception as e:
del zenn_state[call.message.chat.id]
bot.reply_to(call.message, f"Terjadi kesalahan saat mengunduh video: {str(e)}")
# Pesan tidak di kenal
@bot.message_handler(func=lambda message: True)
def unknown_message(message):
bot.reply_to(message, "Maaf, pesan tidak dikenali.")
# Memulai bot
print("Bot berjalan...")
bot.infinity_polling()