add inline mode

fix #345
This commit is contained in:
Benny 2024-02-10 19:29:56 +01:00
parent ebd17fe9d4
commit 0f6f0cc34e
No known key found for this signature in database
GPG key ID: 6CD0DBDA5235D481
4 changed files with 92 additions and 5 deletions

View file

@ -40,6 +40,7 @@ If you need more downloads, you can buy download tokens.
11. cache mechanism - download once for the same video.
12. instagram posts(only available for my bot)
13. 4 GiB file size support with Telegram Premium
14. History and inline mode support
> If you download files larger than 2 GiB, you agreed that this file will be uploaded by me. I know who you are and what
> you download.
@ -285,6 +286,8 @@ unsub - Unsubscribe from YouTube Channel
sub_count - Check subscription status, owner only.
uncache - Delete cache for this link, owner only.
purge - Delete all tasks, owner only.
show_history - Show download history
clear_history - Clear download history
```
# Test data

View file

@ -225,6 +225,7 @@ class MySQL:
resolution varchar(128) null,
method varchar(64) null,
mode varchar(32) default 'Celery' null,
history varchar(10) default 'OFF' null,
constraint settings_pk
primary key (user_id)
);
@ -252,6 +253,14 @@ class MySQL:
is_valid boolean default 1 null
) CHARSET=utf8mb4;
"""
history_sql = """
create table if not exists history
(
user_id bigint null,
link varchar(256) null,
title varchar(512) null
) CHARSET=utf8mb4;
"""
def __init__(self):
try:
@ -273,6 +282,7 @@ class MySQL:
self.cur.execute(self.settings_sql)
self.cur.execute(self.channel_sql)
self.cur.execute(self.subscribe_sql)
self.cur.execute(self.history_sql)
self.con.commit()
def __del__(self):
@ -282,7 +292,7 @@ class MySQL:
self.cur.execute("SELECT * FROM settings WHERE user_id = %s", (user_id,))
data = self.cur.fetchone()
if data is None:
return 100, "high", "video", "Celery"
return 100, "high", "video", "Celery", "OFF"
return data
def set_user_settings(self, user_id: int, field: str, value: str):
@ -297,11 +307,31 @@ class MySQL:
if field == "method":
method = value
resolution = "high"
cur.execute("INSERT INTO settings VALUES (%s,%s,%s,%s)", (user_id, resolution, method, "Celery"))
cur.execute("INSERT INTO settings VALUES (%s,%s,%s,%s,%s)", (user_id, resolution, method, "Celery", "OFF"))
else:
cur.execute(f"UPDATE settings SET {field} =%s WHERE user_id = %s", (value, user_id))
self.con.commit()
def show_history(self, user_id: int):
self.cur.execute("SELECT link,title FROM history WHERE user_id = %s", (user_id,))
data = self.cur.fetchall()
return "\n".join([f"{i[0]} {i[1]}" for i in data])
def clear_history(self, user_id: int):
self.cur.execute("DELETE FROM history WHERE user_id = %s", (user_id,))
self.con.commit()
def add_history(self, user_id: int, link: str, title: str):
self.cur.execute("INSERT INTO history VALUES (%s,%s,%s)", (user_id, link, title))
self.con.commit()
def search_history(self, user_id: int, kw: str):
self.cur.execute("SELECT * FROM history WHERE user_id = %s AND title like %s", (user_id, f"%{kw}%"))
data = self.cur.fetchall()
if data:
return data
return None
class InfluxDB:
def __init__(self):

View file

@ -46,7 +46,7 @@ from config import (
FileTooBig,
)
from constant import BotText
from database import Redis
from database import Redis, MySQL
from downloader import edit_text, tqdm_progress, upload_hook, ytdl_download
from limit import Payment
from utils import (
@ -295,6 +295,10 @@ def ytdl_normal_download(client: Client, bot_msg: types.Message | typing.Any, ur
logging.info("Download complete.")
client.send_chat_action(chat_id, enums.ChatAction.UPLOAD_DOCUMENT)
bot_msg.edit_text("Download complete. Sending now...")
data = MySQL().get_user_settings(chat_id)
if data[4] == "ON":
logging.info("Adding to history...")
MySQL().add_history(chat_id, url, pathlib.Path(video_paths[0]).name)
try:
upload_processor(client, bot_msg, url, video_paths)
except pyrogram.errors.Flood as e:

View file

@ -226,6 +226,24 @@ def sub_count_handler(client: Client, message: types.Message):
client.send_document(chat_id, f)
@app.on_message(filters.command(["show_history"]))
def show_history(client: Client, message: types.Message):
chat_id = message.chat.id
client.send_chat_action(chat_id, enums.ChatAction.TYPING)
data = MySQL().show_history(chat_id)
if data:
client.send_message(chat_id, data, disable_web_page_preview=True)
else:
client.send_message(chat_id, "No history found.")
@app.on_message(filters.command(["clear_history"]))
def clear_history(client: Client, message: types.Message):
chat_id = message.chat.id
MySQL().clear_history(chat_id)
message.reply_text("History cleared.", quote=True)
@app.on_message(filters.command(["direct"]))
def direct_handler(client: Client, message: types.Message):
redis = Redis()
@ -249,9 +267,9 @@ def settings_handler(client: Client, message: types.Message):
payment = Payment()
client.send_chat_action(chat_id, enums.ChatAction.TYPING)
data = MySQL().get_user_settings(chat_id)
set_mode = data[-1]
set_mode = data[3]
text = {"Local": "Celery", "Celery": "Local"}.get(set_mode, "Local")
mode_text = f"Download mode: **{set_mode}**"
mode_text = f"Download mode: **{set_mode}**\nHistory record: {data[4]}"
if message.chat.username == OWNER or payment.get_pay_token(chat_id):
extra = [types.InlineKeyboardButton(f"Change download mode to {text}", callback_data=text)]
else:
@ -269,6 +287,9 @@ def settings_handler(client: Client, message: types.Message):
types.InlineKeyboardButton("Medium Quality", callback_data="medium"),
types.InlineKeyboardButton("Low Quality", callback_data="low"),
],
[
types.InlineKeyboardButton("Toggle History", callback_data=f"history-{data[4]}"),
],
extra,
]
)
@ -505,6 +526,35 @@ def download_resolution_callback(client: Client, callback_query: types.CallbackQ
callback_query.answer(f"Your default download quality was set to {callback_query.data}")
@app.on_callback_query(filters.regex(r"history.*"))
def set_history_callback(client: Client, callback_query: types.CallbackQuery):
chat_id = callback_query.message.chat.id
data = callback_query.data.split("-")[-1]
r = "OFF" if data == "ON" else "ON"
logging.info("Setting %s file type to %s", chat_id, data)
MySQL().set_user_settings(chat_id, "history", r)
callback_query.answer("History setting updated.")
@app.on_inline_query()
def inline_query(client: Client, inline_query: types.InlineQuery):
kw = inline_query.query
user_id = inline_query.from_user.id
data = MySQL().search_history(user_id, kw)
if data:
results = [
types.InlineQueryResultArticle(
id=str(i),
title=item[1],
description=item[2],
input_message_content=types.InputTextMessageContent(item[1]),
)
for i, item in enumerate(data)
]
client.answer_inline_query(inline_query.id, results)
@app.on_callback_query(filters.regex(r"convert"))
def audio_callback(client: Client, callback_query: types.CallbackQuery):
redis = Redis()