Add Python service foundation and gitignore
This commit is contained in:
commit
da2fb7802b
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
coordinates_cache.json
|
||||
26
coordinates_cache.json
Normal file
26
coordinates_cache.json
Normal file
@ -0,0 +1,26 @@
|
||||
{
|
||||
"Tel Aviv": {
|
||||
"name": "Tel Aviv",
|
||||
"latitude": 32.08088,
|
||||
"longitude": 34.78057,
|
||||
"country": "Israel"
|
||||
},
|
||||
"Beersheba": {
|
||||
"name": "Beersheba",
|
||||
"latitude": 31.25181,
|
||||
"longitude": 34.7913,
|
||||
"country": "Israel"
|
||||
},
|
||||
"Jerusalem": {
|
||||
"name": "Jerusalem",
|
||||
"latitude": 31.76904,
|
||||
"longitude": 35.21633,
|
||||
"country": "Israel"
|
||||
},
|
||||
"Szeged": {
|
||||
"name": "Szeged",
|
||||
"latitude": 46.253,
|
||||
"longitude": 20.14824,
|
||||
"country": "Hungary"
|
||||
}
|
||||
}
|
||||
66
main.py
Normal file
66
main.py
Normal file
@ -0,0 +1,66 @@
|
||||
import requests
|
||||
import json
|
||||
import os
|
||||
|
||||
API_URL = "https://geocoding-api.open-meteo.com/v1/search"
|
||||
CITIES = ["Tel Aviv", "Beersheba", "Jerusalem", "Szeged"]
|
||||
CACHE_FILE = "coordinates_cache.json"
|
||||
|
||||
|
||||
def fetch_coordinates(city):
|
||||
params = {
|
||||
"name": city,
|
||||
"count": 1
|
||||
}
|
||||
response = requests.get(API_URL, params=params)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
|
||||
if "results" not in data:
|
||||
raise ValueError(f"No results found for {city}")
|
||||
|
||||
result = data["results"][0]
|
||||
|
||||
return {
|
||||
"name": result["name"],
|
||||
"latitude": result["latitude"],
|
||||
"longitude": result["longitude"],
|
||||
"country": result["country"]
|
||||
}
|
||||
|
||||
|
||||
def load_cache():
|
||||
if os.path.exists(CACHE_FILE):
|
||||
with open(CACHE_FILE, "r") as f:
|
||||
return json.load(f)
|
||||
return None
|
||||
|
||||
|
||||
def save_cache(data):
|
||||
with open(CACHE_FILE, "w") as f:
|
||||
json.dump(data, f, indent=4)
|
||||
|
||||
|
||||
def main():
|
||||
# Try loading cached data first
|
||||
cached_data = load_cache()
|
||||
if cached_data:
|
||||
print("Loaded from cache:")
|
||||
print(json.dumps(cached_data, indent=4))
|
||||
return
|
||||
|
||||
# If no cache, fetch from API
|
||||
print("Fetching from Open-Meteo API...")
|
||||
results = {}
|
||||
|
||||
for city in CITIES:
|
||||
results[city] = fetch_coordinates(city)
|
||||
|
||||
save_cache(results)
|
||||
|
||||
print("Saved to cache:")
|
||||
print(json.dumps(results, indent=4))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
x
Reference in New Issue
Block a user