Skip to content

Commit

Permalink
Enable qbittorrent settings
Browse files Browse the repository at this point in the history
Signed-off-by: anasty17 <[email protected]>
  • Loading branch information
anasty17 committed Oct 29, 2022
1 parent d6fc38b commit 3c4a243
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ In each single file there is a major change from base code, it's almost totaly d
- Qbittorrent support
- Select files from Torrent before and while downloading
- Seed torrents to specific ratio and time
- Edit Global Options while bot running from bot settings
### Aria2c
- Select files from Torrent before and while downloading
- Seed torrents to specific ratio and time
Expand Down
10 changes: 10 additions & 0 deletions bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
GLOBAL_EXTENSION_FILTER = ['.aria2']
user_data = {}
aria2_options = {}
qbit_options = {}

try:
if bool(environ.get('_____REMOVE_THIS_LINE_____')):
Expand Down Expand Up @@ -85,6 +86,9 @@
if a2c_options := db.settings.aria2c.find_one({'_id': bot_id}):
del a2c_options['_id']
aria2_options = a2c_options
if qbit_opt := db.settings.qbittorrent.find_one({'_id': bot_id}):
del qbit_opt['_id']
qbit_options = qbit_opt
conn.close()
else:
config_dict = {}
Expand Down Expand Up @@ -384,6 +388,12 @@ def aria2c_init():
del aria2_options['max-download-limit']
del aria2_options['lowest-speed-limit']

qb_client = get_client()
if not qbit_options:
qbit_options = dict(qb_client.app_preferences())
else:
qb_client.app_set_preferences(qbit_options)

updater = tgUpdater(token=BOT_TOKEN, request_kwargs={'read_timeout': 20, 'connect_timeout': 15})
bot = updater.bot
dispatcher = updater.dispatcher
Expand Down
11 changes: 10 additions & 1 deletion bot/helper/ext_utils/db_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from pymongo import MongoClient
from pymongo.errors import PyMongoError

from bot import DB_URI, user_data, rss_dict, LOGGER, bot_id, config_dict, aria2_options
from bot import DB_URI, user_data, rss_dict, LOGGER, bot_id, config_dict, aria2_options, qbit_options

class DbManger:
def __init__(self):
Expand All @@ -28,6 +28,9 @@ def db_load(self):
# Save Aria2c options
if self.__db.settings.aria2c.find_one({'_id': bot_id}) is None:
self.__db.settings.aria2c.update_one({'_id': bot_id}, {'$set': aria2_options}, upsert=True)
# Save qbittorrent options
if self.__db.settings.qbittorrent.find_one({'_id': bot_id}) is None:
self.__db.settings.qbittorrent.update_one({'_id': bot_id}, {'$set': qbit_options}, upsert=True)
# User Data
if self.__db.users.find_one():
rows = self.__db.users.find({}) # return a dict ==> {_id, is_sudo, is_auth, as_doc, thumb}
Expand Down Expand Up @@ -65,6 +68,12 @@ def update_aria2(self, key, value):
self.__db.settings.aria2c.update_one({'_id': bot_id}, {'$set': {key: value}}, upsert=True)
self.__conn.close()

def update_qbittorrent(self, key, value):
if self.__err:
return
self.__db.settings.qbittorrent.update_one({'_id': bot_id}, {'$set': {key: value}}, upsert=True)
self.__conn.close()

def update_private_file(self, path):
if self.__err:
return
Expand Down
57 changes: 46 additions & 11 deletions bot/modules/bot_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
from os import remove, rename, path as ospath, environ
from subprocess import run as srun, Popen
from dotenv import load_dotenv
from ast import literal_eval

from bot import config_dict, dispatcher, user_data, DB_URI, MAX_SPLIT_SIZE, DRIVES_IDS, DRIVES_NAMES, INDEX_URLS, aria2, GLOBAL_EXTENSION_FILTER, status_reply_dict_lock, Interval, aria2_options, aria2c_global, IS_PREMIUM_USER, download_dict
from bot import config_dict, dispatcher, user_data, DB_URI, MAX_SPLIT_SIZE, DRIVES_IDS, DRIVES_NAMES, INDEX_URLS, aria2, GLOBAL_EXTENSION_FILTER, status_reply_dict_lock, Interval, aria2_options, aria2c_global, IS_PREMIUM_USER, download_dict, qbit_options, get_client
from bot.helper.telegram_helper.message_utils import sendFile, sendMarkup, editMessage, update_all_messages
from bot.helper.telegram_helper.filters import CustomFilters
from bot.helper.telegram_helper.bot_commands import BotCommands
Expand Down Expand Up @@ -286,7 +287,17 @@ def get_buttons(key=None, edit_type=None):
buttons.sbutton(int(x/10), f"botset start aria {x}", position='footer')
msg = f'Aria2c Options. Page: {int(START/10)}. State: {STATE}'
elif key == 'qbit':
pass
for k in list(qbit_options.keys())[START:10+START]:
buttons.sbutton(k, f"botset editqbit {k}")
if STATE == 'view':
buttons.sbutton('Edit', "botset edit qbit")
else:
buttons.sbutton('View', "botset view qbit")
buttons.sbutton('Back', "botset back")
buttons.sbutton('Close', "botset close")
for x in range(0, len(qbit_options)-1, 10):
buttons.sbutton(int(x/10), f"botset start qbit {x}", position='footer')
msg = f'Qbittorrent Options. Page: {int(START/10)}. State: {STATE}'
elif edit_type == 'editvar':
buttons.sbutton('Back', "botset back var")
if key not in ['TELEGRAM_HASH', 'TELEGRAM_API']:
Expand Down Expand Up @@ -380,6 +391,25 @@ def edit_aria(update, context, omsg, key):
if DB_URI:
DbManger().update_aria2(key, value)

def edit_qbit(update, context, omsg, key):
handler_dict[omsg.chat.id] = False
value = update.message.text
if value.lower() == 'true':
value = True
elif value.lower() == 'false':
value = False
elif key == 'max_ratio':
value = float(value)
elif value.isdigit():
value = int(value)
client = get_client()
client.app_set_preferences({key: value})
qbit_options[key] = value
update_buttons(omsg, 'qbit')
update.message.delete()
if DB_URI:
DbManger().update_qbittorrent(key, value)

def upload_file(update, context, omsg):
handler_dict[omsg.chat.id] = False
doc = update.message.document
Expand Down Expand Up @@ -451,9 +481,9 @@ def edit_bot_settings(update, context):
handler_dict[message.chat.id] = False
key = data[2] if len(data) == 3 else None
update_buttons(message, key)
elif data[1] == 'var':
elif data[1] in ['var', 'aria', 'qbit']:
query.answer()
update_buttons(message, 'var')
update_buttons(message, data[1])
elif data[1] == 'resetvar':
query.answer()
value = ''
Expand Down Expand Up @@ -556,7 +586,7 @@ def edit_bot_settings(update, context):
dispatcher.remove_handler(value_handler)
elif data[1] == 'editaria' and STATE == 'view':
value = aria2_options[data[2]]
if len(str(value)) > 200:
if len(value) > 200:
query.answer()
filename = f"{data[2]}.txt"
with open(filename, 'w', encoding='utf-8') as f:
Expand Down Expand Up @@ -584,7 +614,17 @@ def edit_bot_settings(update, context):
update_buttons(message, 'var')
dispatcher.remove_handler(value_handler)
elif data[1] == 'editqbit' and STATE == 'view':
pass
value = qbit_options[data[2]]
if len(str(value)) > 200:
query.answer()
filename = f"{data[2]}.txt"
with open(filename, 'w', encoding='utf-8') as f:
f.write(f'{value}')
sendFile(context.bot, message, filename)
return
elif value == '':
value = None
query.answer(text=f'{value}', show_alert=True)
elif data[1] == 'edit':
query.answer()
globals()['STATE'] = 'edit'
Expand All @@ -605,11 +645,6 @@ def edit_bot_settings(update, context):
&& git push origin {config_dict['UPSTREAM_BRANCH']} -q"], shell=True)
query.message.delete()
query.message.reply_to_message.delete()
elif data[1] == 'aria':
query.answer()
update_buttons(message, data[1])
elif data[1] == 'qbit':
query.answer(text='Soon!', show_alert=True)


def bot_settings(update, context):
Expand Down

0 comments on commit 3c4a243

Please sign in to comment.