def get_quick_info_text(): active_trnts = qb.torrents(filter='active', sort='dlspeed', reverse=False) completed_trnts = qb.torrents(filter='completed') if active_trnts: active_torrents_strings_list = [ TORRENT_STRING_COMPACT.format(**t.dict()) for t in active_trnts ] else: active_torrents_strings_list = ['no active torrent'] if completed_trnts: completed_torrents_strings_list = [ '• {}'.format(t.short_name) for t in completed_trnts ] else: completed_torrents_strings_list = ['no completed torrent'] # shorten the message if it's too long to send completed_torrents_string_len = sum( map(len, completed_torrents_strings_list)) active_torrents_string_len = sum(map(len, active_torrents_strings_list)) if (completed_torrents_string_len + active_torrents_string_len) > MAX_MESSAGE_LENGTH: # we assume the longest one between the two is the completed torrents list completed_torrents_strings_list = [ 'list too long, use /completed to see completed torrents' ] schedule_info = qb.get_schedule() if not schedule_info: schedule_string = '<b>Schedule</b>: off' else: schedule_string = '<b>Schedule</b>: on, from {from_hour} to {to_hour} ({days})'.format( **schedule_info) alt_speed_info = qb.get_alt_speed(human_readable=True) alt_speed_string = '<b>Alt speed is {status}</b> (down: {alt_dl_limit}/s, up: {alt_up_limit}/s)'.format( **alt_speed_info) current_speed = qb.get_speed() current_speed_string = '<b>Current speed</b>: down: {0}/s, up: {1}/s'.format( *current_speed) text = QUICK_INFO_TEXT.format( completed='\n'.join(completed_torrents_strings_list), active='\n'.join(active_torrents_strings_list), schedule=schedule_string, alt_speed=alt_speed_string, current_speed=current_speed_string, last_refresh=datetime.datetime.now().strftime('%d/%m/%Y %H:%M:%S')) return text
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)
def on_torrents_list_selection(_, update, groups): logger.info('torrents list menu button from %s: %s', update.message.from_user.first_name, groups[0]) qbfilter = groups[0] if qbfilter.startswith('/'): # remove the "/" if the category has been used as command qbfilter = qbfilter.replace('/', '') logger.info('torrents status: %s', qbfilter) torrents = qb.torrents(filter=qbfilter, sort='dlspeed', reverse=False) or [] if qbfilter == 'tostart': all_torrents = qb.torrents(filter='all') completed_torrents = [t.hash for t in qb.torrents(filter='completed')] active_torrents = [t.hash for t in qb.torrents(filter='active')] torrents = [t for t in all_torrents if t.hash not in completed_torrents and t.hash not in active_torrents] logger.info('qbittirrent request returned %d torrents', len(torrents)) if not torrents: update.message.reply_html('There is no torrent to be listed for <i>{}</i>'.format(qbfilter)) return if qbfilter == 'completed': base_string = TORRENT_STRING_COMPLETED # use a shorter string with less info for completed torrents else: base_string = TORRENT_STRING_COMPACT markup = None if qbfilter == 'active': markup = kb.REFRESH_ACTIVE strings_list = [base_string.format(**torrent.dict()) 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, reply_markup=markup)
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')
def get_quick_info_text(): active_torrents = qb.torrents(filter='active', sort='dlspeed', reverse=False) completed_torrents = qb.torrents(filter='completed') if not active_torrents: active_torrents_strings_list = ['no active torrent'] else: active_torrents_with_traffic = list() active_torrents_without_traffic_count = 0 for active_torrent in active_torrents: if active_torrent.generic_speed > 0 or active_torrent.state not in ( 'forcedDL', 'forcedUP'): # add to this list only torrents that are generating traffic OR active torrents # that have not been force-started (eg: "fetching metadata") active_torrents_with_traffic.append(active_torrent) else: active_torrents_without_traffic_count += 1 active_torrents_strings_list = [ TORRENT_STRING_COMPACT.format(**t.dict()) for t in active_torrents_with_traffic ] if active_torrents_without_traffic_count > 0: no_traffic_row = '• other <b>{}</b> active torrents stalled'.format( active_torrents_without_traffic_count) active_torrents_strings_list.append(no_traffic_row) if completed_torrents: completed_torrents_strings_list = [ '• {}'.format(t.short_name) for t in completed_torrents ] else: completed_torrents_strings_list = ['no completed torrent'] # shorten the message if it's too long to send completed_torrents_string_len = sum( map(len, completed_torrents_strings_list)) active_torrents_string_len = sum(map(len, active_torrents_strings_list)) if (completed_torrents_string_len + active_torrents_string_len) > MAX_MESSAGE_LENGTH: # we assume the longest one between the two is the completed torrents list completed_torrents_strings_list = [ 'list too long, use /completed to see completed torrents' ] schedule_info = qb.get_schedule() if not schedule_info: schedule_string = '<b>Schedule</b>: off' else: schedule_string = '<b>Schedule</b>: on, from {from_hour} to {to_hour} ({days})'.format( **schedule_info) alt_speed_info = qb.get_alt_speed(human_readable=True) alt_speed_string = '<b>Alt speed is {status}</b> (down: {alt_dl_limit}/s, up: {alt_up_limit}/s)'.format( **alt_speed_info) current_speed = qb.get_speed() current_speed_string = '<b>Current speed</b>: down: {0}/s, up: {1}/s'.format( *current_speed) text = QUICK_INFO_TEXT.format( completed='\n'.join(completed_torrents_strings_list), active='\n'.join(active_torrents_strings_list), schedule=schedule_string, alt_speed=alt_speed_string, current_speed=current_speed_string, last_refresh=datetime.datetime.now().strftime('%d/%m/%Y %H:%M:%S')) return text