randydev commited on
Commit
3e66391
·
verified ·
1 Parent(s): b56ab53

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +160 -160
main.py CHANGED
@@ -1,160 +1,160 @@
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_callback_query(filters.regex("^audiodownload_"))
111
- async def callback_button(client: Client, cb: CallbackQuery):
112
- try:
113
- data = cb.data
114
- user_id = cb.from_user.id
115
- query = link_storage.get(data)
116
- if query:
117
- response = Tiktok.download(tt, query)
118
- await client.send_audio(user_id, response[1])
119
- await cb.answer("Audio sent successfully!")
120
- else:
121
- await cb.answer("Invalid or expired link.", show_alert=True)
122
- except Exception as e:
123
- await cb.answer(f"Error: {str(e)}", show_alert=True)
124
-
125
- @client.on_message(filters.text & filters.private)
126
- async def tiktok_downloader(client: Client, message: Message):
127
- if message.text:
128
- query = message.text
129
- callback_data = generate_callback_data(message.from_user.id, query)
130
- keyboard = InlineKeyboardMarkup(
131
- [
132
- [
133
- InlineKeyboardButton(
134
- text="Audio Download",
135
- callback_data=callback_data
136
- )
137
- ]
138
- ]
139
- )
140
- try:
141
- dll = await message.reply_text("Processing....")
142
- await message.delete()
143
- response = Tiktok.download(tt, query)
144
- await message.reply_video(response[0], reply_markup=keyboard)
145
- await dll.delete()
146
- except Exception as e:
147
- await dll.delete()
148
- await message.reply_text(f"Error: {str(e)}")
149
-
150
- async def setup():
151
- try:
152
- await client.start()
153
- logger.info("Bot started successfully")
154
- await start_periodic_task()
155
- # Run the client
156
- await idle()
157
- except Exception as e:
158
- logger.error(f"Error in setup: {e}")
159
-
160
- client.run(setup())
 
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_callback_query(filters.regex("^audiodownload_"))
111
+ async def callback_button(client: Client, cb: CallbackQuery):
112
+ try:
113
+ data = cb.data
114
+ user_id = cb.from_user.id
115
+ query = link_storage.get(data)
116
+ if query:
117
+ response = Tiktok.download(tt, query)
118
+ await client.send_audio(user_id, response[1])
119
+ await cb.answer("Audio sent successfully!")
120
+ else:
121
+ await cb.answer("Invalid or expired link.", show_alert=True)
122
+ except Exception as e:
123
+ await cb.answer(f"Error: {str(e)}", show_alert=True)
124
+
125
+ @client.on_message(filters.text & filters.private)
126
+ async def tiktok_downloader(client: Client, message: Message):
127
+ if message.text:
128
+ query = message.text
129
+ callback_data = generate_callback_data(message.from_user.id, query)
130
+ keyboard = InlineKeyboardMarkup(
131
+ [
132
+ [
133
+ InlineKeyboardButton(
134
+ text="Audio Download",
135
+ callback_data=callback_data
136
+ )
137
+ ]
138
+ ]
139
+ )
140
+ try:
141
+ dll = await message.reply_text("Processing....")
142
+ await message.delete()
143
+ response = await Tiktok.download(tt, query)
144
+ await message.reply_video(response[0], reply_markup=keyboard)
145
+ await dll.delete()
146
+ except Exception as e:
147
+ await dll.delete()
148
+ await message.reply_text(f"Error: {str(e)}")
149
+
150
+ async def setup():
151
+ try:
152
+ await client.start()
153
+ logger.info("Bot started successfully")
154
+ await start_periodic_task()
155
+ # Run the client
156
+ await idle()
157
+ except Exception as e:
158
+ logger.error(f"Error in setup: {e}")
159
+
160
+ client.run(setup())