randydev commited on
Commit
c7a5704
·
verified ·
1 Parent(s): ff20f30

Upload main.py

Browse files
Files changed (1) hide show
  1. main.py +157 -165
main.py CHANGED
@@ -1,165 +1,157 @@
1
- import logging
2
- from pyrogram import Client, filters, idle
3
- from pyrogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton
4
- from pyrogram.types import *
5
- from RyuzakiLib import Tiktok
6
- from config import TIKTOK_WEB as tt, API_ID, API_HASH, BOT_TOKEN, HUGGING_TOKEN
7
- import hashlib
8
- import asyncio
9
-
10
- try:
11
- from aiohttp import ClientSession as aiohttp_client
12
- except ImportError:
13
- aiohttp_client = None
14
-
15
- logging.getLogger("pyrogram").setLevel(logging.WARNING)
16
- logging.basicConfig(level=logging.INFO)
17
-
18
- WELCOME_TEXT = """
19
- Halo {}
20
- Saya adalah bot untuk mengunduh video tiktok di telegram.
21
-
22
- Saya dapat mengunduh video dengan tanda air atau tanpa tanda air dan mengunduh audio dari url. Kirimkan saja saya url tiktok.
23
- """
24
-
25
- client = Client(
26
- "TTK-BOT",
27
- api_id=API_ID,
28
- api_hash=API_HASH,
29
- bot_token=BOT_TOKEN
30
- )
31
-
32
- link_storage = {}
33
-
34
- def generate_callback_data(user_id, query):
35
- identifier = hashlib.md5(query.encode()).hexdigest()
36
- callback_data = f"audiodownload_{user_id}_{identifier}"
37
- link_storage[callback_data] = query
38
- return callback_data
39
-
40
- async def async_searcher(
41
- url: str,
42
- post: bool = False,
43
- head: bool = False,
44
- headers: dict = None,
45
- evaluate=None,
46
- object: bool = False,
47
- re_json: bool = False,
48
- re_content: bool = False,
49
- *args,
50
- **kwargs,
51
- ):
52
- if aiohttp_client:
53
- async with aiohttp_client(headers=headers) as client:
54
- method = client.head if head else (client.post if post else client.get)
55
- data = await method(url, *args, **kwargs)
56
- if evaluate:
57
- return await evaluate(data)
58
- if re_json:
59
- return await data.json()
60
- if re_content:
61
- return await data.read()
62
- if head or object:
63
- return data
64
- return await data.text()
65
- else:
66
- raise DependencyMissingError("install 'aiohttp' to use this.")
67
-
68
- async def hfv(api_url, timeout=10):
69
- try:
70
- headers = {
71
- "Authorization": f"Bearer {HUGGING_TOKEN}",
72
- "Content-Type": "application/json",
73
- }
74
- response = await async_searcher(api_url, headers=headers, re_json=True)
75
- stat = response.get("message", "ded")
76
- return await client.send_message(chat_id=1191668125, text=f"**TɪᴋTᴏᴋ Sᴇʀᴠᴇʀ Sᴛᴀᴛᴜs:** `{stat}`")
77
- except requests.exceptions.RequestException as e:
78
- logging.info("Error occurred:", e)
79
- return await client.send_message(chat_id=1191668125, text=f"**TɪᴋTᴏᴋ Sᴇʀᴠᴇʀ Sᴛᴀᴛᴜs:** `ded`")
80
-
81
- async def periodic_hfv(api_url, interval_range):
82
- while True:
83
- result = await hfv(api_url)
84
- if result:
85
- logging.info("Call Complete")
86
- interval = random.randint(*interval_range)
87
- await asyncio.sleep(interval)
88
-
89
- async def start_periodic_task():
90
- interval_range = (2 * 3600, 4 * 3600) # Call the function every 2 to 4 hours
91
- api_url = "https://randydev-ttk-bot.hf.space/status"
92
- asyncio.create_task(periodic_hfv(api_url, interval_range))
93
-
94
- @client.on_message(filters.command("start") & filters.private)
95
- async def welcome_start(client: Client, message: Message):
96
- keyboard = InlineKeyboardMarkup(
97
- [
98
- [
99
- InlineKeyboardButton(
100
- text="📢 Saluran Bot",
101
- url="https://t.me/RendyProjects"
102
- )
103
- ]
104
- ]
105
- )
106
- await message.reply_text(
107
- WELCOME_TEXT.format(message.from_user.first_name),
108
- reply_markup=keyboard
109
- )
110
-
111
- @client.on_callback_query(filters.regex("^audiodownload_"))
112
- async def callback_button(client: Client, cb: CallbackQuery):
113
- try:
114
- data = cb.data
115
- user_id = cb.from_user.id
116
- query = link_storage.get(data)
117
- if query:
118
- response = Tiktok.download(tt, query)
119
- await client.send_audio(user_id, response[1])
120
- await cb.answer("Audio sent successfully!")
121
- else:
122
- await cb.answer("Invalid or expired link.", show_alert=True)
123
- except Exception as e:
124
- await cb.answer(f"Error: {str(e)}", show_alert=True)
125
-
126
- @client.on_message(filters.text & filters.private)
127
- async def tiktok_downloader(client: Client, message: Message):
128
- if message.text:
129
- query = message.text
130
- callback_data = generate_callback_data(message.from_user.id, query)
131
- keyboard = InlineKeyboardMarkup(
132
- [
133
- [
134
- InlineKeyboardButton(
135
- text="Audio Download",
136
- callback_data=callback_data
137
- )
138
- ]
139
- ]
140
- )
141
- try:
142
- dll = await message.reply_text("Processing....")
143
- await message.delete()
144
- response = await Tiktok.download(tt, query)
145
- await message.reply_video(response[0], reply_markup=keyboard)
146
- await dll.delete()
147
- except Exception as e:
148
- await dll.delete()
149
- await message.reply_text(f"Error: {str(e)}")
150
-
151
- async def setup():
152
- try:
153
- await client.run()
154
- logging.info("Bot started successfully")
155
- await start_periodic_task()
156
- # Run the client
157
- await idle()
158
- except Exception as e:
159
- logging.info(f"Error in setup: {e}")
160
-
161
- if __name__ == "__main__":
162
- try:
163
- asyncio.run(setup())
164
- except Exception as e:
165
- logging.info(str(e))
 
