def validate(client_id, p, ip, user_agent): if APIController.get_salted(p) == SecureSettings.get_string( "api_password"): client_key = APIController.get_salted(client_id) session_key = AuthController.generate_session_key() Database().add_client(client_key, session_key, ip, user_agent) return True, session_key return False, None
def restart_device(): instance = int(request.args.get("instance")) if instance == 1: os.system('sudo reboot') else: APIController().slave_command(instance, "system", "restart_device") return "OK"
def notify_next_episode(self, callback): if self.next_type is not None: Logger().write( LogVerbosity.Info, "Can continue with next episode: " + self.next_title) APIController().ui_request("SelectNextEpisode", callback, 60 * 60, self.next_title)
def on_init(client_name, key): Logger().write(LogVerbosity.Info, "Init slave: " + client_name) if key != SecureSettings.get_string("master_key"): Logger().write(LogVerbosity.Info, "Slave authentication failed") disconnect() return False slave = APIController.slaves.get_slave(client_name) if slave is not None: if slave.connected: Logger().write( LogVerbosity.Info, "Slave " + str(client_name) + " connected twice?") disconnect() return False else: Logger().write(LogVerbosity.Info, "Slave " + str(client_name) + " reconnected") slave.reconnect(request.sid) APIController.slaves.changed() return True client = WebsocketClient(request.sid, current_time()) client.authenticated = True APIController.slaves.add_slave( SlaveClient(APIController.next_id(), client_name, client)) return client.authenticated
def media_selection_required(self, files): if Settings.get_bool("slave"): data, = SlaveClientController.request_master( "get_history_for_url", 5, self.torrent.uri) if data: history = [ History(x['id'], x['imdb_id'], x['type'], x['title'], x['image'], x['watched_at'], x['season'], x['episode'], x['url'], x['media_file'], x['played_for'], x['length']) for x in json.loads(data) ] else: history = [] else: history = Database().get_history_for_url(self.torrent.uri) for file in files: seen = [x for x in history if x.media_file == file.path] file.seen = len(seen) > 0 if file.seen: seen = seen[-1] file.played_for = seen.played_for file.play_length = seen.length APIController().ui_request("SelectMediaFile", self.set_media_file, 60 * 30, files)
def _send_ui_request(topic, data, timeout): Logger().write(LogVerbosity.Debug, "Sending ui request: " + topic + ", data: " + str(data)) request_id = APIController().next_id() request = Request(request_id, topic, data, None, SlaveClientController._complete_request) SlaveClientController.requests.append(request) SlaveClientController.slave_ns.emit("ui_request", request_id, topic, data, timeout) return request
def update(): instance = int(request.args.get("instance")) if instance == 1: Updater().update() else: APIController().slave_command(instance, "updater", "update") return "OK"
def _send_request(self, topic, data, room): Logger().write(LogVerbosity.Debug, "Sending request: " + topic + ", data: " + str(data)) request_id = APIController().next_id() request_message = Request(request_id, topic, data, room, self._complete_request) self.requests.append(request_message) data = to_JSON(data) self.emit("request", (request_id, topic, data), room=room) return request_message
def close_application(): instance = int(request.args.get("instance")) if instance == 1: sys.exit() else: APIController().slave_command(instance, "system", "close_application") return "OK"
def restart_application(): instance = int(request.args.get("instance")) if instance == 1: python = sys.executable os.execl(python, python, *sys.argv) else: APIController().slave_command(instance, "system", "restart_application") return "OK"
def stop_player(): instance = int(request.args.get("instance")) Logger().write(LogVerbosity.Info, "Stop playing on " + str(instance)) if instance == 1: MediaManager().stop_play() else: APIController().slave_command(instance, "media", "stop_play") return "OK"
def pause_resume_player(): instance = int(request.args.get("instance")) Logger().write(LogVerbosity.Info, "Pause/resume on " + str(instance)) if instance == 1: MediaManager().pause_resume() else: APIController().slave_command(instance, "media", "pause_resume") return "OK"
def seek(): instance = int(request.args.get("instance")) position = int(request.args.get("position")) Logger().write(LogVerbosity.Info, "Seek " + str(instance) + " to " + str(position)) if instance == 1: MediaManager().seek(position) else: APIController().slave_command(instance, "media", "seek", position) return "OK"
def get_update(): instance = int(request.args.get("instance")) if instance == 1: return to_JSON( UpdateAvailable(Updater().check_version(), Updater().last_version)) else: result = APIController().slave_request(instance, "get_last_version", 10) if result is None: return to_JSON(UpdateAvailable(False, "")) return to_JSON(UpdateAvailable(result[0], result[1]))
def change_audio(): instance = int(request.args.get("instance")) track = int(request.args.get("track")) Logger().write(LogVerbosity.Info, "Set audio on " + str(instance) + " to " + str(track)) if instance == 1: MediaManager().change_audio(track) else: APIController().slave_command(instance, "media", "change_audio", track) return "OK"
def change_volume(): instance = int(request.args.get("instance")) volume = int(request.args.get("volume")) Logger().write(LogVerbosity.Info, "Set volume on " + str(instance) + " to " + str(volume)) if instance == 1: MediaManager().change_volume(volume) else: APIController().slave_command(instance, "media", "change_volume", volume) return "OK"
def play_radio(): radio_id = int(request.args.get("id")) radio = [x for x in Database().get_radios() if x.id == radio_id][0] instance = int(request.args.get("instance")) Logger().write(LogVerbosity.Info, "Play radio " + radio.title + " on " + str(instance)) if instance == 1: MediaManager().start_radio(radio.title, radio.url) else: APIController().slave_command(instance, "media", "start_radio", radio.title, radio.url) return "OK"
def on_init(client_id, session_key): Logger().write(LogVerbosity.Info, "Init UI: " + client_id) client = [ x for x in UIWebsocketController.clients if x.sid == request.sid ][0] client_key = APIController.get_salted(client_id) client.authenticated = Database().check_session_key( client_key, session_key) if not client.authenticated: Logger().write(LogVerbosity.Debug, "UI invalid client/session key") return client.authenticated
def change_sub_delay(): instance = int(request.args.get("instance")) delay = int(request.args.get("delay")) Logger().write( LogVerbosity.Info, "Set sub delay on " + str(instance) + " to " + str(delay)) if instance == 1: MediaManager().change_subtitle_delay(delay) else: APIController().slave_command(instance, "media", "change_subtitle_delay", delay) return "OK"
def play_youtube(): instance = int(request.args.get("instance")) title = urllib.parse.unquote(request.args.get("title")) url = urllib.parse.unquote(request.args.get("url")) position = int(request.args.get("position")) Logger().write(LogVerbosity.Info, "Play youtube " + title + " on " + str(instance)) if instance == 1: MediaManager().start_youtube(title, url, position) else: APIController().slave_command(instance, "media", "start_youtube", title, url, position) return "OK"
def on_ui_request(request_id, topic, data, timeout): slave = APIController.slaves.get_slave_by_sid(request.sid) if slave is None: Logger().write(LogVerbosity.Debug, "Slave ui request for not initialized slave") disconnect() return Logger().write(LogVerbosity.Debug, "Slave ui request " + topic + ": " + data) slave = APIController.slaves.get_slave_by_sid(request.sid) APIController().ui_request( topic, lambda *x: SlaveWebsocketController.slave_ui_request_callback( request_id, slave, x), timeout, json.loads(data))
def play_file(): instance = int(request.args.get("instance")) position = int(request.args.get("position")) file = urllib.parse.unquote(request.args.get("path")) if sys.platform == "win32": file = "C:" + file Logger().write(LogVerbosity.Info, "Play file " + file + " on " + str(instance)) if instance == 1: MediaManager().start_file(file, position) else: APIController().slave_command(instance, "media", "start_file", file, position) return "OK"
def on_get_current_requests(): authenticated = [ x for x in UIWebsocketController.clients if x.sid == request.sid ][0].authenticated if not authenticated: Logger().write( LogVerbosity.Info, "Unauthenticated socket request for current requests") return for client_request in APIController().ui_websocket_controller.requests: socketio.emit("request", (client_request.request_id, client_request.topic, to_JSON(client_request.data)), namespace="/UI", room=request.sid)
def play_url(): instance = int(request.args.get("instance")) title = urllib.parse.unquote(request.args.get("title")) url = urllib.parse.unquote(request.args.get("url")) Logger().write( LogVerbosity.Info, "Play url " + title + "(" + url + ") on " + str(instance)) if instance == 1: if url.startswith("magnet:"): MediaManager().start_torrent(title, url, None) else: MediaManager().start_url(title, url) else: APIController().slave_command(instance, "media", "start_url", title, url) return "OK"
def login(): client_id = request.headers.get('Client-ID', None) p = request.args.get('p') ip_addr = request.headers.get('HTTP_X_FORWARDED_FOR', None) or request.remote_addr user_agent = request.user_agent.string success, key = AuthController.validate(client_id, p, ip_addr, user_agent) Logger().write(LogVerbosity.Info, str(client_id) + " log on result: " + str(success)) status = 200 if not success: Database().add_login_attempt(APIController.get_salted(client_id), ip_addr, user_agent, "Login") status = 401 return to_JSON(AuthResult(success, key)), status
def play_torrent(): instance = int(request.args.get("instance")) title = urllib.parse.unquote(request.args.get("title")) url = urllib.parse.unquote(request.args.get("url")) magnet_uri = TorrentController.get_magnet_url(url) if magnet_uri is None: Logger().write(LogVerbosity.Error, "Failed to find torrent magnet uri") return "OK" Logger().write(LogVerbosity.Info, "Play torrent " + title + " on " + str(instance)) if instance == 1: MediaManager().start_torrent(title, magnet_uri) else: APIController().slave_command(instance, "media", "start_torrent", title, magnet_uri) return "OK"
def play_episode(): instance = int(request.args.get("instance")) title = urllib.parse.unquote(request.args.get("title")) movie_id = request.args.get("id") url = urllib.parse.unquote(request.args.get("url")) img = urllib.parse.unquote(request.args.get("img")) position = int(request.args.get("position")) season = int(request.args.get("season")) episode = int(request.args.get("episode")) Logger().write(LogVerbosity.Info, "Play episode " + title + " on " + str(instance)) if instance == 1: MediaManager().start_episode(movie_id, season, episode, title, url, img, position) else: APIController().slave_command(instance, "media", "start_episode", movie_id, season, episode, title, url, img, position) return "OK"
def refresh(): client_id = request.headers.get('Client-ID', None) client_key = APIController.get_salted(client_id) client_known = Database().client_known(client_key) ip_addr = request.headers.get('HTTP_X_FORWARDED_FOR', None) or request.remote_addr user_agent = request.user_agent.string if not client_known: Logger().write(LogVerbosity.Info, str(client_id) + " failed to refresh") Database().add_login_attempt(client_key, ip_addr, user_agent, "Refresh") return to_JSON(AuthResult(False, None)), 401 session_key = AuthController.generate_session_key() Database().refresh_session_key(client_key, session_key, ip_addr, user_agent) Logger().write(LogVerbosity.Debug, str(client_id) + " successfully refreshed") return to_JSON(AuthResult(True, session_key)), 200
def aborting_torrent(self, reason): APIController().ui_message("Aborting torrent", reason) self.stop_play()
def on_ui_message(title, message): APIController().ui_message(title, message)