-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.py
202 lines (188 loc) · 6.53 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
import json, requests, os, shlex, asyncio, uuid, shutil
from typing import Tuple
from pyrogram import Client, filters
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup, CallbackQuery
# Configs
API_HASH = os.environ['API_HASH']
APP_ID = int(os.environ['APP_ID'])
BOT_TOKEN = os.environ['BOT_TOKEN']
downloads = './downloads/{}/'
# Helper
async def run_cmd(cmd: str) -> Tuple[str, str, int, int]:
args = shlex.split(cmd)
process = await asyncio.create_subprocess_exec(
*args, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await process.communicate()
return (
stdout.decode("utf-8", "replace").strip(),
stderr.decode("utf-8", "replace").strip(),
process.returncode,
process.pid,
)
# Button
START_TEXT = """
Hai {}, Saya adalah TikTok-DL-Bot. Anda dapat mengunduh video/audio TikTok menggunakan bot ini. Kirimkan saya tautan tiktok yang ingin di unduh.
Dibuat oleh @AnkiSatya
"""
HELP_TEXT = """
- Kirimkan saya tautan tiktok
- Pilih opsi yang anda inginkan
- Saya akan mengunduhnya
- Dan saya akan mengirim video/audio sesuai yang anda kirim
Dibuat oleh @AnkiSatya
"""
ABOUT_TEXT = """
- **Bot :** `TikTok DL Bot`
- **Pembuat :** [𝔸𝕟𝕜𝕚 𝕊𝕒𝕥𝕪𝕒](https://t.me/AnkiSatya)
- **Channel :** [𝕬𝖓𝖘𝖆𝖐𝖚 𝕭𝖔𝖙 𝕮𝖍𝖆𝖓𝖓𝖊𝖑](https://t.me/ansakubotchannel)
- **Repo :** [Github](https://github.com/Ansaku/TikTok-DL-Bot)
- **Donasi :** [Saweria](https://saweria.co/ansaku)
"""
START_BUTTONS = InlineKeyboardMarkup(
[[
InlineKeyboardButton('Channel', url='https://t.me/ansakubotchannel'),
InlineKeyboardButton('Author', url='https://t.me/AnkiSatya')
],[
InlineKeyboardButton('Help', callback_data='help'),
InlineKeyboardButton('About', callback_data='about'),
InlineKeyboardButton('Close', callback_data='close')
]]
)
HELP_BUTTONS = InlineKeyboardMarkup(
[[
InlineKeyboardButton('Home', callback_data='home'),
InlineKeyboardButton('About', callback_data='about'),
InlineKeyboardButton('Close', callback_data='close')
]]
)
ABOUT_BUTTONS = InlineKeyboardMarkup(
[[
InlineKeyboardButton('Home', callback_data='home'),
InlineKeyboardButton('Help', callback_data='help'),
InlineKeyboardButton('Close', callback_data='close')
]]
)
DL_BUTTONS=[
[
InlineKeyboardButton('No Watermark', callback_data='nowm'),
InlineKeyboardButton('Watermark', callback_data='wm'),
],
[InlineKeyboardButton('Audio', callback_data='audio')],
]
# Running bot
xbot = Client('TikTok-DL-Bot', api_id=APP_ID, api_hash=API_HASH, bot_token=BOT_TOKEN)
# Start
@xbot.on_callback_query()
async def cb_data(bot, update):
if update.data == "home":
await update.message.edit_text(
text=START_TEXT.format(update.from_user.mention),
reply_markup=START_BUTTONS,
disable_web_page_preview=True
)
elif update.data == "help":
await update.message.edit_text(
text=HELP_TEXT,
reply_markup=HELP_BUTTONS,
disable_web_page_preview=True
)
elif update.data == "about":
await update.message.edit_text(
text=ABOUT_TEXT,
reply_markup=ABOUT_BUTTONS,
disable_web_page_preview=True
)
else:
await update.message.delete()
@xbot.on_message(filters.private & filters.command(["start"]))
async def start(bot, update):
await update.reply_text(
text=START_TEXT.format(update.from_user.mention),
disable_web_page_preview=True,
reply_markup=START_BUTTONS
)
# Downloader for tiktok
@xbot.on_message(filters.regex(pattern='.*http.*') & filters.private)
async def _tiktok(bot, update):
url = update.text
session = requests.Session()
resp = session.head(url, allow_redirects=True)
if not 'tiktok.com' in resp.url:
return
await update.reply('Pilih opsi di bawah ini', True, reply_markup=InlineKeyboardMarkup(DL_BUTTONS))
# Callbacks
@xbot.on_callback_query()
async def _callbacks(bot, cb: CallbackQuery):
if cb.data == 'nowm':
dirs = downloads.format(uuid.uuid4().hex)
os.makedirs(dirs)
cbb = cb
update = cbb.message.reply_to_message
await cb.message.delete()
url = update.text
session = requests.Session()
resp = session.head(url, allow_redirects=True)
if '?' in resp.url:
tt = resp.url.split('?', 1)[0]
else:
tt = resp.url
ttid = dirs+tt.split('/')[-1]
r = requests.get('https://api.reiyuura.me/api/dl/tiktok?url='+tt)
result = r.text
rs = json.loads(result)
link = rs['result']['nowm']
resp = session.head(link, allow_redirects=True)
r = requests.get(resp.url, allow_redirects=True)
open(f'{ttid}.mp4', 'wb').write(r.content)
await bot.send_video(update.chat.id, f'{ttid}.mp4',)
shutil.rmtree(dirs)
elif cb.data == 'wm':
dirs = downloads.format(uuid.uuid4().hex)
os.makedirs(dirs)
cbb = cb
update = cbb.message.reply_to_message
await cb.message.delete()
url = update.text
session = requests.Session()
resp = session.head(url, allow_redirects=True)
if '?' in resp.url:
tt = resp.url.split('?', 1)[0]
else:
tt = resp.url
ttid = dirs+tt.split('/')[-1]
r = requests.get('https://api.reiyuura.me/api/dl/tiktok?url='+tt)
result = r.text
rs = json.loads(result)
link = rs['result']['wm']
resp = session.head(link, allow_redirects=True)
r = requests.get(resp.url, allow_redirects=True)
open(f'{ttid}.mp4', 'wb').write(r.content)
await bot.send_video(update.chat.id, f'{ttid}.mp4',)
shutil.rmtree(dirs)
elif cb.data == 'audio':
dirs = downloads.format(uuid.uuid4().hex)
os.makedirs(dirs)
cbb = cb
update = cbb.message.reply_to_message
await cb.message.delete()
url = update.text
session = requests.Session()
resp = session.head(url, allow_redirects=True)
if '?' in resp.url:
tt = resp.url.split('?', 1)[0]
else:
tt = resp.url
ttid = dirs+tt.split('/')[-1]
r = requests.get('https://api.reiyuura.me/api/dl/tiktok?url='+tt)
result = r.text
rs = json.loads(result)
link = rs['result']['wm']
resp = session.head(link, allow_redirects=True)
r = requests.get(resp.url, allow_redirects=True)
open(f'{ttid}.mp4', 'wb').write(r.content)
cmd = f'ffmpeg -i "{ttid}.mp4" -vn -ar 44100 -ac 2 -ab 192 -f mp3 "{ttid}.mp3"'
await run_cmd(cmd)
await bot.send_audio(update.chat.id, f'{ttid}.mp3',)
shutil.rmtree(dirs)
xbot.run()