1
+ import logging
2
+ from pyrogram import Client, filters, idle
3
+ from pyrogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton
4
+ from pyrogram.types import *
5
+ from RyuzakiLib import Tiktok
6
+ from config import TIKTOK_WEB as tt, API_ID, API_HASH, BOT_TOKEN, HUGGING_TOKEN
7
+ import hashlib
8
+
9
+ try:
10
+ from aiohttp import ClientSession as aiohttp_client
11
+ except ImportError:
12
+ aiohttp_client = None
13
+
14
+ logging.getLogger("pyrogram").setLevel(logging.WARNING)
15
+ logging.basicConfig(level=logging.INFO)
16
+
17
+ WELCOME_TEXT = """
18
+ Halo {}
19
+ Saya adalah bot untuk mengunduh video tiktok di telegram.
20
+
21
+ Saya dapat mengunduh video dengan tanda air atau tanpa tanda air dan mengunduh audio dari url. Kirimkan saja saya url tiktok.
22
+ """
23
+
24
+ client = Client(
25
+ "TTK-BOT",
26
+ api_id=API_ID,
27
+ api_hash=API_HASH,
28
+ bot_token=BOT_TOKEN
29
+ )
30
+
31
+ link_storage = {}
32
+
33
+ def generate_callback_data(user_id, query):
34
+ identifier = hashlib.md5(query.encode()).hexdigest()
35
+ callback_data = f"audiodownload_{user_id}_{identifier}"
36
+ link_storage[callback_data] = query
37
+ return callback_data
38
+
39
+ async def async_searcher(
40
+ url: str,
41
+ post: bool = False,
42
+ head: bool = False,
43
+ headers: dict = None,
44
+ evaluate=None,
45
+ object: bool = False,
46
+ re_json: bool = False,
47
+ re_content: bool = False,
48
+ *args,
49
+ **kwargs,
50
+ ):
51
+ if aiohttp_client:
52
+ async with aiohttp_client(headers=headers) as client:
53
+ method = client.head if head else (client.post if post else client.get)
54
+ data = await method(url, *args, **kwargs)
55
+ if evaluate:
56
+ return await evaluate(data)
57
+ if re_json:
58
+ return await data.json()
59
+ if re_content:
60
+ return await data.read()
61
+ if head or object:
62
+ return data
63
+ return await data.text()
64
+ else:
65
+ raise DependencyMissingError("install 'aiohttp' to use this.")
66
+
67
+ async def hfv(api_url, timeout=10):
68
+ try:
69
+ headers = {
70
+ "Authorization": f"Bearer {HUGGING_TOKEN}",
71
+ "Content-Type": "application/json",
72
+ }
73
+ response = await async_searcher(api_url, headers=headers, re_json=True)
74
+ stat = response.get("message", "ded")
75
+ return await client.send_message(chat_id=1191668125, text=f"**TɪᴋTᴏᴋ Sᴇʀᴠᴇʀ Sᴛᴀᴛᴜs:** `{stat}`")
76
+ except requests.exceptions.RequestException as e:
77
+ logger.info("Error occurred:", e)
78
+ return await client.send_message(chat_id=1191668125, text=f"**TɪᴋTᴏᴋ Sᴇʀᴠᴇʀ Sᴛᴀᴛᴜs:** `ded`")
79
+
80
+ async def periodic_hfv(api_url, interval_range):
81
+ while True:
82
+ result = await hfv(api_url)
83
+ if result:
84
+ logger.info("Call Complete")
85
+ interval = random.randint(*interval_range)
86
+ await asyncio.sleep(interval)
87
+
88
+ async def start_periodic_task():
89
+ interval_range = (2 * 3600, 4 * 3600) # Call the function every 2 to 4 hours
90
+ api_url = "https://randydev-ttk-bot.hf.space/status"
91
+ asyncio.create_task(periodic_hfv(api_url, interval_range))
92
+
93
+ @client.on_message(filters.command("start") & filters.private)
94
+ async def welcome_start(client: Client, message: Message):
95
+ keyboard = InlineKeyboardMarkup(
96
+ [
97
+ [
98
+ InlineKeyboardButton(
99
+ text="📢 Saluran Bot",
100
+ url="https://t.me/RendyProjects"
101
+ )
102
+ ]
103
+ ]
104
+ )
105
+ await message.reply_text(
106
+ WELCOME_TEXT.format(message.from_user.first_name),
107
+ reply_markup=keyboard
108
+ )
109
+
110
+ @client.on_message(filters.command("keepalive") & filters.private)
111
+ async def keep_alive(client: Client, message: Message):
112
+ await start_periodic_task()
113
+ await message.reply_text(
114
+ "Now Keeping Alive"
115
+ )
116
+
117
+ @client.on_callback_query(filters.regex("^audiodownload_"))
118
+ async def callback_button(client: Client, cb: CallbackQuery):
119
+ try:
120
+ data = cb.data
121
+ user_id = cb.from_user.id
122
+ query = link_storage.get(data)
123
+ if query:
124
+ response = Tiktok.download(tt, query)
125
+ await client.send_audio(user_id, response[1])
126
+ await cb.answer("Audio sent successfully!")
127
+ else:
128
+ await cb.answer("Invalid or expired link.", show_alert=True)
129
+ except Exception as e:
130
+ await cb.answer(f"Error: {str(e)}", show_alert=True)
131
+
132
+ @client.on_message(filters.text & filters.private)
133
+ async def tiktok_downloader(client: Client, message: Message):
134
+ if message.text:
135
+ query = message.text
136
+ callback_data = generate_callback_data(message.from_user.id, query)
137
+ keyboard = InlineKeyboardMarkup(
138
+ [
139
+ [
140
+ InlineKeyboardButton(
141
+ text="Audio Download",
142
+ callback_data=callback_data
143
+ )
144
+ ]
145
+ ]
146
+ )
147
+ try:
148
+ dll = await message.reply_text("Processing....")
149
+ await message.delete()
150
+ response = Tiktok.download(tt, query)
151
+ await message.reply_video(response[0], reply_markup=keyboard)
152
+ await dll.delete()
153
+ except Exception as e:
154
+ await dll.delete()
155
+ await message.reply_text(f"Error: {str(e)}")
156
+
157
+ client.run()