Created
January 12, 2026 07:11
-
-
Save jamsea/a29468ec4772decacf8e1961f402c667 to your computer and use it in GitHub Desktop.
Fetch IP addresses from Daily.co IP info API by region.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env -S uv run | |
| """Fetch IP addresses from Daily.co IP info API by region. | |
| This script fetches host information from the Daily.co IP info API and filters | |
| for hosts in the 'iad' region. It tracks changes by storing the lastUpdated | |
| timestamp and prints "NEW LIST" when updates are detected. | |
| Usage: | |
| ./regions.py | |
| Cron job example (runs daily at midnight): | |
| # Edit crontab with: crontab -e | |
| # Add the following line: | |
| 0 0 * * * /path/to/regions.py >> /path/to/regions.log 2>&1 | |
| """ | |
| # /// script | |
| # dependencies = ["requests"] | |
| # /// | |
| import json | |
| from pathlib import Path | |
| import requests | |
| LAST_UPDATED_FILE = Path(__file__).parent / ".last_updated" | |
| def get_hosts(): | |
| """Fetch host objects from Daily.co API.""" | |
| url = "https://ip-info.daily.co/ips/ip-info.json" | |
| response = requests.get(url) | |
| response.raise_for_status() | |
| data = response.json() | |
| last_updated = data.get("lastUpdated") | |
| all_hosts = [] | |
| # Navigate through the JSON structure | |
| ip_address_info = data.get("ipAddressInfo", {}) | |
| for category in ip_address_info.values(): | |
| hosts = category.get("hosts", []) | |
| for host in hosts: | |
| # if host.get("region") == "iad": | |
| # iad_hosts.append(host) | |
| all_hosts.append(host) | |
| return last_updated, all_hosts | |
| def check_and_update_last_updated(last_updated: str) -> bool: | |
| """Check if the list has been updated since last run. | |
| Returns True if this is a new list, False otherwise. | |
| """ | |
| is_new = False | |
| if LAST_UPDATED_FILE.exists(): | |
| previous_updated = LAST_UPDATED_FILE.read_text().strip() | |
| if last_updated > previous_updated: | |
| is_new = True | |
| else: | |
| is_new = True | |
| # Save the current lastUpdated value | |
| LAST_UPDATED_FILE.write_text(last_updated) | |
| return is_new | |
| if __name__ == "__main__": | |
| last_updated, hosts = get_hosts() | |
| is_new_list = check_and_update_last_updated(last_updated) | |
| if is_new_list: | |
| print("NEW LIST") | |
| # Logic to potentially update firewall rules would go here | |
| print(f"Last Updated: {last_updated}") | |
| # print(f"Found {len(hosts)} hosts in region 'iad':") | |
| # for host in hosts: | |
| # print(json.dumps(host, indent=2)) | |
| # print("\n--- IP Addresses Only ---") | |
| # for host in hosts: | |
| # print(host.get("ipAddress")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment