From adb14d1468b2f969805e614db5a6a474760ca291 Mon Sep 17 00:00:00 2001 From: serii Date: Tue, 17 May 2016 23:21:28 +0300 Subject: [PATCH 1/2] Add ICloud Account service --- pyicloud/base.py | 12 ++++++++++- pyicloud/services/__init__.py | 1 + pyicloud/services/account.py | 39 +++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 pyicloud/services/account.py 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..ddf8392 --- /dev/null +++ b/pyicloud/services/account.py @@ -0,0 +1,39 @@ +import sys + + +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 + + +class AccountDevice(object): + def __init__(self, device_info): + self.serialNumber = device_info['serialNumber'] + self.osVersion = device_info['osVersion'] + self.modelLargePhotoURL2x = device_info['modelLargePhotoURL2x'] + self.modelLargePhotoURL1x = device_info['modelLargePhotoURL1x'] + self.name = device_info['name'] + self.imei = device_info['imei'] + self.model = device_info['model'] + self.udid = device_info['udid'] + self.modelSmallPhotoURL2x = device_info['modelSmallPhotoURL2x'] + self.modelSmallPhotoURL1x = device_info['modelSmallPhotoURL1x'] + self.modelDisplayName = device_info['modelDisplayName'] From 2b004a2a55a4a9092f015f34d4c1eb95d1e23ef7 Mon Sep 17 00:00:00 2001 From: Adam Coddington Date: Mon, 20 Jun 2016 20:35:46 -0700 Subject: [PATCH 2/2] Minor alterations to AccountService. --- pyicloud/services/account.py | 40 +++++++++++++++++++++++++----------- pyicloud/utils.py | 8 ++++++++ 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/pyicloud/services/account.py b/pyicloud/services/account.py index ddf8392..a309ae0 100644 --- a/pyicloud/services/account.py +++ b/pyicloud/services/account.py @@ -1,5 +1,9 @@ import sys +import six + +from pyicloud.utils import underscore_to_camelcase + class AccountService(object): def __init__(self, service_root, session, params): @@ -24,16 +28,28 @@ class AccountService(object): return self._devices -class AccountDevice(object): +@six.python_2_unicode_compatible +class AccountDevice(dict): def __init__(self, device_info): - self.serialNumber = device_info['serialNumber'] - self.osVersion = device_info['osVersion'] - self.modelLargePhotoURL2x = device_info['modelLargePhotoURL2x'] - self.modelLargePhotoURL1x = device_info['modelLargePhotoURL1x'] - self.name = device_info['name'] - self.imei = device_info['imei'] - self.model = device_info['model'] - self.udid = device_info['udid'] - self.modelSmallPhotoURL2x = device_info['modelSmallPhotoURL2x'] - self.modelSmallPhotoURL1x = device_info['modelSmallPhotoURL1x'] - self.modelDisplayName = device_info['modelDisplayName'] + 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)