def play_music_album(artist, album): """ Shuffle the album. :param artist: String of artist name :param album: String of album name :return: Not important """ artist = artist.replace("_", " ") album = album.replace("_", " ") album_object = library.get_album(artist, album) songs = album_object.get_songs() random.shuffle(songs) # Set playlist playlist = [] for song in songs: artist_name = song.get_artist().get_name() album_title = song.get_album().get_title() song_name = song.get_title() playlist.append([artist_name, album_title, song_name]) variables.put("queue", playlist) # Play first song song = songs[0] server.audio.play(song) return "Playing %s by %s : %s" % (song.get_title(), song.get_artist().get_name(), song.get_album().get_title())
def register_token(): if not valid_ip(): block_user() data = request.data log("Token: " + str(data)) tokens = variables.get("gmc_tokens", []) if data not in tokens: tokens.append(data) variables.put("gcm_tokens", tokens) return "Successfully registered token: %s" % data
def post_queue(): if not valid_ip(): block_user() data = request.data song_strings = data.split(";") playlist = [] for song_str in song_strings: artist_name, album_title, song_name = song_str.split(":") playlist.append([artist_name, album_title, song_name]) variables.put("queue", playlist) return "Queue received succesfully"
def set_volume(vol, fade=False): # Set volume in percentage. Volume has to be between 0 and 100 if 0 <= vol <= 100: # Don't set new volume if this function is called for fading in or out if not fade: variables.put("volume", vol) if vol == 0: os.system("amixer -q sset PCM 0%") return True # Map 0-100% to 50-100% through a function percentage = 5 * vol**0.5 + 50 command = "amixer -q sset PCM " + str(percentage) + "%" os.system(command) return True return False
def resume(): status = variables.get("status", variables.STOPPED) if status == variables.PLAYING: return elif status == variables.STOPPED: # Music is stopped, play song from beginning playing = variables.get("playing", None) if playing is None: return song_obj = library.get_song(playing[0], playing[1], playing[2]) play(song_obj) elif status == variables.PAUSED: os.system("pkill -CONT mpg123") variables.put("status", variables.PLAYING)
def start_timer(): if variables.get("stop_timer", False): # A timer is already waiting to start return variables.put("stop_timer", True, False) variables.put("elapsed", 0, False) time.sleep(0.5) # Wait to give the other timer time to stop variables.put("stop_timer", False, False) # The timer starts playing = variables.get("playing", None) if playing is None: log("Audio.py: playing == None!") return song = library.get_song(playing[0], playing[1], playing[2]) start_time = variables.get("song_start", None) if start_time is None: log("Audio.py: song_start = None!") return # Start counting while True: time_elapsed = time.time() - start_time if time_elapsed >= float(song.get_duration()): # Song is done playing, play next song queue = variables.get("queue", None) if queue is None: log("Audio.py: queue = None!") return # Play new song queue_nr = queue.index(playing) queue_nr += 1 song = queue[queue_nr % len(queue)] # Make dat shit loop song_obj = library.get_song(song[0], song[1], song[2]) play(song_obj) return # Stop this timer if variables.get("stop_timer", False): # A new timer is ready to go, stop this one return # If the music is paused if variables.get("status", -1) == variables.PAUSED: start_pause_time = time.time() while variables.get("status", -1) == variables.PAUSED: time.sleep(0.1) # Wait for the music to be continued start_time += time.time() - start_pause_time variables.put("elapsed", int(time_elapsed*1000)/1000., False) time.sleep(0.1) # Sleep for a bit
def register_ip(): ip = request.remote_addr key = request.args.get("key") if key is None: log("Registery denied") return "Register failed" if hashlib.sha224(key).hexdigest() != \ "e40206e07b61b34c898d38e6756d99c6bbc74279445afa320c0eb053": bannedlist = variables.get("banned", []) bannedlist.append(ip) variables.put("banned", bannedlist) log("IP banned") return "IP banned" ips = variables.get("ip", []) if ip in ips: log("IP already registered") return "IP already registered" ips.append(ip) variables.put("ip", ips) log("Registered succesfully") return "Registered succesfully"
def play(song): log("Playing song: %s by %s" %(song.get_title(), song.get_artist().get_name())) variables.put("status", variables.PLAYING) variables.put("playing", [song.get_artist().get_name(), song.get_album().get_title(), song.get_title()]) variables.put("song_duration", song.get_duration()) variables.put("song_start", time.time(), False) os.system("pkill mpg123") # Kill everything that might be playing os.system('mpg123 -q "%s" &' % song.get_path()) # Play the song push() # Notify the users # Start counting in new thread thread = Thread(target=start_timer) thread.start()
def pause(): os.system("pkill -STOP mpg123") variables.put("status", variables.PAUSED)
def stop(): variables.put("status", variables.STOPPED) variables.put("stop_timer", True, False) os.system("pkill mpg123")
import hashlib import flask import server.audio import lib.string from ast import literal_eval from flask import abort from flask import send_file from flask import request from flask import render_template from server import app from server import library from server import variables from lib.log import log # Set stop_timer on False on server boot variables.put("stop_timer", False, False) variables.put("status", variables.STOPPED) def valid_ip(): """ :return: True when the user is permitted to call the function with it's ip, False if the user is not permitted. """ ip = request.remote_addr return ip in variables.get("ip", []) or str(request.path) == "/register_ip" def block_user(): abort(401)