diff --git a/pyicloud/base.py b/pyicloud/base.py index fcf2982..bed32d6 100644 --- a/pyicloud/base.py +++ b/pyicloud/base.py @@ -21,7 +21,8 @@ from pyicloud.services import ( UbiquityService, ContactsService, RemindersService, - PhotosService + PhotosService, + AccountService ) from pyicloud.utils import get_password_from_keyring @@ -269,6 +270,15 @@ class PyiCloudService(object): self.params ) + @property + def account(self): + service_root = self.webservices['account']['url'] + return AccountService( + service_root, + self.session, + self.params + ) + @property def iphone(self): return self.devices[0] diff --git a/pyicloud/services/__init__.py b/pyicloud/services/__init__.py index 9a72d59..cf51661 100644 --- a/pyicloud/services/__init__.py +++ b/pyicloud/services/__init__.py @@ -4,3 +4,4 @@ from pyicloud.services.ubiquity import UbiquityService from pyicloud.services.contacts import ContactsService from pyicloud.services.reminders import RemindersService from pyicloud.services.photos import PhotosService +from pyicloud.services.account import AccountService diff --git a/pyicloud/services/account.py b/pyicloud/services/account.py new file mode 100644 index 0000000..a309ae0 --- /dev/null +++ b/pyicloud/services/account.py @@ -0,0 +1,55 @@ +import sys + +import six + +from pyicloud.utils import underscore_to_camelcase + + +class AccountService(object): + def __init__(self, service_root, session, params): + self.session = session + self.params = params + self._service_root = service_root + self._devices = [] + + self._acc_endpoint = '%s/setup/web/device' % self._service_root + self._account_devices_url = '%s/getDevices' % self._acc_endpoint + + req = self.session.get(self._account_devices_url, params=self.params) + self.response = req.json() + + for device_info in self.response['devices']: + # device_id = device_info['udid'] + # self._devices[device_id] = AccountDevice(device_info) + self._devices.append(AccountDevice(device_info)) + + @property + def devices(self): + return self._devices + + +@six.python_2_unicode_compatible +class AccountDevice(dict): + def __init__(self, device_info): + super(AccountDevice, self).__init__(device_info) + + def __getattr__(self, name): + try: + return self[underscore_to_camelcase(name)] + except KeyError: + raise AttributeError(name) + + def __str__(self): + return u"{display_name}: {name}".format( + display_name=self.model_display_name, + name=self.name, + ) + + def __repr__(self): + return '<{display}>'.format( + display=( + six.text_type(self) + if sys.version_info[0] >= 3 else + six.text_type(self).encode('utf8', 'replace') + ) + ) diff --git a/pyicloud/utils.py b/pyicloud/utils.py index 8981d30..378b52c 100644 --- a/pyicloud/utils.py +++ b/pyicloud/utils.py @@ -62,3 +62,11 @@ def delete_password_in_keyring(username): KEYRING_SYSTEM, username, ) + + +def underscore_to_camelcase(word, initial_capital=False): + words = [x.capitalize() or '_' for x in word.split('_')] + if not initial_capital: + words[0] = words[0].lower() + + return ''.join(words)