Skip to content

Commit

Permalink
Linting and other fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarons committed Oct 9, 2023
1 parent f00a43c commit 32ca775
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 73 deletions.
153 changes: 80 additions & 73 deletions icloudpy/services/photos.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Photo service."""
import base64
import json
import logging
from datetime import datetime

# fmt: off
Expand All @@ -9,16 +10,18 @@
from pytz import UTC
from six import PY2

# fmt: on
from icloudpy.exceptions import ICloudPyServiceNotActivatedException

# fmt: on
LOGGER = logging.getLogger(__name__)


class PhotoLibrary(object):
class PhotoLibrary:
"""Represents a library in the user's photos.
This provides access to all the albums as well as the photos.
"""

SMART_FOLDERS = {
"All Photos": {
"obj_type": "CPLAssetByAddedDate",
Expand Down Expand Up @@ -136,24 +139,27 @@ def __init__(self, service, zone_id):

self._albums = None

url = ('%s/records/query?%s' %
(self.service._service_endpoint, urlencode(self.service.params)))
json_data = json.dumps({
"query": {"recordType":"CheckIndexingState"},
"zoneID": self.zone_id,
})
url = f"{self.service._service_endpoint}/records/query?{urlencode(self.service.params)}"
json_data = json.dumps(
{
"query": {"recordType": "CheckIndexingState"},
"zoneID": self.zone_id,
}
)

request = self.service.session.post(
url,
data=json_data,
headers={'Content-type': 'text/plain'}
url, data=json_data, headers={"Content-type": "text/plain"}
)
response = request.json()
indexing_state = response['records'][0]['fields']['state']['value']
if indexing_state != 'FINISHED':
raise PyiCloudServiceNotActivatedErrror(
('iCloud Photo Library not finished indexing. Please try '
'again in a few minutes'), None)
indexing_state = response["records"][0]["fields"]["state"]["value"]
if indexing_state != "FINISHED":
raise ICloudPyServiceNotActivatedException(
(
"iCloud Photo Library not finished indexing. Please try "
"again in a few minutes"
),
None,
)

@property
def albums(self):
Expand All @@ -165,75 +171,82 @@ def albums(self):

for folder in self._fetch_folders():
# FIXME: Handle subfolders
if folder['recordName'] in ('----Root-Folder----',
'----Project-Root-Folder----') or \
(folder['fields'].get('isDeleted') and
folder['fields']['isDeleted']['value']):
if folder["recordName"] in (
"----Root-Folder----",
"----Project-Root-Folder----",
) or (
folder["fields"].get("isDeleted")
and folder["fields"]["isDeleted"]["value"]
):
continue

folder_id = folder['recordName']
folder_obj_type = \
"CPLContainerRelationNotDeletedByAssetDate:%s" % folder_id
folder_id = folder["recordName"]
folder_obj_type = (
f"CPLContainerRelationNotDeletedByAssetDate:{folder_id}"
)
folder_name = base64.b64decode(
folder['fields']['albumNameEnc']['value']).decode('utf-8')
query_filter = [{
"fieldName": "parentId",
"comparator": "EQUALS",
"fieldValue": {
"type": "STRING",
"value": folder_id
folder["fields"]["albumNameEnc"]["value"]
).decode("utf-8")
query_filter = [
{
"fieldName": "parentId",
"comparator": "EQUALS",
"fieldValue": {"type": "STRING", "value": folder_id},
}
}]
]

album = PhotoAlbum(self.service, folder_name,
'CPLContainerRelationLiveByAssetDate',
folder_obj_type, 'ASCENDING', query_filter,
folder_id=folder_id, zone_id=self.zone_id)
album = PhotoAlbum(
self.service,
folder_name,
"CPLContainerRelationLiveByAssetDate",
folder_obj_type,
"ASCENDING",
query_filter,
folder_id=folder_id,
zone_id=self.zone_id,
)
self._albums[folder_name] = album

return self._albums

def _fetch_folders(self):
url = ('%s/records/query?%s' %
(self.service._service_endpoint, urlencode(self.service.params)))
json_data = json.dumps({
"query": {"recordType":"CPLAlbumByPositionLive"},
"zoneID": self.zone_id,
})
url = f"{self.service._service_endpoint}/records/query?{urlencode(self.service.params)}"
json_data = json.dumps(
{
"query": {"recordType": "CPLAlbumByPositionLive"},
"zoneID": self.zone_id,
}
)

request = self.service.session.post(
url,
data=json_data,
headers={'Content-type': 'text/plain'}
url, data=json_data, headers={"Content-type": "text/plain"}
)
response = request.json()

return response['records']
return response["records"]

@property
def all(self):
return self.albums['All Photos']
return self.albums["All Photos"]


class PhotosService(PhotoLibrary):
"""The 'Photos' iCloud service.
This also acts as a way to access the user's primary library.
"""

def __init__(self, service_root, session, params):
self.session = session
self.params = dict(params)
self._service_root = service_root
self._service_endpoint = \
('%s/database/1/com.apple.photos.cloud/production/private'
% self._service_root)
self._service_endpoint = (
f"{self._service_root}/database/1/com.apple.photos.cloud/production/private"
)

self._libraries = None

self.params.update({
'remapEnums': True,
'getCurrentSyncToken': True
})
self.params.update({"remapEnums": True, "getCurrentSyncToken": True})

# TODO: Does syncToken ever change?
# self.params.update({
Expand All @@ -243,35 +256,30 @@ def __init__(self, service_root, session, params):

self._photo_assets = {}

super(PhotosService, self).__init__(
service=self, zone_id={u'zoneName': u'PrimarySync'})
super().__init__(service=self, zone_id={"zoneName": "PrimarySync"})

@property
def libraries(self):
if not self._libraries:
try:
url = ('%s/changes/database' %
(self._service_endpoint, ))
url = f"{self._service_endpoint}/changes/database"
request = self.session.post(
url,
data='{}',
headers={'Content-type': 'text/plain'}
url, data="{}", headers={"Content-type": "text/plain"}
)
response = request.json()
zones = response['zones']
zones = response["zones"]
except Exception as e:
logger.error("library exception: %s" % str(e))
LOGGER.error(f"library exception: {str(e)}")

libraries = {}
for zone in zones:
if not zone.get('deleted'):
zone_name = zone['zoneID']['zoneName']
libraries[zone_name] = PhotoLibrary(
self, zone_id=zone['zoneID'])
# obj_type='CPLAssetByAssetDateWithoutHiddenOrDeleted',
# list_type="CPLAssetAndMasterByAssetDateWithoutHiddenOrDeleted",
# direction="ASCENDING", query_filter=None,
# zone_id=zone['zoneID'])
if not zone.get("deleted"):
zone_name = zone["zoneID"]["zoneName"]
libraries[zone_name] = PhotoLibrary(self, zone_id=zone["zoneID"])
# obj_type='CPLAssetByAssetDateWithoutHiddenOrDeleted',
# list_type="CPLAssetAndMasterByAssetDateWithoutHiddenOrDeleted",
# direction="ASCENDING", query_filter=None,
# zone_id=zone['zoneID'])

self._libraries = libraries

Expand Down Expand Up @@ -379,8 +387,7 @@ def _fetch_subalbums(self):
"zoneName":"{}"
}}
}}""".format(
self.folder_id,
self._zone_id["zoneName"]
self.folder_id, self._zone_id["zoneName"]
)
json_data = query
request = self.service.session.post(
Expand Down Expand Up @@ -426,7 +433,7 @@ def subalbums(self):
direction="ASCENDING",
query_filter=query_filter,
folder_id=folder_id,
zone_id=self._zone_id
zone_id=self._zone_id,
)
self._subalbums[folder_name] = album
return self._subalbums
Expand Down
3 changes: 3 additions & 0 deletions pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ disable=abstract-method,
wrong-import-order,
xrange-builtin,
zip-builtin-not-iterating,
protected-access,
logging-not-lazy,
logging-fstring-interpolation


[REPORTS]
Expand Down

0 comments on commit 32ca775

Please sign in to comment.