Advertisement
Guest User

find_my_iphone.py

a guest
Feb 12th, 2018
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.08 KB | None | 0 0
  1. import datetime
  2. import time
  3. import base64
  4. import urllib2
  5. import json
  6. import sys
  7.  
  8.  
  9. def get_location(latitude, longitude):
  10.     url = "https://maps.googleapis.com/maps/api/geocode/json?latlng={0},{1}".format(latitude, longitude)
  11.     headers = {"Content-Type": "application/json"}
  12.     request = urllib2.Request(url, None, headers)
  13.  
  14.     try:
  15.         response = urllib2.urlopen(request)
  16.     except urllib2.HTTPError as ex:
  17.         if ex.code != 200:
  18.             return "HTTP Error: {0}".format(ex.code)
  19.         else:
  20.             print ex
  21.             raise urllib2.HTTPError
  22.  
  23.     formatted_address = json.loads(response.read())["results"][0]["formatted_address"]
  24.     return formatted_address.encode("ascii", "ignore")
  25.  
  26.  
  27. def print_devices(username, password):
  28.     i = 0
  29.  
  30.     try:
  31.         # FMIP token specified, change auth type
  32.         int(username)
  33.         auth_type = "Forever"
  34.     except ValueError:
  35.         # Normal Apple ID
  36.         auth_type = "UserIDGuest"
  37.  
  38.     while True:
  39.         i += 1
  40.  
  41.         url = "https://fmipmobile.icloud.com/fmipservice/device/{0}/initClient".format(username)
  42.         headers = {
  43.             "X-Apple-Realm-Support": "1.0",
  44.             "Authorization": "Basic {0}".format(base64.b64encode("{0}:{1}".format(username, password))),
  45.             "X-Apple-Find-API-Ver": "3.0",
  46.             "X-Apple-AuthScheme": "{0}".format(auth_type),
  47.             "User-Agent": "FindMyiPhone/500 CFNetwork/758.4.3 Darwin/15.5.0",
  48.         }
  49.  
  50.         request = urllib2.Request(url, None, headers)
  51.         request.get_method = lambda: "POST"
  52.  
  53.         try:
  54.             response = urllib2.urlopen(request)
  55.             z = json.loads(response.read())
  56.         except urllib2.HTTPError as e:
  57.             if e.code == 401:
  58.                 print "Error 401: Invalid credentials."
  59.             if e.code == 403:
  60.                 pass
  61.             raise e
  62.  
  63.         if i == 2:  # Break out of while loop
  64.             break
  65.  
  66.         time.sleep(5)  # Wait for iCloud to get results then go back to the beginning of the loop
  67.  
  68.     i = 0
  69.     for y in z["content"]:
  70.             model = str(y["deviceDisplayName"])
  71.             name = str(y["name"].encode("ascii", "ignore"))
  72.  
  73.             try:
  74.                 latitude = y["location"]["latitude"]
  75.                 longitude = y["location"]["longitude"]
  76.  
  77.                 battery_percent = str(float(y["batteryLevel"]) * 100).split(".")[0] + "%"
  78.                 battery_status = str(y["batteryStatus"]).replace("NotCharging", "Not charging")
  79.                 battery = "{0} ({1})".format(battery_percent, battery_status)
  80.  
  81.                 time_stamp = y["location"]["timeStamp"] / 1000
  82.                 time_delta = time.time() - time_stamp  # Time difference in seconds
  83.                 minutes, seconds = divmod(time_delta, 60)  # Great function, saves annoying maths
  84.                 hours, minutes = divmod(minutes, 60)
  85.                 time_stamp = datetime.datetime.fromtimestamp(time_stamp).strftime("%A, %B %d at %I:%M:%S")
  86.  
  87.                 if hours > 0:
  88.                     time_stamp = "%s (%sh %sm %ss ago)" % (time_stamp, str(hours).split(".")[0], str(minutes).split(".")[0], str(seconds).split(".")[0])
  89.                 else:
  90.                     time_stamp = "%s (%sm %ss ago)" % (
  91.                     time_stamp, str(minutes).split(".")[0], str(seconds).split(".")[0])
  92.  
  93.                 print "Device: {0}".format(name)
  94.                 print "Model: {0}".format(model)
  95.                 print "Coordinates: {0}, {1}".format(latitude, longitude)
  96.                 print "Street Address: {0}".format(get_location(latitude, longitude))
  97.                 print "Battery: {0}".format(battery)
  98.                 print "Last Seen: {0}".format(time_stamp)
  99.             except TypeError:  # No latitude / longitude
  100.                 print "Device: {0}".format(name)
  101.                 print "Model: {0}".format(model)
  102.  
  103.             i += 1
  104.             if i != len(z["content"]):
  105.                 print "-" * 15
  106.  
  107.  
  108. if __name__ == '__main__':
  109.     username = sys.argv[1]  # Username
  110.     password = sys.argv[2]  # Password
  111.  
  112.     print_devices(username, password)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement