This repository has been archived by the owner on Oct 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
iCloud.py
252 lines (195 loc) · 8.85 KB
/
iCloud.py
1
2
3
4
5
6
7
8
9
10
11
12
13
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# iClould API Implementation
# Based on https://www.icloud.com requests
# Made by Vladimir Smirnov (vladimir@smirnov.im)
# http://www.mindcollapse.com/
# WARNING!
# This code could be used for educational purposes only.
# I.e. you should not use this code in any testing or production environments,
# otherwise you may violate Apple iCloud Terms and Condition and the Exodus 20:15 "Thou shalt not steal".
# The author is not responsible for any violation of this simple and clear rules.
# Depends on http://pypi.python.org/pypi/httplib2
import httplib2
import json, uuid, hashlib, sys
# Available both for Python 2.x and 3.x (tested on 2.7, 2.6 and 3.3)
if sys.version_info >= (3,0,0): from http.cookies import SimpleCookie
else: from Cookie import SimpleCookie
class iCloudException(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class iCloud():
# The list of the URLs for requests
urls = {
"version": "https://www.icloud.com/system/version.json",
"validate": "https://setup.icloud.com/setup/ws/1/validate?clientBuildNumber={0}&clientId={1}",
"authenticate": "https://setup.icloud.com/setup/ws/1/login?clientBuildNumber={0}&clientId={1}",
"logout_no_services": "https://setup.icloud.com/setup/ws/1/logout",
"get_contacts_list": "{0}/co/startup?clientBuildNumber={1}&clientId={2}&clientVersion=2.1&dsid={3}&id={4}&locale=en_US&order=last%2Cfirst",
"refresh_web_auth": "{0}/refreshWebAuth?clientBuildNumber={1}&clientId={2}&dsid={3}&id={4}",
"get_notes_list": "{0}/no/startup?clientBuildNumber={1}&clientId={2}&dsid={3}&id={4}",
"get_active_reminders": "{0}/rd/startup?clientVersion=4.0&dsid={1}&id={2}&lang=en-us&usertz=US%2FPacific",
"get_completed_reminders": "{0}/rd/completed?clientVersion=4.0&dsid={1}&id={2}&lang=en-us&usertz=US%2FPacific",
"fmi": None,
"fmi_init": "{0}/fmipservice/client/web/initClient?dsid={1}&id={2}",
"fmi_refresh": "{0}/fmipservice/client/web/refreshClient?dsid={1}&id={2}",
"get_calendar_events": "{0}/ca/events?clientBuildNumber={1}&clientID={2}&clientVersion=4.0&dsid={3}&endDate={4}&id={5}&lang=en-us&requestID=1&startDate={6}&usertz=US%2FPacific"
}
http = None
cookies = SimpleCookie()
clientId = None
clientBuildNumber = None
login = None
password = None
instance = None
checksum = None
webservices = {}
# The dict of iCloud account details
dsInfo = {}
# Logout from iCloud
def logout(self):
return self.__callapi(request="logout")
# Should be called every 10 minutes on idle for updating auth tokens
def refreshWebAuth(self):
return self.__callapi(request="refreshWebAuth")
# Returns the list of all contact from iCloud
def getContactsList(self):
return self.__callapi(request="getContactsList")
# Returns the list of all notes from iCloud
def getNotesList(self):
return self.__callapi(request="getNotesList")
# Returns the list of all active reminders from iCloud
def getActiveRemindersList(self):
return self.__callapi(request="getActiveRemindersList")
# Returns the list of all completed reminders from iCloud
def getCompletedRemindersList(self):
return self.__callapi(request="getCompletedRemindersList")
# Returns the location of all devices available from Find My iPhone
# Call with refres=True until you get updated data
def findMyIphone(self, refresh=False):
fmiData = None
fmiDict = {
"clientContext": {
"appName": "iCloud Find (Web)",
"appVersion": "2.0",
"timezone": "US/Pacific",
"inactiveTime": 1,
"apiVersion": "3.0"
}
}
if refresh: self.urls["fmi"] = self.urls["fmi_refresh"]
else: self.urls["fmi"] = self.urls["fmi_init"]
try: fmiData = self.__callapi(request="findMyIphone", params=fmiDict)
except (iCloudException): self.login()
fmiData = self.__callapi(request="findMyIphone", params=fmiDict)
return fmiData
# Returns the list of all calendar events between the dates
# Date format is YYYY-MM-DD
def getCalendarEvents(self, efrom, eto):
return self.__callapi(request="getCalendarEvents", params={"from":efrom, "to": eto})
# Private method for calling API methods
def __callapi(self, request, params={}):
callURL = None
callPayload = ""
method = "GET"
if request == "getContactsList":
callURL = self.urls["get_contacts_list"].format(self.webservices["contacts"]["url"], self.clientBuildNumber, self.clientId, self.dsInfo["dsid"], self.checksum)
elif request == "refreshWebAuth":
callURL = self.urls["refresh_web_auth"].format(self.webservices["push"]["url"], self.clientBuildNumber, self.clientId, self.dsInfo["dsid"], self.checksum)
elif request == "getNotesList":
callURL = self.urls["get_notes_list"].format(self.webservices["notes"]["url"], self.clientBuildNumber, self.clientId, self.dsInfo["dsid"], self.checksum)
elif request == "getActiveRemindersList":
callURL = self.urls["get_active_reminders"].format(self.webservices["reminders"]["url"], self.dsInfo["dsid"], self.checksum)
elif request == "getCompletedRemindersList":
callURL = self.urls["get_completed_reminders"].format(self.webservices["reminders"]["url"], self.dsInfo["dsid"], self.checksum)
elif request == "logout":
if "account" in self.webservices:
callURL = self.webservices["account"]["url"] + "/setup/ws/1/logout"
else:
callURL = self.urls["logout_no_services"]
elif request == "findMyIphone":
callPayload = json.dumps(params)
callURL = self.urls["fmi"].format(self.webservices["findme"]["url"], self.dsInfo["dsid"], self.checksum)
elif request == "getCalendarEvents":
callURL = self.urls["get_calendar_events"].format(self.webservices["calendar"]["url"], self.clientBuildNumber, self.clientId, self.dsInfo["dsid"], params["to"], self.checksum, params["from"])
else: raise iCloudException("wrong call request")
if callPayload != "": method = "POST"
resp, data = self.http.request(callURL, method, headers = {
"Origin": "https://www.icloud.com",
"Referer": "https://www.icloud.com",
"Cookie": self.__prepare_cookies()
}, body = callPayload)
if "set-cookie" in resp:
self.__update_cookies(resp["set-cookie"])
if resp.status != 200:
raise iCloudException(request + " did not suceed")
try: return json.loads(data.decode('utf-8'))
except (ValueError): return {}
# Login to the iCloud
# Use rememberMe=True if you need long term login
# You should consider storing self.cookies somewhere
# using serialization/deserialization if you need real long-time sessions
def authenticate(self, rememberMe=False):
authURL = self.urls["authenticate"].format(self.clientBuildNumber, self.clientId)
self.checksum = hashlib.sha1(self.login.encode('utf-8') + self.instance.encode('utf-8')).hexdigest().upper()
authDict = {
"apple_id": self.login,
"password": self.password,
"id": self.checksum,
"extended_login": rememberMe
}
resp, data = self.http.request(authURL, "POST", headers = {
"Origin": "https://www.icloud.com",
"Referer": "https://www.icloud.com",
"Cookie": self.__prepare_cookies()
}, body = json.dumps(authDict))
jdata = json.loads(data.decode('utf-8'))
if "instance" not in jdata:
raise iCloudException("wrong login data format")
if "set-cookie" in resp:
self.__update_cookies(resp["set-cookie"])
self.instance = jdata["instance"]
if resp.status != 200 or "error" in jdata:
raise iCloudException("authentication did not succeed")
if "webservices" not in jdata or "dsInfo" not in jdata:
raise iCloudException("wrong login data format")
self.webservices = jdata["webservices"]
self.dsInfo = jdata["dsInfo"]
# Private method for updating cookies dict from every request
def __update_cookies(self, respcookies):
tmpCookies = SimpleCookie()
tmpCookies.load(respcookies)
for cookie in tmpCookies:
self.cookies[cookie] = tmpCookies[cookie].value
# Private method for returning cookies in format that could be used in Cookie: header
def __prepare_cookies(self):
return self.cookies.output(sep=";", attrs=["value"], header="").strip()
# Private method for validating session cookies
def __validate(self):
validateURL = self.urls["validate"].format(self.clientBuildNumber, self.clientId)
resp, data = self.http.request(validateURL, "POST", headers = {
"Origin": "https://www.icloud.com",
"Referer": "https://www.icloud.com",
"Cookie": self.__prepare_cookies()
})
if "set-cookie" in resp:
self.__update_cookies(resp["set-cookie"])
jdata = json.loads(data.decode('utf-8'))
if "instance" not in jdata:
raise iCloudException("wrong validate data format")
self.instance = jdata["instance"]
# Get buildNuber and generate clientID
def __init__(self, login, password):
self.http = httplib2.Http()
self.clientBuildNumber = "1P24"
self.clientId = str(uuid.uuid1()).upper()
self.login = login
self.password = password
self.__validate()
"""
USAGE EXAMPLE
testCloud = iCloud(login="email@icloud.com", password="123456")
testCloud.authenticate()
print (testCloud.getContactsList())
testCloud.logout()
"""