Skip to content

Instantly share code, notes, and snippets.

@rayhanadev
Created June 2, 2025 18:45
Show Gist options
  • Save rayhanadev/24dfa8b431943a29ce9ed6fd029b6d57 to your computer and use it in GitHub Desktop.
Save rayhanadev/24dfa8b431943a29ce9ed6fd029b6d57 to your computer and use it in GitHub Desktop.
import sys
from pyicloud import PyiCloudService
from geopy.geocoders import Nominatim
api = PyiCloudService("<username>@icloud.com", "<password>")
geolocator = Nominatim(user_agent="silly_phone_tracker")
if api.requires_2fa:
print("Two-factor authentication required.")
code = input("Enter the code you received of one of your approved devices: ")
result = api.validate_2fa_code(code)
print("Code validation result: %s" % result)
if not result:
print("Failed to verify security code")
sys.exit(1)
if not api.is_trusted_session:
print("Session is not trusted. Requesting trust...")
result = api.trust_session()
print("Session trust result %s" % result)
if not result:
print(
"Failed to request trust. You will likely be prompted for the code again in the coming weeks"
)
elif api.requires_2sa:
import click
print("Two-step authentication required. Your trusted devices are:")
devices = api.trusted_devices
for i, device in enumerate(devices):
print(
" %s: %s"
% (i, device.get("deviceName", "SMS to %s" % device.get("phoneNumber")))
)
device = click.prompt("Which device would you like to use?", default=0)
device = devices[device]
if not api.send_verification_code(device):
print("Failed to send verification code")
sys.exit(1)
code = click.prompt("Please enter validation code")
if not api.validate_verification_code(device, code):
print("Failed to verify verification code")
sys.exit(1)
iphone = None
for device in api.devices:
if device.get("name") == "Ray’s iPhone 14 Pro":
iphone = device
break
if iphone is None:
print("Could not find iPhone in Find My data")
sys.exit(1)
print("iPhone found: %s" % iphone.get("name"))
location = iphone.location()
if location is None:
print("Could not get iPhone location")
sys.exit(1)
latitude = location.get("latitude")
longitude = location.get("longitude")
print("iPhone Location: %s, %s" % (latitude, longitude))
location = geolocator.reverse("%s, %s" % (latitude, longitude))
if location is None:
print("Could not get location from geocoder")
sys.exit(1)
print(location.raw)
print(
"iPhone Location Address: %s, %s"
% (location.raw.address.city, location.raw.address.state)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment