From da2fb7802b7bbd4900ff1dc9ba184b5620ea8459 Mon Sep 17 00:00:00 2001 From: dvirlabs Date: Sun, 15 Feb 2026 17:06:20 +0200 Subject: [PATCH] Add Python service foundation and gitignore --- .gitignore | 1 + coordinates_cache.json | 26 +++++++++++++++++ main.py | 66 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 .gitignore create mode 100644 coordinates_cache.json create mode 100644 main.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3330019 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +coordinates_cache.json \ No newline at end of file diff --git a/coordinates_cache.json b/coordinates_cache.json new file mode 100644 index 0000000..e274f31 --- /dev/null +++ b/coordinates_cache.json @@ -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" + } +} \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..24fbf97 --- /dev/null +++ b/main.py @@ -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()