def list_devices(show_desktop, show_mobile): """ List device IDs registered with Google Music. Defaults to showing both desktop and mobile IDs. """ from gmusicapi.clients import Webclient from keyring import get_password webclient = Webclient() email = app.config['GACCOUNT_EMAIL'] password = get_password('gmusicprocurator', email) if password is None: error('Password not set. Please run the set_password subcommand.') return success = webclient.login(email, password) if not success: error('Login failed.') return for device in webclient.get_registered_devices(): dname = device['name'] if len(device['name']) > 0 else device['model'] if not show_desktop and device['type'] == 'DESKTOP_APP': continue if not show_mobile and device['type'] == 'PHONE': continue print(u'* {dname} ({type}): {id}'.format(dname=dname, **device))
def list_devices(show_desktop, show_mobile): """ List device IDs registered with Google Music. Defaults to showing both desktop and mobile IDs. """ from gmusicapi.clients import Webclient webclient = Webclient() success = webclient.login(app.config['GACCOUNT_EMAIL'], app.config['GACCOUNT_PASSWORD']) if not success: print('Login failed.', file=sys.stderr) return for device in webclient.get_registered_devices(): dname = device['name'] if len(device['name']) > 0 else device['model'] if not show_desktop and device['type'] == 'DESKTOP_APP': continue if not show_mobile and device['type'] == 'PHONE': continue print(u'* {dname} ({type}): {id}'.format(dname=dname, **device))
def prompt_for_wc_auth(): """Return a valid (user, pass) tuple by continually prompting the user.""" print ("These tests will never delete or modify your music." "\n\n" "If the tests fail, you *might* end up with a test" " song/playlist in your library, though." "\n") wclient = Webclient() valid_wc_auth = False while not valid_wc_auth: print() email = input("Email: ") passwd = getpass() valid_wc_auth = wclient.login(email, passwd) return email, passwd
def freeze_login_details(): """Searches the environment for credentials, and freezes them to client.login if found. If no auth is present in the env, the user is prompted. OAuth is read from the default path. If running on Travis, the prompt will never be fired; sys.exit is called if the envvars are not present. """ #Attempt to get auth from environ. user, passwd, refresh_tok = [ os.environ.get(name) for name in ('GM_USER', 'GM_PASS', 'GM_OAUTH') ] on_travis = os.environ.get('TRAVIS') mm_kwargs = {} wc_kwargs = {} has_env_auth = user and passwd and refresh_tok if not has_env_auth and on_travis: print 'on Travis but could not read auth from environ; quitting.' sys.exit(1) if os.environ.get('TRAVIS'): #Travis runs on VMs with no "real" mac - we have to provide one. mm_kwargs.update({ 'uploader_id': travis_id, 'uploader_name': travis_name }) if has_env_auth: wc_kwargs.update({'email': user, 'password': passwd}) # mm expects a full OAuth2Credentials object credentials = credentials_from_refresh_token(refresh_tok) mm_kwargs.update({'oauth_credentials': credentials}) else: # no travis, no credentials # we need to login here to verify their credentials. # the authenticated api is then thrown away. wclient = Webclient() valid_auth = False print( "These tests will never delete or modify your music." "\n\n" "If the tests fail, you *might* end up with a test" " song/playlist in your library, though." "You must have oauth credentials stored at the default" " path by Musicmanager.perform_oauth prior to running.") while not valid_auth: print email = raw_input("Email: ") passwd = getpass() valid_auth = wclient.login(email, passwd) wc_kwargs.update({'email': email, 'password': passwd}) # globally freeze our params in place. # they can still be overridden manually; they're just the defaults now. Musicmanager.login = MethodType( update_wrapper(partial(Musicmanager.login, **mm_kwargs), Musicmanager.login), None, Musicmanager) Webclient.login = MethodType( update_wrapper(partial(Webclient.login, **wc_kwargs), Webclient.login), None, Webclient)
def no_client_auth_initially(): wc = Webclient() assert_false(wc.is_authenticated()) mm = Musicmanager() assert_false(mm.is_authenticated())