def push(): config = { 'GCM_API_KEY': variables.get("gcm_api_key", "") } app.config.update(config) client = FlaskGCM() client.init_app(app) with app.app_context(): tokens = variables.get("gcm_tokens", []) if not tokens: log("No devices registered") return playing = variables.get("playing", []) alert = {"artist": playing[0], "album": playing[1], "song": playing[2], "duration": variables.get("song_duration", 0)} # Send to single device. # NOTE: Keyword arguments are optional. res = client.send(tokens, alert, collapse_key='collapse_key', delay_while_idle=False, time_to_live=600)
def long_poll(): artist, album, song = variables.get("playing", []) while True: # Return when the song has changed new_artist, new_album, new_song = variables.get("playing", []) if artist != new_artist or album != new_album or song != new_song: # New song is played json = {"artist": new_artist, "album": new_album, "song": new_song} return flask.jsonify(**json) time.sleep(0.5)
def next_song(): queue = variables.get("queue", None) playing = variables.get("playing", None) if queue is None or playing is None: return "Something went wrong" queue_nr = queue.index(playing) song = queue[(queue_nr + 1) % len(queue)] song_obj = library.get_song(song[0], song[1], song[2]) server.audio.play(song_obj) return song[0] + ";" + song[1] + ";" + song[2]
def get_status(): # Returns JSON with server status artist, album, song = variables.get("playing", []) json = dict() status = variables.get("status", -1) json["status"] = status if status != variables.STOPPED: json["playing"] = {"artist": artist, "album": album, "song": song, "elapsed": variables.get("elapsed", 0), "duration": variables.get("song_duration", 0)} json["volume"] = variables.get("volume", 50) return flask.jsonify(**json)
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 get_playing(): # Return songObject of song played ans = variables.get("playing", None) if ans is None: return ans artist, album, song = ans return library.get_song(artist, album, song)
def get_route(): queue = variables.get("queue", []) if not queue: # Queue is empty return "No queue found" songlist = [{"artist": i[0], "album": i[1], "song": i[2]} for i in queue] json = {"queue": songlist} return flask.jsonify(**json)
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 previous_song(): queue = variables.get("queue", None) playing = variables.get("playing", None) if queue is None or playing is None: return "Something went wrong" queue_nr = queue.index(playing) if variables.get("elapsed", 0) > 4: # Replay the song song = playing log("Replaying song from beginning:" + str(song)) else: # Play the previous song song = queue[queue_nr - 1] song_obj = library.get_song(song[0], song[1], song[2]) server.audio.play(song_obj) return song[0] + ";" + song[1] + ";" + song[2]
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 fade_in(s): # Fade in in s seconds if s < 0.5: log("Time to fade in may not be smaller than 0.5 seconds.") s = 0.5 volume = variables.get("volume", 75) s -= 0.5 # Compensate for computing time for i in range(20): vol = volume * (i / 20.) set_volume(int(vol), True) time.sleep(s / 20.)
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 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 fade_out(s): # Music paused afterwards and original volume restored. if s < 0.5: log("Time to fade out may not be smaller than 0.5 seconds.") s = 0.5 volume = variables.get("volume", 75) s -= 0.5 # Compensate for computing time for i in range(20): vol = volume * (1 - (i / 20.)) set_volume(int(vol), True) time.sleep(s / 20.) pause() # Pause the music # Wait, otherwise you hear the music resume for a fraction of a second time.sleep(0.5) set_volume(volume, True) # Set the volume back