Add file function

This commit is contained in:
trueold89 2023-10-31 02:37:37 +03:00
parent 500520455b
commit 5a4fb2279d
Signed by: trueold89
GPG Key ID: C122E85DD49E6B30
2 changed files with 50 additions and 3 deletions

42
bot.py
View File

@ -2,9 +2,11 @@
# -- coding: utf-8 -- # -- coding: utf-8 --
import func, telebot, os import func, telebot, os
from db import PATH
TOKEN = os.environ["TOKEN"] TOKEN = os.environ["TOKEN"]
bot = telebot.TeleBot(TOKEN) bot = telebot.TeleBot(TOKEN)
folder_list = []
# Start # Start
@bot.message_handler(commands=['start']) @bot.message_handler(commands=['start'])
@ -90,12 +92,52 @@ def magnet(message):
else: else:
bot.reply_to(message,'Log in to use bot /login <passwd>') bot.reply_to(message,'Log in to use bot /login <passwd>')
# 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 <folder_name> <path>')
else:
bot.reply_to(message,'Choose dir:',reply_markup=f)
else:
bot.reply_to(message,'Log in to use bot /login <passwd>')
# 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 <passwd>')
# Dir choose # Dir choose
def dirchoose(message): def dirchoose(message):
global dir global dir
dir = message.text dir = message.text
if type == 'magnet': if type == 'magnet':
bot.reply_to(message,'Send magnet link') bot.reply_to(message,'Send magnet link')
if type == 'file':
bot.reply_to(message,'Send .torrent file')
# Unknown message # Unknown message
@bot.message_handler(func=lambda message: True) @bot.message_handler(func=lambda message: True)

11
func.py
View File

@ -1,7 +1,7 @@
#!/usr/bin/python3 #!/usr/bin/python3
# -- coding: utf-8 -- # -- coding: utf-8 --
import db, os, subprocess import db, os
from db import * from db import *
def u_auth(id,passwd): def u_auth(id,passwd):
@ -67,9 +67,14 @@ def magnet(id,link,dir):
def file(id,file,dir): def file(id,file,dir):
if auth_check(id): if auth_check(id):
subprocess.run(f"qbt torrent add file {PATH}{file} -f {dir}", check=True, shell=True) dict = db.read(DIR_FILE)
os.remove(f'{PATH}{file}') path = dict[dir]
command = f'''qbt torrent add file "{file}" -f {path}'''
os.system(f"bash -c '{command}'")
os.remove(file)
return 'Success' return 'Success'
else:
return 'Log in first'
def dirlist(): def dirlist():
dirs = {} dirs = {}