-
Notifications
You must be signed in to change notification settings - Fork 396
/
main.py
131 lines (112 loc) · 5.11 KB
/
main.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
# Copyright 2021 TerminalWarlord under the terms of the MIT
# license found at https://github.com/TerminalWarlord/TikTok-Downloader-Bot/blob/master/LICENSE
# Encoding = 'utf-8'
# Fork and Deploy, do not modify this repo and claim it yours
# For collaboration mail me at [email protected]
from pyrogram import Client, filters
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, InlineQueryResultArticle, InputTextMessageContent
import shutil
import requests
import json
import os
import re
from bs4 import BeautifulSoup as bs
import time
from datetime import timedelta
import math
import base64
from progress_bar import progress, TimeFormatter, humanbytes
from dotenv import load_dotenv
load_dotenv()
bot_token = os.environ.get('BOT_TOKEN')
workers = int(os.environ.get('WORKERS'))
api = int(os.environ.get('API_KEY'))
hash = os.environ.get('API_HASH')
chnnl = os.environ.get('CHANNEL_URL')
BOT_URL = os.environ.get('BOT_URL')
app = Client("JayBee", bot_token=bot_token, api_id=api, api_hash=hash, workers=workers)
@app.on_message(filters.command('start'))
def start(client, message):
kb = [[InlineKeyboardButton('Channel 🛡', url=chnnl),InlineKeyboardButton('Repo 🔰', url="https://github.com/TerminalWarlord/TikTok-Downloader-Bot/")]]
reply_markup = InlineKeyboardMarkup(kb)
app.send_message(chat_id=message.from_user.id, text=f"Hello there, I am **TikTok Downloader Bot**.\nI can download TikTok video without Watermark.\n\n"
"__**Developer :**__ __@JayBeeDev__\n"
"__**Language :**__ __Python__\n"
"__**Framework :**__ __🔥 Pyrogram__",
parse_mode='md',
reply_markup=reply_markup)
@app.on_message(filters.command('help'))
def help(client, message):
kb = [[InlineKeyboardButton('Channel 🛡', url=chnnl),InlineKeyboardButton('Repo 🔰', url="https://github.com/TerminalWarlord/TikTok-Downloader-Bot/")]]
reply_markup = InlineKeyboardMarkup(kb)
app.send_message(chat_id=message.from_user.id, text=f"Hello there, I am **TikTok Downloader Bot**.\nI can download any TikTok video from a given link.\n\n"
"__Send me a TikTok video link__",
parse_mode='md',
reply_markup=reply_markup)
@app.on_message((filters.regex("http://")|filters.regex("https://")) & (filters.regex('tiktok')|filters.regex('douyin')))
def tiktok_dl(client, message):
a = app.send_message(chat_id=message.chat.id,
text='__Downloading File to the Server__',
parse_mode='md')
link = re.findall(r'\bhttps?://.*[(tiktok|douyin)]\S+', message.text)[0]
link = link.split("?")[0]
params = {
"link": link
}
headers = {
'x-rapidapi-host': "tiktok-info.p.rapidapi.com",
'x-rapidapi-key': "f9d65af755msh3c8cac23b52a5eep108a33jsnbf7de971bb72"
}
### Get your Free TikTok API from https://rapidapi.com/TerminalWarlord/api/tiktok-info/
#Using the default one can stop working any moment
api = f"https://tiktok-info.p.rapidapi.com/dl/"
r = requests.get(api, params=params, headers=headers).json()['videoLinks']['download']
directory = str(round(time.time()))
filename = str(int(time.time()))+'.mp4'
size = int(requests.head(r).headers['Content-length'])
total_size = "{:.2f}".format(int(size) / 1048576)
try:
os.mkdir(directory)
except:
pass
with requests.get(r, timeout=(50, 10000), stream=True) as r:
r.raise_for_status()
with open(f'./{directory}/{filename}', 'wb') as f:
chunk_size = 1048576
dl = 0
show = 1
for chunk in r.iter_content(chunk_size=chunk_size):
f.write(chunk)
dl = dl + chunk_size
percent = round(dl * 100 / size)
if percent > 100:
percent = 100
if show == 1:
try:
a.edit(f'__**URL :**__ __{message.text}__\n'
f'__**Total Size :**__ __{total_size} MB__\n'
f'__**Downloaded :**__ __{percent}%__\n',
disable_web_preview=False)
except:
pass
if percent == 100:
show = 0
a.edit(f'__Downloaded to the server!\n'
f'Uploading to Telegram Now ⏳__')
start = time.time()
title = filename
app.send_document(chat_id=message.chat.id,
document=f"./{directory}/{filename}",
caption=f"**File :** __{filename}__\n"
f"**Size :** __{total_size} MB__\n\n"
f"__Uploaded by @{BOT_URL}__",
file_name=f"{directory}",
parse_mode='md',
progress=progress,
progress_args=(a, start, title))
a.delete()
try:
shutil.rmtree(directory)
except:
pass
app.run()