forked from X-Gorn/TikTokDL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
139 lines (128 loc) · 4.48 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
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/{}/'
#Button
START_BUTTONS=[
[
InlineKeyboardButton('Source', url='https://github.com/X-Gorn/TikTokDL'),
InlineKeyboardButton('Project Channel', url='https://t.me/xTeamBots'),
],
[InlineKeyboardButton('Author', url='https://t.me/xgorn')],
]
DL_BUTTONS=[
[
InlineKeyboardButton('No Watermark', callback_data='nowm'),
InlineKeyboardButton('Watermark', callback_data='wm'),
],
[InlineKeyboardButton('Audio', callback_data='audio')],
]
# Running bot
xbot = Client('TikTokDL', api_id=APP_ID, api_hash=API_HASH, bot_token=BOT_TOKEN)
# Helpers
# Thanks to FridayUB
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,
)
# Start
@xbot.on_message(filters.command('start') & filters.private)
async def _start(bot, update):
await update.reply_text(f"I'm TikTokDL!\nYou can download tiktok video/audio using this bot", True, reply_markup=InlineKeyboardMarkup(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('Select the options below', 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()