Created
June 15, 2023 21:01
-
-
Save jasonmadigan/c05c3edb0de50f99e26c313cc01a90e9 to your computer and use it in GitHub Desktop.
This file contains 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
import requests | |
from geopy.distance import geodesic | |
from geopy.geocoders import Nominatim | |
def get_outages(): | |
url = "https://api.esb.ie/esbn/powercheck/v1.0/outages" | |
headers = {"api-subscription-key": "f713e48af3a746bbb1b110ab69113960"} | |
response = requests.get(url, headers=headers) | |
# print(response.json()) | |
if response.status_code == 200: | |
return response.json()["outageMessage"] | |
else: | |
print(f"Error: {response.status_code} - {response.text}") | |
def get_outage_details(outage_id): | |
url = f"https://api.esb.ie/esbn/powercheck/v1.0/outages/{outage_id}" | |
headers = {"api-subscription-key": "f713e48af3a746bbb1b110ab69113960"} | |
response = requests.get(url, headers=headers) | |
if response.status_code == 200: | |
print(response.json()) | |
return response.json() | |
else: | |
print(f"Error: {response.status_code} - {response.text}") | |
def find_outages_nearby(latitude, longitude, distance=20, outage_type=None): | |
outages = get_outages() | |
nearby_outages = [] | |
for outage in outages: | |
outage_latitude = outage["p"]["c"].split(",")[0] | |
outage_longitude = outage["p"]["c"].split(",")[1] | |
outage_location = (float(outage_latitude), float(outage_longitude)) | |
user_location = (latitude, longitude) | |
outage_distance = geodesic(user_location, outage_location).kilometers | |
if outage_distance <= distance and (outage_type is None or outage["t"] == outage_type): | |
outage["distance_km"] = outage_distance | |
nearby_outages.append(outage) | |
return nearby_outages | |
def find_planned_outages_nearby(latitude, longitude, distance=20): | |
return find_outages_nearby(latitude, longitude, distance, outage_type="Planned") | |
def find_restored_outages_nearby(latitude, longitude, distance=20): | |
return find_outages_nearby(latitude, longitude, distance, outage_type="Restored") | |
# Example usage | |
my_latitude = 52.421633 | |
my_longitude = -7.017033 | |
outages_nearby = find_planned_outages_nearby(my_latitude, my_longitude) | |
geolocator = Nominatim(user_agent="my_geocoder") | |
for outage in outages_nearby: | |
outage_id = outage['i'] | |
outage_type = outage['t'] | |
outage_latitude = outage['p']['c'].split(',')[0] | |
outage_longitude = outage['p']['c'].split(',')[1] | |
outage_distance = outage['distance_km'] | |
outage_location = geolocator.reverse(f"{outage_latitude}, {outage_longitude}") | |
outage_address = outage_location.address if outage_location else "Address not found" | |
print(f"Outage ID: {outage_id}") | |
print(f"Outage Type: {outage_type}") | |
print(f"Latitude: {outage_latitude}") | |
print(f"Longitude: {outage_longitude}") | |
print(f"Address: {outage_address}") | |
print(f"Distance to Outage: {outage_distance} km") | |
print("-----------------------------------") | |
# Get additional details for the specific outage ID | |
outage_details = get_outage_details(outage_id) | |
if outage_details: | |
# Process and display the additional details | |
print("Additional Outage Details:") | |
print(f"Outage ID: {outage_details['outageId']}") | |
print(f"Outage Type: {outage_details['outageType']}") | |
print(f"Location: {outage_details['location']}") | |
print(f"Planner Group: {outage_details['plannerGroup']}") | |
print(f"Number of Customers Affected: {outage_details['numCustAffected']}") | |
print(f"Start Time: {outage_details['startTime']}") | |
print(f"Estimated Restore Time: {outage_details['estRestoreTime']}") | |
print(f"Status Message: {outage_details['statusMessage']}") | |
print(f"Restore Time: {outage_details['restoreTime']}") | |
print("-----------------------------------") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment