from datetime import datetime , timedelta import requests from flask import Flask , render_template,redirect,url_for import json from apscheduler.schedulers.background import BackgroundScheduler from satnogs_api_client import fetch_satellites, DB_BASE_URL,fetch_tle_of_observation from satellite_tle import fetch_tles scheduler = BackgroundScheduler() app = Flask(__name__) Passes = [] Stations = [] TLEs = {} class Pass: id = 0 start = None end = None ground_station = None satellite = None transmitter = None norad = 0 def getActive(): start = (datetime.utcnow() - timedelta(0,0,0,0,20)).strftime('%Y-%m-%dT%H:%M:%S%z') end = (datetime.utcnow() + timedelta(0,0,0,0,30)).strftime('%Y-%m-%dT%H:%M:%S%z') passesR = requests.get("https://network.satnogs.org/api/observations/?end="+end+"&format=json&start="+start) passes = passesR.json() if passesR.links.has_key("next"): while passesR.links.has_key("next"): passesR = requests.get(passesR.links["next"]["url"]) passes += passesR.json() ground_stations = {} for x in passes: if datetime.strptime(x["start"],'%Y-%m-%dT%H:%M:%Sz') > datetime.utcnow() or datetime.strptime(x["end"],'%Y-%m-%dT%H:%M:%Sz') < datetime.utcnow(): passes.remove(x) else: if ground_stations.has_key(x["ground_station"]): ground_stations[x["ground_station"]].append(x) else: ground_stations[x["ground_station"]] = [] ground_stations[x["ground_station"]].append(x) passes = [] for x in ground_stations: start = datetime.utcnow() current = {"start":datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S%z')+"z"} for y in ground_stations[x]: if datetime.strptime(y["start"],'%Y-%m-%dT%H:%M:%Sz') < datetime.strptime(current["start"],'%Y-%m-%dT%H:%M:%Sz'): current = y passes.append(current) Passes =[] for x in passes: temp = Pass() temp.id = x["id"] temp.start = datetime.strptime(x["start"],'%Y-%m-%dT%H:%M:%Sz') temp.end = datetime.strptime(x["end"],'%Y-%m-%dT%H:%M:%Sz') temp.ground_station = x["ground_station"] temp.norad = str(x["norad_cat_id"]) try: temp.satellite = requests.get("https://db.satnogs.org/api/satellites/"+str(x["norad_cat_id"])).json() except: temp.satellite = {"name":""} Passes.append(temp) return Passes def GetGroundStations(): stationsR = requests.get("https://network.satnogs.org/api/stations/") stations = stationsR.json() while stationsR.links.has_key("next"): stationsR = requests.get(stationsR.links["next"]["url"]) stations += stationsR.json() for x in stations: if x["last_seen"] == None: stations.remove(x) continue if datetime.strptime(x["last_seen"],'%Y-%m-%dT%H:%M:%Sz') < (datetime.utcnow()- timedelta(10,0,0,0)): stations.remove(x) for x in stations: if x["last_seen"] == None: stations.remove(x) continue if datetime.strptime(x["last_seen"],'%Y-%m-%dT%H:%M:%Sz') < (datetime.utcnow()- timedelta(10,0,0,0)): stations.remove(x) return stations @scheduler.scheduled_job('interval',minutes=3) def updatePasses(): global Passes print "Updating Passes" Passes = getActive() @scheduler.scheduled_job('interval',hours=1) def updateStations(): global Stations print "Updating Stations" Stations = GetGroundStations() @scheduler.scheduled_job('interval',days=1) def updateTLE(): print "Updating TLE" global TlEs sats = fetch_satellites(None,DB_BASE_URL) satnogs_db_norad_ids = set(sat['norad_cat_id'] for sat in sats if sat['status'] != 're-entered') # Remove satellites with temporary norad ids temporary_norad_ids = set(filter(lambda norad_id: norad_id >= 99900, satnogs_db_norad_ids)) satnogs_db_norad_ids = satnogs_db_norad_ids - temporary_norad_ids # Fetch TLEs for the satellites of interest print satnogs_db_norad_ids tles = fetch_tles(satnogs_db_norad_ids) TLEs = {} for norad_id, (source, tle) in tles.items(): TLEs[norad_id] = [str(tle[0]),str(tle[1]),str(tle[2])] print('\nTLEs for {} of {} requested satellites found ({} satellites with temporary norad ids skipped).'.format(len(tles), len(satnogs_db_norad_ids), len(temporary_norad_ids))) @app.route("/") def map_view(): return render_template("map.html") @app.route('/active_stations') def api_active_stations(): sations = [] for x in Stations: sations.append([x["name"],x["lat"],x["lng"]]) return json.dumps(sations) @app.route('/stations_from_sat/') def api_occuring_observations(norad): obs = [] for x in Passes: if x.norad == norad: obs.append(x.ground_station) return json.dumps(obs) @app.route('/occuring_sats') def api_occuring_sats(): tle = {} for x in Passes: if x.satellite['norad_cat_id'] not in TLEs.keys(): q = fetch_tle_of_observation(x.id) TLEs[ x.norad ] = [str(x.satellite["name"]),str(q[0]),str(q[1])] tle[x.norad] = TLEs[x.norad] return json.dumps(tle) updatePasses() updateStations() updateTLE() scheduler.start() app.run(use_reloader=False,host = "0.0.0.0",port=5001)