Пример #1
0
    def refresh(self):
        dicts = var.music_db.query_random_music(var.config.getint("bot", "autoplay_length", fallback=5),
                                                Condition().and_not_sub_condition(
                                                    Condition().and_like('tags', "%don't autoplay,%")))

        if dicts:
            _list = [get_cached_wrapper_from_dict(var.bot, _dict, "AutoPlay") for _dict in dicts]
            self.from_list(_list, -1)
Пример #2
0
def cmd_play_file(bot, user, text, command, parameter, do_not_refresh_cache=False):
    global log, song_shortlist

    # assume parameter is a path
    music_wrappers = get_cached_wrappers_from_dicts(var.music_db.query_music(Condition().and_equal('path', parameter)), user)
    if music_wrappers:
        var.playlist.append(music_wrappers[0])
        log.info("cmd: add to playlist: " + music_wrappers[0].format_debug_string())
        send_item_added_message(bot, music_wrappers[0], len(var.playlist) - 1, text)
        return

    # assume parameter is a folder
    music_wrappers = get_cached_wrappers_from_dicts(var.music_db.query_music(Condition()
                                                                             .and_equal('type', 'file')
                                                                             .and_like('path', parameter + '%')), user)
    if music_wrappers:
        msgs = [tr('multiple_file_added')]

        for music_wrapper in music_wrappers:
            log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
            msgs.append("<b>{:s}</b> ({:s})".format(music_wrapper.item().title, music_wrapper.item().path))

        var.playlist.extend(music_wrappers)

        send_multi_lines_in_channel(bot, msgs)
        return

    # try to do a partial match
    matches = var.music_db.query_music(Condition()
                                       .and_equal('type', 'file')
                                       .and_like('path', '%' + parameter + '%', case_sensitive=False))
    if len(matches) == 1:
        music_wrapper = get_cached_wrapper_from_dict(matches[0], user)
        var.playlist.append(music_wrapper)
        log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
        send_item_added_message(bot, music_wrapper, len(var.playlist) - 1, text)
        return
    elif len(matches) > 1:
        song_shortlist = matches
        msgs = [tr('multiple_matches')]
        for index, match in enumerate(matches):
            msgs.append("<b>{:d}</b> - <b>{:s}</b> ({:s})".format(
                index + 1, match['title'], match['path']))
        msgs.append(tr("shortlist_instruction"))
        send_multi_lines(bot, msgs, text)
        return

    if do_not_refresh_cache:
        bot.send_msg(tr("no_file"), text)
    else:
        var.cache.build_dir_cache()
        cmd_play_file(bot, user, text, command, parameter, do_not_refresh_cache=True)
Пример #3
0
def cmd_play_file(bot,
                  user,
                  text,
                  command,
                  parameter,
                  do_not_refresh_cache=False):
    global log, song_shortlist

    # if parameter is {index}
    if parameter.isdigit():
        music_wrappers = get_cached_wrappers_from_dicts(
            bot,
            var.music_db.query_music(Condition().and_equal(
                'type',
                'file').order_by('path').limit(1).offset(int(parameter))),
            user)

        if music_wrappers:
            var.playlist.append(music_wrappers[0])
            log.info("cmd: add to playlist: " +
                     music_wrappers[0].format_debug_string())
            bot.send_msg(
                constants.strings('file_added',
                                  item=music_wrappers[0].format_song_string()))
            return

    # assume parameter is a path
    music_wrappers = get_cached_wrappers_from_dicts(
        bot, var.music_db.query_music(Condition().and_equal('path',
                                                            parameter)), user)
    if music_wrappers:
        var.playlist.append(music_wrappers[0])
        log.info("cmd: add to playlist: " +
                 music_wrappers[0].format_debug_string())
        bot.send_msg(
            constants.strings('file_added',
                              item=music_wrappers[0].format_song_string()))
        return

    # assume parameter is a folder
    music_wrappers = get_cached_wrappers_from_dicts(
        bot,
        var.music_db.query_music(Condition().and_equal(
            'type', 'file').and_like('path', parameter + '%')), user)
    if music_wrappers:
        msgs = [constants.strings('multiple_file_added')]

        for music_wrapper in music_wrappers:
            var.playlist.append(music_wrapper)
            log.info("cmd: add to playlist: " +
                     music_wrapper.format_debug_string())
            msgs.append("{} ({})".format(music_wrapper.item().title,
                                         music_wrapper.item().path))

        send_multi_lines(bot, msgs, None)
        return

    # try to do a partial match
    matches = var.music_db.query_music(Condition().and_equal(
        'type', 'file').and_like('path',
                                 '%' + parameter + '%',
                                 case_sensitive=False))
    if len(matches) == 1:
        music_wrapper = get_cached_wrapper_from_dict(bot, matches[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()))
        return
    elif len(matches) > 1:
        song_shortlist = matches
        msgs = [constants.strings('multiple_matches')]
        for index, match in enumerate(matches):
            msgs.append("<b>{:d}</b> - <b>{:s}</b> ({:s})".format(
                index + 1, match['title'], match['path']))
        msgs.append(constants.strings("shortlist_instruction"))
        send_multi_lines(bot, msgs, text)
        return

    if do_not_refresh_cache:
        bot.send_msg(constants.strings("no_file"), text)
    else:
        var.cache.build_dir_cache(bot)
        cmd_play_file(bot,
                      user,
                      text,
                      command,
                      parameter,
                      do_not_refresh_cache=True)