def authentication_refresh(): try: app.authentication_mgr.authenticate(do_refresh=True) except Exception as e: return app.error(str(e)) return app.success()
def input_send_button(console, button): btn = console.input_keys.get(button) if not btn: return app.error('Invalid button passed, button: {0}'.format(button), HTTPStatus.BAD_REQUEST) console.send_gamepad_button(btn) return app.success()
def authentication_load_from_disk(): try: app.authentication_mgr.load(app.token_file) except FileNotFoundError as e: return app.error('Failed to load tokens from \'{0}\'. Error: {1}'.format(e.filename, e.strerror), HTTPStatus.NOT_FOUND) return app.success()
def device_overview(): addr = request.args.get('addr') discovered = ConsoleWrap.discover(addr=addr).copy() liveids = [d.liveid for d in discovered] for i, c in enumerate(app.console_cache.values()): if c.liveid in liveids: # Refresh existing entries index = liveids.index(c.liveid) if c.device_status != discovered[index].device_status: app.console_cache[c.liveid] = ConsoleWrap(discovered[index]) del discovered[index] del liveids[index] elif c.liveid not in liveids: # Set unresponsive consoles to Unavailable app.console_cache[ c.liveid].console.device_status = enum.DeviceStatus.Unavailable # Extend by new entries for d in discovered: app.console_cache.update({d.liveid: ConsoleWrap(d)}) # Filter for specific console when ip address query is supplied (if available) data = { console.liveid: console.status for console in app.console_cache.values() if (addr and console.status.get('address') == addr) or not addr } return app.success(devices=data)
def authentication_oauth_post(): is_webview = request.form.get('webview') app.reset_authentication() redirect_uri = request.form.get('redirect_uri') if not redirect_uri: return app.error('Please provide redirect_url', HTTPStatus.BAD_REQUEST) try: access, refresh = AuthenticationManager.parse_redirect_url(redirect_uri) app.authentication_mgr.access_token = access app.authentication_mgr.refresh_token = refresh app.authentication_mgr.authenticate(do_refresh=False) app.authentication_mgr.dump(app.token_file) except Exception as e: if is_webview: return render_template('auth_result.html', title='Login fail', result='Login failed', message='Error message: {0}'.format(str(e)), link_path='/auth/login', link_title='Try again') else: return app.error('Login failed, error: {0}'.format(str(e))) if is_webview: return render_template('auth_result.html', title='Login success', result='Login succeeded', message='Welcome {}!'.format(app.logged_in_gamertag), link_path='/auth/logout', link_title='Logout') else: return app.success(message='Login success', gamertag=app.logged_in_gamertag)
def infrared_available_keys(console, device_id): stump_config = console.stump_config for device_config in stump_config.params: if device_config.device_id != device_id: continue button_links = {} for button in device_config.buttons: button_links[button] = { 'url': '/device/{0}/ir/{1}/{2}'.format(console.liveid, device_config.device_id, button), 'value': device_config.buttons[button] } return app.success(device_type=device_config.device_type, device_brand=device_config.device_brand, device_model=device_config.device_model, device_name=device_config.device_name, device_id=device_config.device_id, buttons=button_links) return app.error('Device Id \'{0}\' not found'.format(device_id), HTTPStatus.BAD_REQUEST)
def infrared(console): stump_config = console.stump_config devices = {} for device_config in stump_config.params: button_links = {} for button in device_config.buttons: button_links[button] = { 'url': '/device/{0}/ir/{1}/{2}'.format(console.liveid, device_config.device_id, button), 'value': device_config.buttons[button] } devices[device_config.device_type] = { 'device_type': device_config.device_type, 'device_brand': device_config.device_brand, 'device_model': device_config.device_model, 'device_name': device_config.device_name, 'device_id': device_config.device_id, 'buttons': button_links } return app.success(**devices)
def device_overview(): discovered = ConsoleWrap.discover().copy() liveids = [d.liveid for d in discovered] for i, c in enumerate(app.console_cache.values()): if c.liveid in liveids: # Refresh existing entries index = liveids.index(c.liveid) if c.device_status != discovered[index].device_status: app.console_cache[c.liveid] = ConsoleWrap(discovered[index]) del discovered[index] del liveids[index] elif c.liveid not in liveids: # Set unresponsive consoles to Unavailable app.console_cache[ c.liveid].console.device_status = enum.DeviceStatus.Unavailable # Extend by new entries for d in discovered: app.console_cache.update({d.liveid: ConsoleWrap(d)}) data = { console.liveid: console.status for console in app.console_cache.values() } return app.success(devices=data)
def media_command(console, command): cmd = console.media_commands.get(command) if not cmd: return app.error('Invalid command passed, command: {0}'.format(command), HTTPStatus.BAD_REQUEST) console.send_media_command(cmd) return app.success()
def authentication_login_post(): is_webview = request.form.get('webview') email_address = request.form.get('email') password = request.form.get('password') if app.authentication_mgr.authenticated: return app.error( 'An account is already signed in.. please logout first', HTTPStatus.BAD_REQUEST) elif not email_address or not password: return app.error('No email or password parameter provided', HTTPStatus.BAD_REQUEST) app.authentication_mgr.email_address = email_address app.authentication_mgr.password = password try: app.authentication_mgr.authenticate() app.authentication_mgr.dump(app.token_file) except AuthenticationException as e: if is_webview: return render_template('auth_result.html', title='Login fail', result='Login failed', message='Error: {0}!'.format(str(e)), link_path='/auth/login', link_title='Try again') else: return app.error('Login failed! Error: {0}'.format(str(e)), two_factor_required=False) except TwoFactorAuthRequired: if is_webview: return render_template( 'auth_result.html', title='Login fail', result='Login failed, 2FA required', message= 'Please click the following link to authenticate via OAUTH', link_path='/auth/oauth', link_title='Login via OAUTH') else: return app.error('Login failed, 2FA required!', two_factor_required=True) if is_webview: return render_template('auth_result.html', title='Login success', result='Login succeeded', message='Welcome {}!'.format( app.logged_in_gamertag), link_path='/auth/logout', link_title='Logout') else: return app.success(message='Login success', gamertag=app.logged_in_gamertag)
def authentication_store_on_disk(): if not app.authentication_mgr.authenticated: return app.error('Sorry, no valid authentication for saving was found', HTTPStatus.BAD_REQUEST) try: app.authentication_mgr.dump(app.token_file) except Exception as e: return app.error('Failed to save tokens to \'{0}\'. Error: {1}'.format(app.token_file, str(e))) return app.success()
def library_versions(): import pkg_resources versions = {} for name in app.smartglass_packetnames: try: versions[name] = pkg_resources.get_distribution(name).version except: versions[name] = None return app.success(versions=versions)
def authentication_logout_post(): is_webview = request.form.get('webview') username = app.logged_in_gamertag app.reset_authentication() if is_webview: return render_template('auth_result.html', title='Logout success', result='Logout succeeded', message='Goodbye {0}!'.format(username), link_path='/auth/login', link_title='Login') else: return app.success(message='Logout succeeded')
def gamedvr_record(console): """ Default to record last 60 seconds Adjust with start/end query parameter (delta time in seconds) """ try: start_delta = request.args.get('start', -60) end_delta = request.args.get('end', 0) console.dvr_record(int(start_delta), int(end_delta)) except Exception as e: return app.error('GameDVR failed, error: {0}'.format(e)) return app.success()
def authentication_overview(): tokens = { 'access_token': app.authentication_mgr.access_token, 'refresh_token': app.authentication_mgr.refresh_token, 'user_token': app.authentication_mgr.user_token, 'xsts_token': app.authentication_mgr.xsts_token } data = {} for k, v in tokens.items(): data.update({k: v.to_dict() if v else None}) userinfo = app.authentication_mgr.userinfo.to_dict() if app.authentication_mgr.userinfo else None return app.success(tokens=data, userinfo=userinfo, authenticated=app.authentication_mgr.authenticated)
def force_connect(console): try: userhash = '' xtoken = '' if app.authentication_mgr.authenticated and not request.args.get('anonymous'): userhash = app.authentication_mgr.userinfo.userhash xtoken = app.authentication_mgr.xsts_token.jwt state = console.connect(userhash, xtoken) except Exception as e: return app.error(str(e)) if state != enum.ConnectionState.Connected: return app.error('Connection failed', connection_state=state.name) return app.success(connection_state=state.name)
def console_status(console): status = console.console_status client = app.xbl_client # Update Title Info if client and status: for t in status['active_titles']: try: title_id = t['title_id'] resp = app.title_cache.get(title_id) if not resp: resp = client.titlehub.get_title_info(title_id, 'image').json() if resp['titles'][0]: app.title_cache[title_id] = resp t['name'] = resp['titles'][0]['name'] t['image'] = resp['titles'][0]['displayImage'] t['type'] = resp['titles'][0]['type'] except: pass return app.success(console_status=status)
def index(): routes = set([str(rule) for rule in app.url_map.iter_rules()]) return app.success(endpoints=sorted(routes))
def poweroff(console): if not console.power_off(): return app.error("Failed to power off") else: return app.success()
def disconnect(console): console.disconnect() return app.success()
def device_info(console): return app.success(device=console.status)
def stump_livetv_info(console): return app.success(livetv_info=console.livetv_info.params.dump())
def nano_stop(console): console.nano_stop() return app.success()
def nano_start(console): console.nano_start() return app.success()
def nano_overview(console): return app.success(nano_status=console.nano_status)
def text_send(console, text): console.send_text(text) return app.success()
def stump_tuner_lineups(console): return app.success(tuner_lineups=console.tuner_lineups.params.dump())
def index(): routes = [] for rule in app.url_map.iter_rules(): routes.append('%s' % rule) return app.success(endpoints=sorted(routes))
def text_overview(console): return app.success(text_session_active=console.text_active)
def poweron(liveid): ConsoleWrap.power_on(liveid) return app.success()