reply_markup=kb.QUICK_MENU_BUTTON) update.callback_query.answer('Scheduled altenrative speed on') @u.check_permissions(required_permission=Permissions.EDIT) @u.failwithmessage @u.ignore_not_modified_exception def on_schedoff_button_quick(_, update): logger.info('quick info: schedoff button') qb.set_preferences(**{'scheduler_enabled': False}) text = get_quick_info_text() update.callback_query.edit_message_text(text, parse_mode=ParseMode.HTML, reply_markup=kb.QUICK_MENU_BUTTON) update.callback_query.answer('Scheduled altenrative speed off') updater.add_handler(CommandHandler(['quick'], on_quick_info_command)) updater.add_handler( CallbackQueryHandler(on_refresh_button_quick, pattern=r'^quick:refresh$')) updater.add_handler( CallbackQueryHandler(on_alton_button_quick, pattern=r'^quick:alton$')) updater.add_handler( CallbackQueryHandler(on_altoff_button_quick, pattern=r'^quick:altoff$')) updater.add_handler( CallbackQueryHandler(on_schedon_button_quick, pattern=r'^quick:schedon')) updater.add_handler( CallbackQueryHandler(on_schedoff_button_quick, pattern=r'^quick:schedoff'))
import logging # noinspection PyPackageRequirements from telegram.ext import CommandHandler from bot import updater from utils import u from utils import kb logger = logging.getLogger(__name__) @u.failwithmessage def remove_keyboard(_, update): logger.info('/rmkb from %s', update.effective_user.first_name) update.message.reply_text('Keyboard removed', reply_markup=kb.REMOVE) updater.add_handler(CommandHandler(['rmkb'], remove_keyboard))
preference_key = 'alt_up_limit' kbs: str = args[0] if not kbs.isdigit(): update.message.reply_text('Please pass the alternative speed limit in kb/s, as an integer') return preferences_to_edit[preference_key] = int(kbs) * 1014 qb.set_preferences(**preferences_to_edit) update.message.reply_markdown('`{}` set to {} kb/s'.format(preference_key, kbs)) @u.check_permissions(required_permission=Permissions.READ) @u.failwithmessage def altdown_speed_callback(_, update, groups): logger.info('remove buttons inline button') speed_kbs = int(groups[0]) * 1024 preferences_to_edit = dict() preference_key = 'alt_dl_limit' preferences_to_edit[preference_key] = speed_kbs qb.set_preferences(**preferences_to_edit) update.callback_query.answer('Alternative dl speed set to {} kb/s'.format(speed_kbs)) updater.add_handler(CommandHandler(['altdown', 'altup'], change_alternative_limits, pass_args=True)) updater.add_handler(CallbackQueryHandler(altdown_speed_callback, pattern=r'^altdown:(\d+)$', pass_groups=True))
@u.failwithmessage def on_settings_command(_, update): logger.info('/settings from %s', update.effective_user.first_name) preferences = qb.preferences() lines = sorted(['{}: <code>{}</code>'.format(k, v) for k, v in preferences.items()]) for strings_chunk in u.split_text(lines): update.message.reply_html('\n'.join(strings_chunk), disable_web_page_preview=True) @u.check_permissions(required_permission=Permissions.EDIT) @u.failwithmessage def change_setting(_, update, args): logger.info('/set from %s', update.effective_user.first_name) if len(args) < 2: update.message.reply_html('Usage: /set <code>[setting] [value]</code>') return key = args[0].lower() val = args[1] qb.set_preferences(**{key: val}) update.message.reply_html('<b>Setting changed</b>:\n\n<code>{}</code>'.format(val)) updater.add_handler(CommandHandler(['settings', 's'], on_settings_command)) updater.add_handler(CommandHandler(['set'], change_setting, pass_args=True))
logger = logging.getLogger(__name__) @u.check_permissions(required_permission=Permissions.READ) @u.failwithmessage def on_filter_command(_, update, args): logger.info('/filter command used by %s (query: %s)', update.effective_user.first_name, args) if not args[0:]: update.message.reply_text('Please provide a search term') return query = ' '.join(args[0:]) torrents = qb.filter(query) if not torrents: update.message.reply_text('No results for "{}"'.format(query)) return strings_list = [torrent.string() for torrent in torrents] for strings_chunk in u.split_text(strings_list): update.message.reply_html('\n'.join(strings_chunk), disable_web_page_preview=True) updater.add_handler( CommandHandler(['filter', 'f'], on_filter_command, pass_args=True))
• magnet url: add a torrent by magnet url <i>EDIT commands</i> • /altdown: change the alternative max download speed from a keyboard • /altdown <code>[kb/s]</code>: change the alternative max download speed • /altup <code>[kb/s]</code>: change the alternative max upload speed • /pauseall: pause all torrents • /resumeall: resume all torrents • /set <code>[setting] [new value]</code>: change a setting <i>ADMIN commands</i> • /getlog or /log: get the log file • /permissions: get the current permissions configuration • /pset <code>[key] [val]</code>: change the value of a permission key • /config: get the qbittorrent's section of the config file <i>FREE commands</i> • /rmkb: remove the keyboard, if any""" @u.check_permissions(required_permission=Permissions.READ) @u.failwithmessage def on_help(_, update): logger.info('/help from %s', update.message.from_user.first_name) update.message.reply_html(HELP_MESSAGE) updater.add_handler(CommandHandler('help', on_help)) updater.add_handler(RegexHandler(r'^\/start$', on_help))
@u.check_permissions(required_permission=Permissions.WRITE) @u.failwithmessage def add_from_file(bot, update): logger.info('document from %s', update.effective_user.first_name) if update.message.document.mime_type != 'application/x-bittorrent': update.message.reply_markdown('Please send me a `.torrent` file') return file_id = update.message.document.file_id torrent_file = bot.get_file(file_id) file_path = './downloads/{}'.format(update.message.document.file_name) torrent_file.download(file_path) with open(file_path, 'rb') as f: # this method always returns an empty json: # https://python-qbittorrent.readthedocs.io/en/latest/modules/api.html#qbittorrent.client.Client.download_from_file qb.download_from_file(f) os.remove(file_path) update.message.reply_text('Torrent added', quote=True) updater.add_handler( MessageHandler(Filters.text & Filters.regex(r'^magnet:\?.*'), add_from_magnet)) updater.add_handler(MessageHandler(Filters.document, add_from_file))
@u.failwithmessage def on_json_command(_, update): logger.info('/json command from %s', update.message.from_user.first_name) torrents = qb.torrents(filter='all') logger.info('qbittirrent request returned %d torrents', len(torrents)) if not torrents: update.message.reply_html('There is no torrent') return result_dict = defaultdict(list) for torrent in torrents: result_dict[torrent.state].append(torrent.dict()) file_path = os.path.join('downloads', '{}.json'.format(update.message.message_id)) with open(file_path, 'w+') as f: json.dump(result_dict, f, indent=4) update.message.reply_document(open(file_path, 'rb'), caption='#torrents_list', timeout=60 * 10) os.remove(file_path) updater.add_handler(CommandHandler('json', on_json_command))
from utils import u from utils import Permissions logger = logging.getLogger(__name__) @u.check_permissions(required_permission=Permissions.EDIT) @u.failwithmessage def on_resume_all_command(_, update): logger.info('resume all command from %s', update.message.from_user.first_name) qb.resume_all() update.message.reply_text('Resumed all torrents') @u.check_permissions(required_permission=Permissions.EDIT) @u.failwithmessage def on_pause_all_command(_, update): logger.info('pause all command from %s', update.message.from_user.first_name) qb.pause_all() update.message.reply_text('Paused all torrents') updater.add_handler(CommandHandler(['resumeall'], on_resume_all_command)) updater.add_handler(CommandHandler(['pauseall'], on_pause_all_command))
@u.check_permissions(required_permission=Permissions.READ) @u.failwithmessage def refresh_active_torrents(_, update): logger.info('refresh active torrents inline button used by %s', update.effective_user.first_name) torrents = qb.torrents(filter='active', sort='dlspeed', reverse=False) or [] if not torrents: update.callback_query.answer('Cannot refresh: no torrents') return strings_list = [TORRENT_STRING_COMPACT.format(**torrent.dict()) for torrent in torrents] # we assume the list doesn't require more than one message try: update.callback_query.edit_message_text( '\n'.join(strings_list), reply_markup=kb.REFRESH_ACTIVE, parse_mode=ParseMode.HTML ) except BadRequest as br: logger.error('Telegram error when refreshing the active torrents list: %s', br.message) update.callback_query.answer('Error: {}'.format(br.message)) return update.callback_query.answer('Refreshed') updater.add_handler(RegexHandler(TORRENT_CATEG_REGEX, on_torrents_list_selection, pass_groups=True)) updater.add_handler(CallbackQueryHandler(refresh_active_torrents, pattern=r'^refreshactive$'))
import logging from pprint import pformat # noinspection PyPackageRequirements from telegram.ext import CommandHandler from bot import updater from utils import u from utils import Permissions from config import config logger = logging.getLogger(__name__) @u.check_permissions(required_permission=Permissions.ADMIN) @u.failwithmessage def on_config_command(_, update): logger.info('/config from %s', update.effective_user.first_name) update.message.reply_html('<code>{}</code>'.format( pformat(config.qbittorrent))) updater.add_handler(CommandHandler('config', on_config_command))
update.message.reply_html('<code>{}</code>'.format(str(permissions))) @u.check_permissions(required_permission=Permissions.ADMIN) @u.failwithmessage def set_permission(_, update, args): logger.info('/pset from %s', update.effective_user.first_name) if len(args) < 2: update.message.reply_html('Usage: /pset <code>[permission key] [true/false/1/0]</code>') return key = args[0].lower() val = args[1].lower() if val.lower() not in ('true', 'false', '0', '1'): update.message.reply_html('Wrong value passed. Usage: /pset <code>[permission key] [true/false/1/0]</code>') return if permissions.get(key, None) is None: update.message.reply_text('Wrong key. Use /permissions to see the current permissions config') return actual_val = True if val in ('true', '1') else False permissions.set(key, actual_val) update.message.reply_html('<b>New config</b>:\n\n<code>{}</code>'.format(str(permissions))) updater.add_handler(CommandHandler(['permissions', 'p'], get_permissions)) updater.add_handler(CommandHandler(['pset'], set_permission, pass_args=True))
import logging # noinspection PyPackageRequirements from telegram.ext import CommandHandler from bot import updater from bot import qb from utils import u from utils import Permissions logger = logging.getLogger(__name__) @u.check_permissions(required_permission=Permissions.READ) @u.failwithmessage def on_version_command(_, update): logger.info('/version from %s', update.message.from_user.first_name) text = 'qBittorrent version: <code>{}</code>\nAPI version: <code>{}</code>'.format( qb.qbittorrent_version, qb.api_version) update.message.reply_html(text) updater.add_handler(CommandHandler('version', on_version_command))
def reduce_buttons(_, update, groups): logger.info('remove buttons inline button') torrent_hash = groups[0] logger.info('torrent hash: %s', torrent_hash) torrent = qb.torrent(torrent_hash) update.callback_query.edit_message_text( torrent.string(refresh_properties=True), reply_markup=torrent.short_markup(), parse_mode=ParseMode.HTML) update.callback_query.answer('Inline keyboard reduced') updater.add_handler( RegexHandler(r'^\/start info(.*)$', on_info_deeplink, pass_groups=True)) updater.add_handler( CallbackQueryHandler(manage_torrent_cb, pattern=r'^manage:(.*)$', pass_groups=True)) updater.add_handler( CallbackQueryHandler(see_trackers_cb, pattern=r'^trackers:(.*)$', pass_groups=True)) updater.add_handler( CallbackQueryHandler(refresh_torrent_cb, pattern=r'^refresh:(.*)$', pass_groups=True)) updater.add_handler( CallbackQueryHandler(pause_torrent_cb, pattern=r'^pause:(.*)$',
import logging # noinspection PyPackageRequirements from telegram.ext import CommandHandler from bot import updater from utils import u from utils import Permissions logger = logging.getLogger(__name__) @u.check_permissions(required_permission=Permissions.ADMIN) @u.failwithmessage def send_log_file(_, update): logger.info('/getlog from %s', update.effective_user.first_name) with open('logs/qbtbot.log', 'rb') as f: update.message.reply_document(f, timeout=600) updater.add_handler(CommandHandler(['getlog', 'log'], send_log_file))