Skip to content

Instantly share code, notes, and snippets.

@dtkav
Created September 2, 2016 07:26
Show Gist options
  • Save dtkav/815d793a850e2e91a5b4c09f7ed7064f to your computer and use it in GitHub Desktop.
Save dtkav/815d793a850e2e91a5b4c09f7ed7064f to your computer and use it in GitHub Desktop.
ephemerides-jsonp-lambda
from __future__ import print_function
import urllib
import urllib2
import json
PLANET_URL = "http://ephemerides.planet-labs.com/planet_mc.tle"
def lambda_handler(event, context):
cb = event["params"]["querystring"].get("callback", "tle_data")
def jsonp(callback, data):
fmt = "{callback}({data})"
fmt = fmt.format(callback=callback, data=json.dumps({"sources": {"PLANET": {"satellites": data}}}))
return fmt
tles = []
req = urllib2.Request(PLANET_URL, None)
response = urllib2.urlopen(req)
tle_txt = response.read().split("\n")
tle_data = {}
counter = 0
sat = None
for line in tle_txt:
if not line:
continue
if counter == 0:
split = line.split()
sat = split[3]
flock = split[2].split("-")[0]
tle_data[sat] = {"flock": flock}
else:
tle_data[sat]["tle" + str(counter)] = line
counter = (counter + 1) % 3
print(event)
return jsonp(cb, tle_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment