[#83] Adding support for icloud Account Service. Thanks @serii833!

This commit is contained in:
Adam Coddington 2016-06-20 20:38:30 -07:00
commit 2d4ca14710
4 changed files with 75 additions and 1 deletions

View file

@ -21,7 +21,8 @@ from pyicloud.services import (
UbiquityService, UbiquityService,
ContactsService, ContactsService,
RemindersService, RemindersService,
PhotosService PhotosService,
AccountService
) )
from pyicloud.utils import get_password_from_keyring from pyicloud.utils import get_password_from_keyring
@ -269,6 +270,15 @@ class PyiCloudService(object):
self.params self.params
) )
@property
def account(self):
service_root = self.webservices['account']['url']
return AccountService(
service_root,
self.session,
self.params
)
@property @property
def iphone(self): def iphone(self):
return self.devices[0] return self.devices[0]

View file

@ -4,3 +4,4 @@ from pyicloud.services.ubiquity import UbiquityService
from pyicloud.services.contacts import ContactsService from pyicloud.services.contacts import ContactsService
from pyicloud.services.reminders import RemindersService from pyicloud.services.reminders import RemindersService
from pyicloud.services.photos import PhotosService from pyicloud.services.photos import PhotosService
from pyicloud.services.account import AccountService

View file

@ -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')
)
)

View file

@ -62,3 +62,11 @@ def delete_password_in_keyring(username):
KEYRING_SYSTEM, KEYRING_SYSTEM,
username, 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)