def test_initialise_account(): assert len(Account._all_accounts) == 0 accounts = Account.get_accounts() assert len(accounts) == 3 assert accounts['1'].username == '12345678' assert accounts['1'].service_type == '2311' assert accounts['1'].deleted == False assert accounts['1'].nickname == 'mysonos' assert accounts.get('4') is None
def desc_from_uri(uri): """Create the content of DIDL desc element from a uri Args: uri (str): A uri, eg: x-sonos-http:track%3a3402413.mp3?sid=2&flags=32&sn=4 Returns: (str): The content of a desc element for that uri, eg [email protected] """ # # If there is an sn parameter (which is the serial number of an account), # we can obtain all the infomration we need from that, because we can find # the relevant service_id in the account database (it is the same as the # service_type). Consequently, the sid parameter is unneeded. But if sn is # missing, we need the sid (service_type) parameter to find a relevant # account # urlparse does not work consistently with custom URI schemes such as # those used by Sonos. This is especially broken in Python 2.6 and # early versions of 2.7: http://bugs.python.org/issue9374 # As a workaround, we split off the scheme manually, and then parse # the uri as if it were http if ":" in uri: _, uri = uri.split(":", 1) query_string = parse_qs(urlparse(uri, 'http').query) # Is there an account serial number? if query_string.get('sn'): account_serial_number = query_string['sn'][0] try: account = Account.get_accounts()[account_serial_number] desc = "SA_RINCON{0}_{1}".format(account.service_type, account.username) return desc except KeyError: # There is no account matching this serial number. Fall back to # using the service id to find an account pass if query_string.get('sid'): service_id = query_string['sid'][0] for service in MusicService._get_music_services_data().values(): if service_id == service["ServiceID"]: service_type = service["ServiceType"] account = Account.get_accounts_for_service(service_type) if len(account) == 0: break # Use the first account we find account = account[0] desc = "SA_RINCON{0}_{1}".format(account.service_type, account.username) return desc # Nothing found. Default to the standard desc value. Is this the right # thing to do? desc = 'RINCON_AssociatedZPUDN' return desc
def desc_from_uri(uri): """Create the content of DIDL desc element from a uri Args: uri (str): A uri, eg: x-sonos-http:track%3a3402413.mp3?sid=2&flags=32&sn=4 Returns: (str): The content of a desc element for that uri, eg [email protected] """ # # If there is an sn parameter (which is the serial number of an account), # we can obtain all the infomration we need from that, because we can find # the relevant service_id in the account database (it is the same as the # service_type). Consequently, the sid parameter is unneeded. But if sn is # missing, we need the sid (service_type) parameter to find a relevant # account # urlparse does not work consistently with custom URI schemes such as # those used by Sonos. This is especially broken in Python 2.6 and # early versions of 2.7: http://bugs.python.org/issue9374 # As a workaround, we split off the scheme manually, and then parse # the uri as if it were http if ":" in uri: _, uri = uri.split(":", 1) query_string = parse_qs(urlparse(uri, 'http').query) # Is there an account serial number? if query_string.get('sn'): account_serial_number = query_string['sn'][0] try: account = Account.get_accounts()[account_serial_number] desc = "SA_RINCON{0}_{1}".format( account.service_type, account.username) return desc except KeyError: # There is no account matching this serial number. Fall back to # using the service id to find an account pass if query_string.get('sid'): service_id = query_string['sid'][0] for service in MusicService._get_music_services_data().values(): if service_id == service["ServiceID"]: service_type = service["ServiceType"] account = Account.get_accounts_for_service(service_type) if len(account) == 0: break # Use the first account we find account = account[0] desc = "SA_RINCON{0}_{1}".format( account.service_type, account.username) return desc # Nothing found. Default to the standard desc value. Is this the right # thing to do? desc = 'RINCON_AssociatedZPUDN' return desc
def test_initialise_account(): assert len(Account._all_accounts) == 0 accounts = Account.get_accounts() assert len(accounts) == 4 # TuneIn account is added automatically assert accounts['0'].service_type == '65031' # TuneIn assert accounts['1'].username == '12345678' assert accounts['1'].service_type == '2311' assert accounts['1'].deleted is False assert accounts['1'].nickname == 'mysonos' assert accounts.get('4') is None
def test_initialise_account(): assert len(Account._all_accounts) == 0 accounts = Account.get_accounts() assert len(accounts) == 4 # TuneIn account is added automatically assert accounts["0"].service_type == "65031" # TuneIn assert accounts["1"].username == "12345678" assert accounts["1"].service_type == "2311" assert accounts["1"].deleted is False assert accounts["1"].nickname == "mysonos" assert accounts.get("4") is None
def __init__(self, service_name, account=None): """Initialise the instance. Args: service_name (str): The name of the music service, as returned by `get_all_music_services_names()`, eg 'Spotify', or 'TuneIn' account (Account): The account to use to access this service. If none is specified, one will be chosen automatically if possible. Raises: MusicServiceException """ self.service_name = service_name # Look up the data for this service data = self.get_data_for_name(service_name) self.uri = data['Uri'] self.secure_uri = data['SecureUri'] self.capabilities = data['Capabilities'] self.version = data['Version'] self.container_type = data['ContainerType'] self.service_id = data['Id'] # Auth_type can be 'Anonymous', 'UserId, 'DeviceLink' self.auth_type = data['Auth'] self.presentation_map_uri = data.get('PresentationMapUri', None) self._search_prefix_map = None self.service_type = data['ServiceType'] if account is not None: self.account = account elif service_name == "TuneIn": # TuneIn is always present, but will have no account data, so we # need to handle it specially. Create a dummy account self.account = Account() else: # try to find an account for this service for acct in Account.get_accounts().values(): if acct.service_type == self.service_type: self.account = acct break else: raise MusicServiceException( "No account found for service: '%s'" % service_name) self.soap_client = MusicServiceSoapClient( endpoint=self.secure_uri, timeout=9, # The default is 60 music_service=self )
def __init__(self, service_name, account=None): """Initialise the instance. Args: service_name (str): The name of the music service, as returned by `get_all_music_services_names()`, eg 'Spotify', or 'TuneIn' account (Account): The account to use to access this service. If none is specified, one will be chosen automatically if possible. Raises: MusicServiceException """ self.service_name = service_name # Look up the data for this service data = self.get_data_for_name(service_name) self.uri = data['Uri'] self.secure_uri = data['SecureUri'] self.capabilities = data['Capabilities'] self.version = data['Version'] self.container_type = data['ContainerType'] self.service_id = data['Id'] # Auth_type can be 'Anonymous', 'UserId, 'DeviceLink' self.auth_type = data['Auth'] self.presentation_map_uri = data.get('PresentationMapUri', None) self._search_prefix_map = None self.service_type = data['ServiceType'] if account is not None: self.account = account elif service_name == "TuneIn": # TuneIn is always present, but will have no account data, so we # need to handle it specially. Create a dummy account self.account = Account() else: # try to find an account for this service for acct in Account.get_accounts().values(): if acct.service_type == self.service_type: self.account = acct break else: raise MusicServiceException( "No account found for service: '%s'" % service_name) self.soap_client = MusicServiceSoapClient( endpoint=self.secure_uri, timeout=9, # The default is 60 music_service=self)
def test_get_all_accounts(): a = Account.get_accounts() assert len(a) == 4 # Including TuneIn
def test_get_all_accounts(): a = Account.get_accounts() assert len(a) == 3