Example #1
0
def cmd_shortlist(bot, user, text, command, parameter):
    global song_shortlist, log
    if parameter.strip() == "*":
        msgs = [tr('multiple_file_added') + "<ul>"]
        music_wrappers = []
        for kwargs in song_shortlist:
            kwargs['user'] = user
            music_wrapper = get_cached_wrapper_from_scrap(**kwargs)
            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().type, music_wrapper.item().title))

        var.playlist.extend(music_wrappers)

        msgs.append("</ul>")
        send_multi_lines_in_channel(bot, msgs, "")
        return

    try:
        indexes = [int(i) for i in parameter.split(" ")]
    except ValueError:
        bot.send_msg(tr('bad_parameter', command=command), text)
        return

    if len(indexes) > 1:
        msgs = [tr('multiple_file_added') + "<ul>"]
        music_wrappers = []
        for index in indexes:
            if 1 <= index <= len(song_shortlist):
                kwargs = song_shortlist[index - 1]
                kwargs['user'] = user
                music_wrapper = get_cached_wrapper_from_scrap(**kwargs)
                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().type, music_wrapper.item().title))
            else:
                var.playlist.extend(music_wrappers)
                bot.send_msg(tr('bad_parameter', command=command), text)
                return

        var.playlist.extend(music_wrappers)

        msgs.append("</ul>")
        send_multi_lines_in_channel(bot, msgs, "")
        return
    elif len(indexes) == 1:
        index = indexes[0]
        if 1 <= index <= len(song_shortlist):
            kwargs = song_shortlist[index - 1]
            kwargs['user'] = user
            music_wrapper = get_cached_wrapper_from_scrap(**kwargs)
            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

    bot.send_msg(tr('bad_parameter', command=command), text)
Example #2
0
def cmd_play_radio(bot, user, text, command, parameter):
    global log

    if not parameter:
        all_radio = var.config.items('radio')
        msg = tr('preconfigurated_radio')
        for i in all_radio:
            comment = ""
            if len(i[1].split(maxsplit=1)) == 2:
                comment = " - " + i[1].split(maxsplit=1)[1]
            msg += "<br />" + i[0] + comment
        bot.send_msg(msg, text)
    else:
        if var.config.has_option('radio', parameter):
            parameter = var.config.get('radio', parameter)
            parameter = parameter.split()[0]
        url = util.get_url_from_input(parameter)
        if url:
            music_wrapper = get_cached_wrapper_from_scrap(type='radio',
                                                          url=url,
                                                          user=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)
        else:
            bot.send_msg(tr('bad_url'), text)
Example #3
0
def cmd_rb_play(bot, user, text, command, parameter):
    global log

    log.debug('cmd: Play a station by ID')
    if not parameter:
        log.debug('rbplay without parameter')
        msg = tr('rb_play_empty')
        bot.send_msg(msg, text)
    else:
        log.debug('cmd: Retreiving url for station ID ' + parameter)
        rb = RadioBrowser()
        rstation = rb.station_by_uuid(parameter)
        stationname = rstation[0]['name']
        country = rstation[0]['countrycode']
        codec = rstation[0]['codec']
        bitrate = rstation[0]['bitrate']
        genre = rstation[0]['tags']
        homepage = rstation[0]['homepage']
        url = rstation[0]['url']
        msg = 'Radio station added to playlist:'

        msg += '<table><tr><th>ID</th><th>Station Name</th><th>Genre</th><th>Codec/Bitrate</th><th>Country</th><th>Homepage</th></tr>' + \
               f"<tr><td>{parameter}</td><td>{stationname}</td><td>{genre}</td><td>{codec}/{bitrate}</td><td>{country}</td><td>{homepage}</td></tr></table>"
        log.debug(f'cmd: Added station to playlist {stationname}')
        bot.send_msg(msg, text)
        if url != "-1":
            log.info('cmd: Found url: ' + url)
            music_wrapper = get_cached_wrapper_from_scrap(type='radio', url=url, name=stationname, user=user)
            var.playlist.append(music_wrapper)
            log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
            bot.async_download_next()
        else:
            log.info('cmd: No playable url found.')
            msg += "No playable url found for this station, please try another station."
            bot.send_msg(msg, text)
Example #4
0
def cmd_shortlist(bot, user, text, command, parameter):
    global song_shortlist, log
    try:
        indexes = [int(i) for i in parameter.split(" ")]
    except ValueError:
        bot.send_msg(constants.strings('bad_parameter', command=command), text)
        return

    if len(indexes) > 1:
        msgs = [constants.strings('multiple_file_added') + "<ul>"]
        for index in indexes:
            if 1 <= index <= len(song_shortlist):
                kwargs = song_shortlist[index - 1]
                kwargs['user'] = user
                music_wrapper = get_cached_wrapper_from_scrap(bot, **kwargs)
                var.playlist.append(music_wrapper)
                log.info("cmd: add to playlist: " +
                         music_wrapper.format_debug_string())
                msgs.append("<li>[{}] <b>{}</b></li>".format(
                    music_wrapper.item().type,
                    music_wrapper.item().title))
            else:
                bot.send_msg(
                    constants.strings('bad_parameter', command=command), text)
                return

        msgs.append("</ul>")
        send_multi_lines(bot, msgs, None, "")
        return
    elif len(indexes) == 1:
        index = indexes[0]
        if 1 <= index <= len(song_shortlist):
            kwargs = song_shortlist[index - 1]
            kwargs['user'] = user
            music_wrapper = get_cached_wrapper_from_scrap(bot, **kwargs)
            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

    bot.send_msg(constants.strings('bad_parameter', command=command), text)
Example #5
0
def cmd_play_url(bot, user, text, command, parameter):
    global log

    url = util.get_url_from_input(parameter)
    if url:
        music_wrapper = get_cached_wrapper_from_scrap(type='url', url=url, user=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)
        if len(var.playlist) == 2:
            # If I am the second item on the playlist. (I am the next one!)
            bot.async_download_next()
    else:
        bot.send_msg(tr('bad_parameter', command=command), text)
Example #6
0
def cmd_play_playlist(bot, user, text, command, parameter):
    global log

    offset = 0  # if you want to start the playlist at a specific index
    try:
        offset = int(parameter.split(" ")[-1])
    except ValueError:
        pass

    url = util.get_url_from_input(parameter)
    log.debug(f"cmd: fetching media info from playlist url {url}")
    items = get_playlist_info(url=url, start_index=offset, user=user)
    if len(items) > 0:
        items = var.playlist.extend(list(map(lambda item: get_cached_wrapper_from_scrap(**item), items)))
        for music in items:
            log.info("cmd: add to playlist: " + music.format_debug_string())
    else:
        bot.send_msg(tr("playlist_fetching_failed"), text)
Example #7
0
def post():
    global log

    if request.method == 'POST':
        if request.form:
            log.debug("web: Post request from %s: %s" %
                      (request.remote_addr, str(request.form)))

        if 'add_item_at_once' in request.form:
            music_wrapper = get_cached_wrapper_by_id(
                request.form['add_item_at_once'], user)
            if music_wrapper:
                var.playlist.insert(var.playlist.current_index + 1,
                                    music_wrapper)
                log.info('web: add to playlist(next): ' +
                         music_wrapper.format_debug_string())
                if not var.bot.is_pause:
                    var.bot.interrupt()
                else:
                    var.bot.is_pause = False
            else:
                abort(404)

        if 'add_item_bottom' in request.form:
            music_wrapper = get_cached_wrapper_by_id(
                request.form['add_item_bottom'], user)

            if music_wrapper:
                var.playlist.append(music_wrapper)
                log.info('web: add to playlist(bottom): ' +
                         music_wrapper.format_debug_string())
            else:
                abort(404)

        elif 'add_item_next' in request.form:
            music_wrapper = get_cached_wrapper_by_id(
                request.form['add_item_next'], user)
            if music_wrapper:
                var.playlist.insert(var.playlist.current_index + 1,
                                    music_wrapper)
                log.info('web: add to playlist(next): ' +
                         music_wrapper.format_debug_string())
            else:
                abort(404)

        elif 'add_url' in request.form:
            music_wrapper = get_cached_wrapper_from_scrap(
                type='url', url=request.form['add_url'], user=user)
            var.playlist.append(music_wrapper)

            log.info("web: add to playlist: " +
                     music_wrapper.format_debug_string())
            if len(var.playlist) == 2:
                # If I am the second item on the playlist. (I am the next one!)
                var.bot.async_download_next()

        elif 'add_radio' in request.form:
            url = request.form['add_radio']
            music_wrapper = get_cached_wrapper_from_scrap(type='radio',
                                                          url=url,
                                                          user=user)
            var.playlist.append(music_wrapper)

            log.info("cmd: add to playlist: " +
                     music_wrapper.format_debug_string())

        elif 'delete_music' in request.form:
            music_wrapper = var.playlist[int(request.form['delete_music'])]
            log.info("web: delete from playlist: " +
                     music_wrapper.format_debug_string())

            if len(var.playlist) >= int(request.form['delete_music']):
                index = int(request.form['delete_music'])

                if index == var.playlist.current_index:
                    var.playlist.remove(index)

                    if index < len(var.playlist):
                        if not var.bot.is_pause:
                            var.bot.interrupt()
                            var.playlist.current_index -= 1
                            # then the bot will move to next item

                    else:  # if item deleted is the last item of the queue
                        var.playlist.current_index -= 1
                        if not var.bot.is_pause:
                            var.bot.interrupt()
                else:
                    var.playlist.remove(index)

        elif 'play_music' in request.form:
            music_wrapper = var.playlist[int(request.form['play_music'])]
            log.info("web: jump to: " + music_wrapper.format_debug_string())

            if len(var.playlist) >= int(request.form['play_music']):
                var.playlist.point_to(int(request.form['play_music']) - 1)
                if not var.bot.is_pause:
                    var.bot.interrupt()
                else:
                    var.bot.is_pause = False
                time.sleep(0.1)

        elif 'delete_item_from_library' in request.form:
            _id = request.form['delete_item_from_library']
            var.playlist.remove_by_id(_id)
            item = var.cache.get_item_by_id(_id)

            if os.path.isfile(item.uri()):
                log.info("web: delete file " + item.uri())
                os.remove(item.uri())

            var.cache.free_and_delete(_id)
            time.sleep(0.1)

        elif 'add_tag' in request.form:
            music_wrappers = get_cached_wrappers_by_tags(
                [request.form['add_tag']], user)
            for music_wrapper in music_wrappers:
                log.info("cmd: add to playlist: " +
                         music_wrapper.format_debug_string())
            var.playlist.extend(music_wrappers)

        elif 'action' in request.form:
            action = request.form['action']
            if action == "randomize":
                if var.playlist.mode != "random":
                    var.playlist = media.playlist.get_playlist(
                        "random", var.playlist)
                else:
                    var.playlist.randomize()
                var.bot.interrupt()
                var.db.set('playlist', 'playback_mode', "random")
                log.info("web: playback mode changed to random.")
            if action == "one-shot":
                var.playlist = media.playlist.get_playlist(
                    "one-shot", var.playlist)
                var.db.set('playlist', 'playback_mode', "one-shot")
                log.info("web: playback mode changed to one-shot.")
            if action == "repeat":
                var.playlist = media.playlist.get_playlist(
                    "repeat", var.playlist)
                var.db.set('playlist', 'playback_mode', "repeat")
                log.info("web: playback mode changed to repeat.")
            if action == "autoplay":
                var.playlist = media.playlist.get_playlist(
                    "autoplay", var.playlist)
                var.db.set('playlist', 'playback_mode', "autoplay")
                log.info("web: playback mode changed to autoplay.")
            if action == "rescan":
                var.cache.build_dir_cache()
                log.info("web: Local file cache refreshed.")
            elif action == "stop":
                if var.config.getboolean("bot", "clear_when_stop_in_oneshot", fallback=False) \
                        and var.playlist.mode == 'one-shot':
                    var.bot.clear()
                else:
                    var.bot.stop()
            elif action == "pause":
                var.bot.pause()
            elif action == "resume":
                var.bot.resume()
            elif action == "clear":
                var.bot.clear()
            elif action == "volume_up":
                if var.bot.volume_set + 0.03 < 1.0:
                    var.bot.volume_set = var.bot.volume_set + 0.03
                else:
                    var.bot.volume_set = 1.0
                var.db.set('bot', 'volume', str(var.bot.volume_set))
                log.info("web: volume up to %d" % (var.bot.volume_set * 100))
            elif action == "volume_down":
                if var.bot.volume_set - 0.03 > 0:
                    var.bot.volume_set = var.bot.volume_set - 0.03
                else:
                    var.bot.volume_set = 0
                var.db.set('bot', 'volume', str(var.bot.volume_set))
                log.info("web: volume up to %d" % (var.bot.volume_set * 100))

    return status()
