diff --git a/routes/satellite.py b/routes/satellite.py index b3a7e98..8fb9054 100644 --- a/routes/satellite.py +++ b/routes/satellite.py @@ -11,9 +11,9 @@ from urllib.parse import urlparse import requests -from flask import Blueprint, jsonify, request, render_template, Response - -from config import SHARED_OBSERVER_LOCATION_ENABLED +from flask import Blueprint, jsonify, request, render_template, Response + +from config import SHARED_OBSERVER_LOCATION_ENABLED from data.satellites import TLE_SATELLITES from utils.logging import satellite_logger as logger @@ -42,30 +42,30 @@ def _fetch_iss_realtime(observer_lat: Optional[float] = None, observer_lon: Opti iss_alt = 420 # Default altitude in km source = None - # Try primary API: Open Notify + # Try primary API: Where The ISS At try: - response = requests.get('http://api.open-notify.org/iss-now.json', timeout=5) + response = requests.get('https://api.wheretheiss.at/v1/satellites/25544', timeout=5) if response.status_code == 200: data = response.json() - if data.get('message') == 'success': - iss_lat = float(data['iss_position']['latitude']) - iss_lon = float(data['iss_position']['longitude']) - source = 'open-notify' + iss_lat = float(data['latitude']) + iss_lon = float(data['longitude']) + iss_alt = float(data.get('altitude', 420)) + source = 'wheretheiss' except Exception as e: - logger.debug(f"Open Notify API failed: {e}") + logger.debug(f"Where The ISS At API failed: {e}") - # Try fallback API: Where The ISS At + # Try fallback API: Open Notify if iss_lat is None: try: - response = requests.get('https://api.wheretheiss.at/v1/satellites/25544', timeout=5) + response = requests.get('http://api.open-notify.org/iss-now.json', timeout=5) if response.status_code == 200: data = response.json() - iss_lat = float(data['latitude']) - iss_lon = float(data['longitude']) - iss_alt = float(data.get('altitude', 420)) - source = 'wheretheiss' + if data.get('message') == 'success': + iss_lat = float(data['iss_position']['latitude']) + iss_lon = float(data['iss_position']['longitude']) + source = 'open-notify' except Exception as e: - logger.debug(f"Where The ISS At API failed: {e}") + logger.debug(f"Open Notify API failed: {e}") if iss_lat is None: return None @@ -120,12 +120,12 @@ def _fetch_iss_realtime(observer_lat: Optional[float] = None, observer_lon: Opti @satellite_bp.route('/dashboard') -def satellite_dashboard(): - """Popout satellite tracking dashboard.""" - return render_template( - 'satellite_dashboard.html', - shared_observer_location=SHARED_OBSERVER_LOCATION_ENABLED, - ) +def satellite_dashboard(): + """Popout satellite tracking dashboard.""" + return render_template( + 'satellite_dashboard.html', + shared_observer_location=SHARED_OBSERVER_LOCATION_ENABLED, + ) @satellite_bp.route('/predict', methods=['POST']) diff --git a/routes/sstv.py b/routes/sstv.py index aa2a179..1640fb8 100644 --- a/routes/sstv.py +++ b/routes/sstv.py @@ -467,7 +467,32 @@ def iss_position(): observer_lat = request.args.get('latitude', type=float) observer_lon = request.args.get('longitude', type=float) - # Try primary API: Open Notify + # Try primary API: Where The ISS At + try: + response = requests.get('https://api.wheretheiss.at/v1/satellites/25544', timeout=5) + if response.status_code == 200: + data = response.json() + iss_lat = float(data['latitude']) + iss_lon = float(data['longitude']) + + result = { + 'status': 'ok', + 'lat': iss_lat, + 'lon': iss_lon, + 'altitude': float(data.get('altitude', 420)), + 'timestamp': datetime.utcnow().isoformat(), + 'source': 'wheretheiss' + } + + # Calculate observer-relative data if location provided + if observer_lat is not None and observer_lon is not None: + result.update(_calculate_observer_data(iss_lat, iss_lon, observer_lat, observer_lon)) + + return jsonify(result) + except Exception as e: + logger.warning(f"Where The ISS At API failed: {e}") + + # Try fallback API: Open Notify try: response = requests.get('http://api.open-notify.org/iss-now.json', timeout=5) if response.status_code == 200: @@ -493,31 +518,6 @@ def iss_position(): except Exception as e: logger.warning(f"Open Notify API failed: {e}") - # Try fallback API: Where The ISS At - try: - response = requests.get('https://api.wheretheiss.at/v1/satellites/25544', timeout=5) - if response.status_code == 200: - data = response.json() - iss_lat = float(data['latitude']) - iss_lon = float(data['longitude']) - - result = { - 'status': 'ok', - 'lat': iss_lat, - 'lon': iss_lon, - 'altitude': float(data.get('altitude', 420)), - 'timestamp': datetime.utcnow().isoformat(), - 'source': 'wheretheiss' - } - - # Calculate observer-relative data if location provided - if observer_lat is not None and observer_lon is not None: - result.update(_calculate_observer_data(iss_lat, iss_lon, observer_lat, observer_lon)) - - return jsonify(result) - except Exception as e: - logger.warning(f"Where The ISS At API failed: {e}") - # Both APIs failed return jsonify({ 'status': 'error',