2018-10-24 14:32:53 -06:00
from datetime import datetime , timedelta
import requests
from flask import Flask , render_template , redirect , url_for
import json
2018-12-22 00:33:23 -07:00
import random
2018-10-24 14:32:53 -06:00
from apscheduler . schedulers . background import BackgroundScheduler
2018-10-24 15:37:05 -06:00
from satnogs_api_client import fetch_satellites , DB_BASE_URL , fetch_tle_of_observation
2018-10-24 14:32:53 -06:00
from satellite_tle import fetch_tles
scheduler = BackgroundScheduler ( )
app = Flask ( __name__ )
Passes = [ ]
2018-12-19 14:25:38 -07:00
Occuring_sats = { }
2018-10-24 14:32:53 -06:00
Stations = [ ]
TLEs = { }
2018-12-22 00:33:23 -07:00
Transmitters = { }
2018-10-24 14:32:53 -06:00
class Pass :
id = 0
start = None
end = None
ground_station = None
satellite = None
transmitter = None
2018-11-21 13:28:37 -07:00
norad = 0
2018-12-18 11:27:19 -07:00
2018-10-24 14:32:53 -06:00
def getActive ( ) :
start = ( datetime . utcnow ( ) - timedelta ( 0 , 0 , 0 , 0 , 20 ) ) . strftime ( ' % Y- % m- %d T % H: % M: % S % z ' )
end = ( datetime . utcnow ( ) + timedelta ( 0 , 0 , 0 , 0 , 30 ) ) . strftime ( ' % Y- % m- %d T % 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- %d T % H: % M: % Sz ' ) > datetime . utcnow ( ) or datetime . strptime ( x [ " end " ] , ' % Y- % m- %d T % 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- %d T % H: % M: % S % z ' ) + " z " }
for y in ground_stations [ x ] :
if datetime . strptime ( y [ " start " ] , ' % Y- % m- %d T % H: % M: % Sz ' ) < datetime . strptime ( current [ " start " ] , ' % Y- % m- %d T % 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- %d T % H: % M: % Sz ' )
temp . end = datetime . strptime ( x [ " end " ] , ' % Y- % m- %d T % H: % M: % Sz ' )
temp . ground_station = x [ " ground_station " ]
2018-12-22 00:33:23 -07:00
temp . transmitter = x [ " transmitter " ]
2018-11-21 13:28:37 -07:00
temp . norad = str ( x [ " norad_cat_id " ] )
try :
2018-12-18 11:27:19 -07:00
temp . satellite = requests . get ( " https://db.satnogs.org/api/satellites/ " + str ( x [ " norad_cat_id " ] ) ) . json ( )
2018-11-21 13:28:37 -07:00
except :
temp . satellite = { " name " : " " }
2018-10-24 14:32:53 -06:00
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- %d T % 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- %d T % H: % M: % Sz ' ) < ( datetime . utcnow ( ) - timedelta ( 10 , 0 , 0 , 0 ) ) :
stations . remove ( x )
return stations
2018-12-22 00:33:23 -07:00
@scheduler.scheduled_job ( ' interval ' , days = 5 )
def updateTransmitters ( ) :
global Transmitters
print " Updating Transmitters "
temp = requests . get ( " https://db.satnogs.org/api/transmitters/ " ) . json ( )
for x in temp :
if str ( x [ " norad_cat_id " ] ) in Transmitters . keys ( ) :
Transmitters [ str ( x [ " norad_cat_id " ] ) ] [ x [ " uuid " ] ] = [ x [ " description " ] , " # " + str ( " %06x " % random . randint ( 0 , 0xFFFFFF ) ) ]
else :
Transmitters [ str ( x [ " norad_cat_id " ] ) ] = { }
Transmitters [ str ( x [ " norad_cat_id " ] ) ] [ x [ " uuid " ] ] = [ x [ " description " ] , " # " + str ( " %06x " % random . randint ( 0 , 0xFFFFFF ) ) ]
#print Transmitters
2018-10-24 14:32:53 -06:00
@scheduler.scheduled_job ( ' interval ' , minutes = 3 )
def updatePasses ( ) :
global Passes
2018-12-19 14:25:38 -07:00
global Occuring_sats
2018-10-24 14:32:53 -06:00
print " Updating Passes "
Passes = getActive ( )
2018-12-19 14:25:38 -07:00
Occuring_sats = { }
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 ] ) ]
Occuring_sats [ x . norad ] = TLEs [ x . norad ]
2018-11-21 13:28:37 -07:00
@scheduler.scheduled_job ( ' interval ' , hours = 1 )
2018-10-24 14:32:53 -06:00
def updateStations ( ) :
global Stations
print " Updating Stations "
Stations = GetGroundStations ( )
@scheduler.scheduled_job ( ' interval ' , days = 1 )
def updateTLE ( ) :
print " Updating TLE "
2018-10-24 14:35:01 -06:00
global TlEs
2018-12-18 11:27:19 -07:00
sats = fetch_satellites ( None , DB_BASE_URL )
2018-10-24 14:32:53 -06:00
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
2018-12-19 14:25:38 -07:00
2018-10-24 14:32:53 -06:00
tles = fetch_tles ( satnogs_db_norad_ids )
2018-10-24 14:35:01 -06:00
TLEs = { }
2018-10-24 14:32:53 -06:00
for norad_id , ( source , tle ) in tles . items ( ) :
TLEs [ norad_id ] = [ str ( tle [ 0 ] ) , str ( tle [ 1 ] ) , str ( tle [ 2 ] ) ]
print ( ' \n TLEs for {} of {} requested satellites found ( {} satellites with temporary norad ids skipped). ' . format ( len ( tles ) , len ( satnogs_db_norad_ids ) , len ( temporary_norad_ids ) ) )
2018-11-01 13:12:35 -06:00
@app.route ( " / " )
2018-10-24 14:32:53 -06:00
def map_view ( ) :
2018-12-18 11:27:19 -07:00
return render_template ( " map.html " )
2018-10-24 14:32:53 -06:00
2018-12-18 19:41:43 -07:00
@app.route ( ' /active_stations ' )
2018-10-24 14:32:53 -06:00
def api_active_stations ( ) :
2018-12-18 11:49:23 -07:00
sations = [ ]
for x in Stations :
2018-12-19 14:25:38 -07:00
sations . append ( { ' id ' : x [ ' id ' ] , ' name ' : x [ ' name ' ] , ' lat_lng ' : [ x [ " lat " ] , x [ ' lng ' ] ] } )
2018-12-18 19:41:43 -07:00
return json . dumps ( sations )
2018-10-24 14:32:53 -06:00
2018-12-18 19:42:36 -07:00
@app.route ( ' /stations_from_sat/<string:norad> ' )
2018-12-18 19:30:14 -07:00
def api_occuring_observations ( norad ) :
2018-10-24 14:32:53 -06:00
obs = [ ]
2018-12-22 00:33:23 -07:00
trans = [ ]
2018-10-24 14:32:53 -06:00
for x in Passes :
2018-12-18 19:30:14 -07:00
if x . norad == norad :
2018-12-22 00:33:23 -07:00
obs . append ( [ x . ground_station , Transmitters [ norad ] [ x . transmitter ] [ 1 ] ] )
trans . append ( x . transmitter )
#print Transmitters[norad].values()
transList = [ ]
for x in set ( trans ) :
transList . append ( Transmitters [ norad ] [ x ] )
#print transList,norad
return json . dumps ( [ obs , transList ] )
2018-10-24 14:32:53 -06:00
2018-12-18 19:41:43 -07:00
@app.route ( ' /occuring_sats ' )
2018-10-24 14:32:53 -06:00
def api_occuring_sats ( ) :
2018-12-19 14:25:38 -07:00
return json . dumps ( Occuring_sats )
2018-10-24 14:32:53 -06:00
2018-12-18 11:27:19 -07:00
2018-10-24 14:32:53 -06:00
2018-12-18 11:27:19 -07:00
2018-10-24 14:32:53 -06:00
updatePasses ( )
updateStations ( )
updateTLE ( )
2018-12-22 00:33:23 -07:00
updateTransmitters ( )
2018-10-24 14:32:53 -06:00
scheduler . start ( )
2018-12-18 11:28:17 -07:00
app . run ( use_reloader = False , host = " 0.0.0.0 " , port = 5001 )