def no_notification_cb(update: Update, context: CallbackContext): logger.info('no notification inline button') if not config.notifications.no_notification_tag: update.callback_query.answer("tag not set in the config file", cache_time=10) return torrent_hash = context.match[1] logger.info('torrent hash: %s', torrent_hash) torrent = qb.torrent(torrent_hash, get_torrent_generic_properties=False) torrent_tags = torrent.tags_list(lower=True) no_notification_tag = config.notifications.no_notification_tag is_torrent_tagged = no_notification_tag.lower() in torrent_tags if is_torrent_tagged: torrent.remove_tags(no_notification_tag) callback_answer = f'Tag "{no_notification_tag}" removed -> 🔔' else: torrent.add_tags(no_notification_tag) callback_answer = f'Tag "{no_notification_tag}" added -> 🔕' update.callback_query.edit_message_reply_markup( kb.short_markup(torrent_hash, not is_torrent_tagged)) update.callback_query.answer(callback_answer)
def add_from_magnet(update: Update, context: CallbackContext): logger.info('magnet url from %s', update.effective_user.first_name) magnet_link = update.message.text qb.download_from_link(magnet_link) # always returns an empty json: # https://python-qbittorrent.readthedocs.io/en/latest/modules/api.html#qbittorrent.client.Client.download_from_link torrent_hash = u.hash_from_magnet(magnet_link) logger.info('torrent hash from regex: %s', torrent_hash) torrent_keyboard_markup = kb.short_markup(torrent_hash) update.message.reply_html('Magnet added', reply_markup=torrent_keyboard_markup, quote=True) target_chat_id = notify_addition(update.effective_chat.id) if not target_chat_id: return text = "User {} [{}] added a magnet link, hash: <code>{}</code>".format( update.effective_user.full_name, update.effective_user.id, torrent_hash) context.bot.send_message(target_chat_id, text, reply_markup=torrent_keyboard_markup, parse_mode=ParseMode.HTML, disable_web_page_preview=True)
def add_from_magnet(_, update): logger.info('magnet url from %s', update.effective_user.first_name) magnet_link = update.message.text qb.download_from_link(magnet_link) # always returns an empty json: # https://python-qbittorrent.readthedocs.io/en/latest/modules/api.html#qbittorrent.client.Client.download_from_link torrent_hash = re.search(r'magnet:\?xt=urn:btih:([a-z0-9]+)(?:&.*)?', magnet_link, re.I).group(1) logger.info('torrent hash from regex: %s', torrent_hash) update.message.reply_html('Magnet added', reply_markup=kb.short_markup(torrent_hash), quote=True)
def add_from_file(update: Update, context: CallbackContext): logger.info('application/x-bittorrent document from %s', update.effective_user.first_name) document = update.message.document if document.mime_type != "application/x-bittorrent" and not document.file_name.lower( ).endswith(".torrent"): logger.info('invalid document from %s (mime type: %s; file name: %s)', update.effective_user.full_name, document.mime_type, document.file_name) update.message.reply_markdown( 'Please send me a valid torrent file (`.torrent` extension or `application/x-bittorrent` mime type)', quote=True) return file_id = document.file_id torrent_file = context.bot.get_file(file_id) file_path = './downloads/{}'.format(document.file_name) torrent_file.download(file_path) kwargs = get_qbt_request_kwargs() with open(file_path, 'rb') as f: # https://stackoverflow.com/a/46270711 decoded_dict = bencoding.bdecode(f.read()) torrent_hash = hashlib.sha1(bencoding.bencode( decoded_dict[b"info"])).hexdigest() f.seek(0) # 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, **kwargs) update.message.reply_text('Torrent added', quote=True, reply_markup=kb.short_markup(torrent_hash)) os.remove(file_path) notify_addition(update.effective_chat.id, context.bot, update.effective_user, document.file_name or "[unknown file name]")
def add_from_magnet(update: Update, context: CallbackContext): logger.info('magnet url from %s', update.effective_user.first_name) magnet_link = update.message.text kwargs = get_qbt_request_kwargs() qb.download_from_link(magnet_link, **kwargs) # always returns an empty json: # https://python-qbittorrent.readthedocs.io/en/latest/modules/api.html#qbittorrent.client.Client.download_from_link torrent_hash = u.hash_from_magnet(magnet_link) logger.info('torrent hash from regex: %s', torrent_hash) update.message.reply_html('Magnet added', reply_markup=kb.short_markup(torrent_hash), quote=True) notify_addition(update.effective_chat.id, context.bot, update.effective_user, torrent_hash)
def short_markup(self, *args, **kwargs): return kb.short_markup(self.hash, *args, **kwargs)