Example #8
0
def post():
    global log

    payload = request.form if request.form else request.json
    if payload:
        log.debug("web: Post request from %s: %s" %
                  (request.remote_addr, str(payload)))

        if 'add_item_at_once' in payload:
            music_wrapper = get_cached_wrapper_by_id(
                payload['add_item_at_once'], user)
            if music_wrapper:
                var.playlist.insert(var.playlist.current_index + 1,
                                    music_wrapper)
                log.info('web: add to playlist(next): ' +
                         music_wrapper.format_debug_string())
                if not var.bot.is_pause:
                    var.bot.interrupt()
                else:
                    var.bot.is_pause = False
            else:
                abort(404)

        if 'add_item_bottom' in payload:
            music_wrapper = get_cached_wrapper_by_id(
                payload['add_item_bottom'], user)

            if music_wrapper:
                var.playlist.append(music_wrapper)
                log.info('web: add to playlist(bottom): ' +
                         music_wrapper.format_debug_string())
            else:
                abort(404)

        elif 'add_item_next' in payload:
            music_wrapper = get_cached_wrapper_by_id(payload['add_item_next'],
                                                     user)
            if music_wrapper:
                var.playlist.insert(var.playlist.current_index + 1,
                                    music_wrapper)
                log.info('web: add to playlist(next): ' +
                         music_wrapper.format_debug_string())
            else:
                abort(404)

        elif 'add_url' in payload:
            music_wrapper = get_cached_wrapper_from_scrap(
                type='url', url=payload['add_url'], user=user)
            var.playlist.append(music_wrapper)

            log.info("web: add to playlist: " +
                     music_wrapper.format_debug_string())
            if len(var.playlist) == 2:
                # If I am the second item on the playlist. (I am the next one!)
                var.bot.async_download_next()

        elif 'add_radio' in payload:
            url = payload['add_radio']
            music_wrapper = get_cached_wrapper_from_scrap(type='radio',
                                                          url=url,
                                                          user=user)
            var.playlist.append(music_wrapper)

            log.info("cmd: add to playlist: " +
                     music_wrapper.format_debug_string())

        elif 'delete_music' in payload:
            music_wrapper = var.playlist[int(payload['delete_music'])]
            log.info("web: delete from playlist: " +
                     music_wrapper.format_debug_string())

            if len(var.playlist) >= int(payload['delete_music']):
                index = int(payload['delete_music'])

                if index == var.playlist.current_index:
                    var.playlist.remove(index)

                    if index < len(var.playlist):
                        if not var.bot.is_pause:
                            var.bot.interrupt()
                            var.playlist.current_index -= 1
                            # then the bot will move to next item

                    else:  # if item deleted is the last item of the queue
                        var.playlist.current_index -= 1
                        if not var.bot.is_pause:
                            var.bot.interrupt()
                else:
                    var.playlist.remove(index)

        elif 'play_music' in payload:
            music_wrapper = var.playlist[int(payload['play_music'])]
            log.info("web: jump to: " + music_wrapper.format_debug_string())

            if len(var.playlist) >= int(payload['play_music']):
                var.bot.play(int(payload['play_music']))
                time.sleep(0.1)
        elif 'move_playhead' in payload:
            if float(payload['move_playhead']) < var.playlist.current_item(
            ).item().duration:
                log.info(
                    f"web: move playhead to {float(payload['move_playhead'])} s."
                )
                var.bot.play(var.playlist.current_index,
                             float(payload['move_playhead']))

        elif 'delete_item_from_library' in payload:
            _id = payload['delete_item_from_library']
            var.playlist.remove_by_id(_id)
            item = var.cache.get_item_by_id(_id)

            if os.path.isfile(item.uri()):
                log.info("web: delete file " + item.uri())
                os.remove(item.uri())

            var.cache.free_and_delete(_id)
            time.sleep(0.1)

        elif 'add_tag' in payload:
            music_wrappers = get_cached_wrappers_by_tags([payload['add_tag']],
                                                         user)
            for music_wrapper in music_wrappers:
                log.info("cmd: add to playlist: " +
                         music_wrapper.format_debug_string())
            var.playlist.extend(music_wrappers)

        elif 'action' in payload:
            action = payload['action']
            if action == "randomize":
                if var.playlist.mode != "random":
                    var.playlist = media.playlist.get_playlist(
                        "random", var.playlist)
                else:
                    var.playlist.randomize()
                var.bot.interrupt()
                var.db.set('playlist', 'playback_mode', "random")
                log.info("web: playback mode changed to random.")
            if action == "one-shot":
                var.playlist = media.playlist.get_playlist(
                    "one-shot", var.playlist)
                var.db.set('playlist', 'playback_mode', "one-shot")
                log.info("web: playback mode changed to one-shot.")
            if action == "repeat":
                var.playlist = media.playlist.get_playlist(
                    "repeat", var.playlist)
                var.db.set('playlist', 'playback_mode', "repeat")
                log.info("web: playback mode changed to repeat.")
            if action == "autoplay":
                var.playlist = media.playlist.get_playlist(
                    "autoplay", var.playlist)
                var.db.set('playlist', 'playback_mode', "autoplay")
                log.info("web: playback mode changed to autoplay.")
            if action == "rescan":
                var.cache.build_dir_cache()
                var.music_db.manage_special_tags()
                log.info("web: Local file cache refreshed.")
            elif action == "stop":
                if var.config.getboolean("bot", "clear_when_stop_in_oneshot", fallback=False) \
                        and var.playlist.mode == 'one-shot':
                    var.bot.clear()
                else:
                    var.bot.stop()
            elif action == "next":
                if not var.bot.is_pause:
                    var.bot.interrupt()
                else:
                    var.playlist.next()
                    var.bot.wait_for_ready = True
            elif action == "pause":
                var.bot.pause()
            elif action == "resume":
                var.bot.resume()
            elif action == "clear":
                var.bot.clear()
            elif action == "volume_up":
                if var.bot.volume_helper.plain_volume_set + 0.03 < 1.0:
                    var.bot.volume_helper.set_volume(
                        var.bot.volume_helper.plain_volume_set + 0.03)
                else:
                    var.bot.volume_helper.set_volume(1.0)
                var.db.set('bot', 'volume',
                           str(var.bot.volume_helper.plain_volume_set))
                log.info("web: volume up to %d" %
                         (var.bot.volume_helper.plain_volume_set * 100))
            elif action == "volume_down":
                if var.bot.volume_helper.plain_volume_set - 0.03 > 0:
                    var.bot.volume_helper.set_volume(
                        var.bot.unconverted_volume - 0.03)
                else:
                    var.bot.volume_helper.set_volume(1.0)
                var.db.set('bot', 'volume',
                           str(var.bot.volume_helper.plain_volume_set))
                log.info("web: volume down to %d" %
                         (var.bot.volume_helper.plain_volume_set * 100))
            elif action == "volume_set_value":
                if 'new_volume' in payload:
                    if float(payload['new_volume']) > 1:
                        var.bot.volume_helper.set_volume(1.0)
                    elif float(payload['new_volume']) < 0:
                        var.bot.volume_helper.set_volume(0)
                    else:
                        # value for new volume is between 0 and 1, round to two decimal digits
                        var.bot.volume_helper.set_volume(
                            round(float(payload['new_volume']), 2))

                    var.db.set('bot', 'volume',
                               str(var.bot.volume_helper.plain_volume_set))
                    log.info("web: volume set to %d" %
                             (var.bot.volume_helper.plain_volume_set * 100))

    return status()