def cmd_search_library(bot, user, text, command, parameter): global song_shortlist if not parameter: bot.send_msg(constants.strings('bad_parameter', command=command), text) return msgs = [constants.strings('multiple_file_found') + "<ul>"] count = 0 _keywords = parameter.split(" ") keywords = [] for kw in _keywords: if kw: keywords.append(kw) music_dicts = var.music_db.query_music_by_keywords(keywords) if music_dicts: items = dicts_to_items(bot, music_dicts) song_shortlist = music_dicts if len(items) == 1: music_wrapper = get_cached_wrapper(items[0], user) var.playlist.append(music_wrapper) log.info("cmd: add to playlist: " + music_wrapper.format_debug_string()) bot.send_msg( constants.strings('file_added', item=music_wrapper.format_song_string())) else: for item in items: count += 1 if count > ITEMS_PER_PAGE: break if len(item.tags) > 0: msgs.append( "<li><b>{:d}</b> - [{}] <b>{}</b> (<i>{}</i>)</li>". format(count, item.display_type(), item.title, ", ".join(item.tags))) else: msgs.append( "<li><b>{:d}</b> - [{}] <b>{}</b> </li>".format( count, item.display_type(), item.title, ", ".join(item.tags))) if count != 0: msgs.append("</ul>") if count > ITEMS_PER_PAGE: msgs.append(constants.strings("records_omitted")) msgs.append(constants.strings("shortlist_instruction")) send_multi_lines(bot, msgs, text, "") else: bot.send_msg(constants.strings("no_file"), text) else: bot.send_msg(constants.strings("no_file"), text)
def cmd_play_file_match(bot, user, text, command, parameter, do_not_refresh_cache=False): global log if parameter: file_dicts = var.music_db.query_music(Condition().and_equal( 'type', 'file')) msgs = [tr('multiple_file_added') + "<ul>"] try: count = 0 music_wrappers = [] for file_dict in file_dicts: file = file_dict['title'] match = re.search(parameter, file) if match and match[0]: count += 1 music_wrapper = get_cached_wrapper(dict_to_item(file_dict), user) music_wrappers.append(music_wrapper) log.info("cmd: add to playlist: " + music_wrapper.format_debug_string()) msgs.append("<li><b>{}</b> ({})</li>".format( music_wrapper.item().title, file[:match.span()[0]] + "<b style='color:pink'>" + file[match.span()[0]:match.span()[1]] + "</b>" + file[match.span()[1]:])) if count != 0: msgs.append("</ul>") var.playlist.extend(music_wrappers) send_multi_lines_in_channel(bot, msgs, "") else: if do_not_refresh_cache: bot.send_msg(tr("no_file"), text) else: var.cache.build_dir_cache() cmd_play_file_match(bot, user, text, command, parameter, do_not_refresh_cache=True) except re.error as e: msg = tr('wrong_pattern', error=str(e)) bot.send_msg(msg, text) else: bot.send_msg(tr('bad_parameter', command=command), text)
def library(): global log ITEM_PER_PAGE = 10 if request.form: log.debug("web: Post request from %s: %s" % (request.remote_addr, str(request.form))) if request.form['action'] in ['add', 'query', 'delete']: condition = build_library_query_condition(request.form) total_count = 0 try: total_count = var.music_db.query_music_count(condition) except sqlite3.OperationalError: pass if not total_count: return ('', 204) if request.form['action'] == 'add': items = dicts_to_items(var.music_db.query_music(condition)) music_wrappers = [] for item in items: music_wrapper = get_cached_wrapper(item, user) music_wrappers.append(music_wrapper) log.info("cmd: add to playlist: " + music_wrapper.format_debug_string()) var.playlist.extend(music_wrappers) return redirect("./", code=302) elif request.form['action'] == 'delete': items = dicts_to_items(var.music_db.query_music(condition)) for item in items: var.playlist.remove_by_id(item.id) item = var.cache.get_item_by_id(item.id) if os.path.isfile(item.uri()): log.info("web: delete file " + item.uri()) os.remove(item.uri()) var.cache.free_and_delete(item.id) if len(os.listdir(var.music_folder + request.form['dir'])) == 0: os.rmdir(var.music_folder + request.form['dir']) time.sleep(0.1) return redirect("./", code=302) else: page_count = math.ceil(total_count / ITEM_PER_PAGE) current_page = int( request.form['page']) if 'page' in request.form else 1 if current_page <= page_count: condition.offset((current_page - 1) * ITEM_PER_PAGE) else: current_page = 1 condition.limit(ITEM_PER_PAGE) items = dicts_to_items(var.music_db.query_music(condition)) results = [] for item in items: result = {} result['id'] = item.id result['title'] = item.title result['type'] = item.display_type() result['tags'] = [(tag, tag_color(tag)) for tag in item.tags] if item.type != 'radio' and item.thumbnail: result[ 'thumb'] = f"data:image/PNG;base64,{item.thumbnail}" else: result['thumb'] = "static/image/unknown-album.png" if item.type == 'file': result['path'] = item.path result['artist'] = item.artist else: result['path'] = item.url result['artist'] = "??" results.append(result) return jsonify({ 'items': results, 'total_pages': page_count, 'active_page': current_page }) elif request.form['action'] == 'edit_tags': tags = list(dict.fromkeys( request.form['tags'].split(","))) # remove duplicated items if request.form['id'] in var.cache: music_wrapper = get_cached_wrapper_by_id( request.form['id'], user) music_wrapper.clear_tags() music_wrapper.add_tags(tags) var.playlist.version += 1 else: item = var.music_db.query_music_by_id(request.form['id']) item['tags'] = tags var.music_db.insert_music(item) return redirect("./", code=302) else: abort(400)