Skip to content

Account

Bases: BaseService

The 'Account' iCloud service.

Source code in pyicloud/services/account.py
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
class AccountService(BaseService):
    """The 'Account' iCloud service."""

    def __init__(
        self,
        service_root: str,
        session: PyiCloudSession,
        china_mainland: bool,
        params: dict[str, Any],
    ) -> None:
        super().__init__(service_root, session, params)

        self._devices: list[AccountDevice] = []
        self._family: list[FamilyMember] = []
        self._storage: AccountStorage | None = None

        self._acc_endpoint: str = f"{self.service_root}/setup/web"
        self._acc_devices_url: str = f"{self._acc_endpoint}/device/getDevices"
        self._acc_family_details_url: str = (
            f"{self._acc_endpoint}/family/getFamilyDetails"
        )
        self._acc_family_member_photo_url: str = (
            f"{self._acc_endpoint}/family/getMemberPhoto"
        )
        self._acc_storage_url: str = f"{self.service_root}/setup/ws/1/storageUsageInfo"

        self._gateway: str = (
            f"https://gatewayws.icloud.com{'' if not china_mainland else '.cn'}"
        )
        self._gateway_root: str = f"{self._gateway}/acsegateway"
        dsid: str = self.params.get("dsid", DEFAULT_DSID)
        self._gateway_pricing_url: str = (
            f"{self._gateway_root}/v1/accounts/{dsid}/plans/icloud/pricing"
        )
        self._gateway_summary_plan_url: str = (
            f"{self._gateway_root}/v3/accounts/{dsid}/subscriptions"
            "/features/cloud.storage/plan-summary"
        )

    @property
    def devices(self) -> list["AccountDevice"]:
        """Returns current paired devices."""
        if not self._devices:
            req: Response = self.session.get(self._acc_devices_url, params=self.params)
            response = req.json()

            for device_info in response["devices"]:
                self._devices.append(AccountDevice(device_info))

        return self._devices

    @property
    def family(self) -> list["FamilyMember"]:
        """Returns family members."""
        if not self._family:
            req: Response = self.session.get(
                self._acc_family_details_url, params=self.params
            )
            response = req.json()

            for member_info in response.get("familyMembers", []):
                self._family.append(
                    FamilyMember(
                        member_info,
                        self.session,
                        self.params,
                        self._acc_family_member_photo_url,
                    )
                )

        return self._family

    @property
    def storage(self) -> "AccountStorage":
        """Returns storage infos."""
        if self._storage is None:
            return self.refresh_storage()
        return self._storage

    def refresh_storage(self) -> "AccountStorage":
        """Fetch current storage information and replace the cached snapshot.

        A failed request or response parsing leaves the previous snapshot intact.
        """
        response = self.session.post(self._acc_storage_url, params=self.params)
        storage = AccountStorage(response.json())
        # Usage properties are lazy: require their essential fields before caching.
        usage_bytes = (
            storage.usage.used_storage_in_bytes,
            storage.usage.total_storage_in_bytes,
        )
        if any(
            not isinstance(value, int) or isinstance(value, bool) or value < 0
            for value in usage_bytes
        ):
            raise ValueError("Invalid account storage usage")
        self._storage = storage
        return storage

    @property
    def summary_plan(self) -> Any:
        """Returns your subscription plan."""
        req: Response = self.session.get(
            self._gateway_summary_plan_url, params=self.params
        )
        response = req.json()
        return response

    def __str__(self) -> str:
        return (
            f"{{devices: {len(self.devices)}, family: {len(self.family)}, "
            f"storage: {self.storage.usage.available_storage_in_bytes} bytes free}}"
        )

    def __repr__(self) -> str:
        return f"<{type(self).__name__}: {self}>"

devices property

Returns current paired devices.

family property

Returns family members.

storage property

Returns storage infos.

summary_plan property

Returns your subscription plan.

refresh_storage()

Fetch current storage information and replace the cached snapshot.

A failed request or response parsing leaves the previous snapshot intact.

Source code in pyicloud/services/account.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
def refresh_storage(self) -> "AccountStorage":
    """Fetch current storage information and replace the cached snapshot.

    A failed request or response parsing leaves the previous snapshot intact.
    """
    response = self.session.post(self._acc_storage_url, params=self.params)
    storage = AccountStorage(response.json())
    # Usage properties are lazy: require their essential fields before caching.
    usage_bytes = (
        storage.usage.used_storage_in_bytes,
        storage.usage.total_storage_in_bytes,
    )
    if any(
        not isinstance(value, int) or isinstance(value, bool) or value < 0
        for value in usage_bytes
    ):
        raise ValueError("Invalid account storage usage")
    self._storage = storage
    return storage