From 5a4fb2279dbfd5dbbef98b846c10113ba3ed533f Mon Sep 17 00:00:00 2001 From: trueold89 Date: Tue, 31 Oct 2023 02:37:37 +0300 Subject: [PATCH] Add file function --- bot.py | 42 ++++++++++++++++++++++++++++++++++++++++++ func.py | 11 ++++++++--- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/bot.py b/bot.py index 6f3eda7..48788f5 100644 --- a/bot.py +++ b/bot.py @@ -2,9 +2,11 @@ # -- coding: utf-8 -- import func, telebot, os +from db import PATH TOKEN = os.environ["TOKEN"] bot = telebot.TeleBot(TOKEN) +folder_list = [] # Start @bot.message_handler(commands=['start']) @@ -90,12 +92,52 @@ def magnet(message): else: bot.reply_to(message,'Log in to use bot /login ') +# File +@bot.message_handler(func=lambda message: message.text == 'File') +def file(message): + id = message.from_user.id + if func.auth_check(id): + global type + type = 'file' + f = folder_menu() + if f == None: + bot.reply_to(message,'No folders, use /add ') + else: + bot.reply_to(message,'Choose dir:',reply_markup=f) + else: + bot.reply_to(message,'Log in to use bot /login ') + +# File download +@bot.message_handler(content_types=['document']) +def download(message): + id = message.from_user.id + if func.auth_check(id): + global type, dir, folder_list + if dir != None and type == 'file': + if message.document.file_name.lower().endswith('.torrent'): + file_info = bot.get_file(message.document.file_id) + file_path = file_info.file_path + file = bot.download_file(file_path) + file_name = os.path.join(PATH, message.document.file_name) + with open(file_name, 'wb') as dl: + dl.write(file) + f = func.file(id,file_name,dir) + dir, type, folder_list = None,None,[] + bot.reply_to(message,f) + else: + bot.reply_to(message,'Send .torrent file') + bot.reply_to(message,'Choose download type:',reply_markup=home()) + else: + bot.reply_to(message,'Log in to use bot /login ') + # Dir choose def dirchoose(message): global dir dir = message.text if type == 'magnet': bot.reply_to(message,'Send magnet link') + if type == 'file': + bot.reply_to(message,'Send .torrent file') # Unknown message @bot.message_handler(func=lambda message: True) diff --git a/func.py b/func.py index 94eb061..2a0abd9 100644 --- a/func.py +++ b/func.py @@ -1,7 +1,7 @@ #!/usr/bin/python3 # -- coding: utf-8 -- -import db, os, subprocess +import db, os from db import * def u_auth(id,passwd): @@ -67,9 +67,14 @@ def magnet(id,link,dir): def file(id,file,dir): if auth_check(id): - subprocess.run(f"qbt torrent add file {PATH}{file} -f {dir}", check=True, shell=True) - os.remove(f'{PATH}{file}') + dict = db.read(DIR_FILE) + path = dict[dir] + command = f'''qbt torrent add file "{file}" -f {path}''' + os.system(f"bash -c '{command}'") + os.remove(file) return 'Success' + else: + return 'Log in first' def dirlist(): dirs = {}