Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions codecarbon/external/geography.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"""

import re
import urllib.parse
from dataclasses import dataclass
from typing import Callable, Dict, Optional

import pycountry
import requests

from codecarbon.core.cloud import get_env_cloud_details
Expand Down Expand Up @@ -93,10 +93,14 @@ def from_geo_js(cls, url: str) -> "GeoMetadata":
try:
response: Dict = requests.get(url, timeout=0.5).json()

region = response.get("region", "").lower()
if not region:
raise ValueError("Region is empty")

return cls(
country_iso_code=response["country_code3"].upper(),
country_name=response["country"],
region=response.get("region", "").lower(),
region=region,
latitude=float(response.get("latitude")),
longitude=float(response.get("longitude")),
country_2letter_iso_code=response.get("country_code"),
Expand All @@ -107,32 +111,36 @@ def from_geo_js(cls, url: str) -> "GeoMetadata":
f"Unable to access geographical location through primary API. Will resort to using the backup API - Exception : {e} - url={url}"
)

geo_url_backup = "https://ip-api.com/json/"
geo_url_backup = "https://ipinfo.io/json"

try:
geo_response: Dict = requests.get(geo_url_backup, timeout=0.5).json()
country_name = geo_response["country"]

# The previous request does not return the three-letter country code
country_code_3_url = f"https://api.first.org/data/v1/countries?q={urllib.parse.quote_plus(country_name)}&scope=iso"
country_code_response: Dict = requests.get(
country_code_3_url, timeout=0.5
).json()
# extract latitude and longitude from loc (e.g., "loc": "37.4056,-122.0775")
loc = geo_response.get("loc", "").split(",")
latitude = float(loc[0]) if len(loc) == 2 else 0.0
longitude = float(loc[1]) if len(loc) == 2 else 0.0

# Retrieve the 3-letter ISO code using pycountry
country_2letter_iso_code = geo_response.get("country")
country = pycountry.countries.get(alpha_2=country_2letter_iso_code)

# Some countries might not be found or mapped perfectly
country_iso_code = country.alpha_3 if country else ""
country_name = country.name if country else ""

return cls(
country_iso_code=next(
iter(country_code_response["data"].keys())
).upper(),
country_iso_code=country_iso_code.upper(),
country_name=country_name,
region=geo_response.get("regionName", "").lower(),
latitude=float(geo_response.get("lat")),
longitude=float(geo_response.get("lon")),
country_2letter_iso_code=geo_response.get("countryCode"),
region=geo_response.get("region", "").lower(),
latitude=latitude,
longitude=longitude,
country_2letter_iso_code=country_2letter_iso_code,
)
except Exception as e:
# If both API calls fail, default to Canada
logger.warning(
f"Unable to access geographical location. Using 'Canada' as the default value - Exception : {e} - url={url}"
f"Unable to access geographical location through fallback API. Using 'Canada' as the default value - Exception : {e} - url={geo_url_backup}"
)

return cls(
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ dependencies = [
"questionary",
"rich",
"typer",
"pycountry",
]

[tool.setuptools.dynamic]
Expand Down
20 changes: 16 additions & 4 deletions tests/test_geography.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
CLOUD_METADATA_AZURE,
CLOUD_METADATA_GCP,
CLOUD_METADATA_GCP_EMPTY,
COUNTRY_METADATA_USA,
GEO_METADATA_CANADA,
GEO_METADATA_USA,
GEO_METADATA_USA_BACKUP,
Expand Down Expand Up @@ -91,14 +90,27 @@ def test_geo_metadata_USA_backup(self):
)
responses.add(
responses.GET,
"https://ip-api.com/json/",
"https://ipinfo.io/json",
json=GEO_METADATA_USA_BACKUP,
status=200,
)
geo = GeoMetadata.from_geo_js(self.geo_js_url)
self.assertEqual("USA", geo.country_iso_code)
self.assertEqual("United States", geo.country_name)
self.assertEqual("illinois", geo.region)

@responses.activate
def test_geo_metadata_empty_region_fallback(self):
empty_region_response = GEO_METADATA_USA.copy()
empty_region_response["region"] = ""

responses.add(
responses.GET, self.geo_js_url, json=empty_region_response, status=200
)
responses.add(
responses.GET,
"https://api.first.org/data/v1/countries?q=United%20States&scope=iso",
json=COUNTRY_METADATA_USA,
"https://ipinfo.io/json",
json=GEO_METADATA_USA_BACKUP,
status=200,
)
geo = GeoMetadata.from_geo_js(self.geo_js_url)
Expand Down
36 changes: 6 additions & 30 deletions tests/testdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,14 @@
}

GEO_METADATA_USA_BACKUP = {
"organization_name": "foobar",
"regionName": "Illinois",
"accuracy": 1,
"asn": 0,
"organization": "foobar",
"timezone": "America/Chicago",
"lon": "88",
"area_code": "0",
"ip": "foobar",
"city": "Chicago",
"country": "United States",
"countryCode": "US",
"lat": "0",
}

COUNTRY_METADATA_USA = {
"status": "OK",
"status-code": 200,
"version": "1.0",
"access": "public",
"data": {
"USA": {
"id": "USA",
"country": "United States of America (the)",
"region": "North America",
},
"UMI": {
"id": "UMI",
"country": "United States Minor Outlying Islands (the)",
"region": "Oceania",
},
},
"region": "Illinois",
"country": "US",
"loc": "0,88",
"org": "foobar",
"postal": "60601",
"timezone": "America/Chicago",
}

GEO_METADATA_CANADA = {
Expand Down
13 changes: 12 additions & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading