Skip to content

Commit

Permalink
updated README
Browse files Browse the repository at this point in the history
  • Loading branch information
redraw committed May 9, 2024
1 parent 6a00df2 commit e7ce8f8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 38 deletions.
11 changes: 7 additions & 4 deletions app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ Parameters:
- `days` number of days to calculate passes ahead
- `visible_only` can be `true`/`false`, filter passes by visible passes only

#### Response example

```
GET /passes/25544?lat=-34.9112212&lon=-57.9372988&limit=1 HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: sat.terrestre.ar
User-Agent: HTTPie/1.0.3
```
Response example,
```
[
{
"rise": {
Expand Down Expand Up @@ -60,8 +61,10 @@ Response example,
}
]
```
Notes:

- `alt`/`az` are measured in degrees.
- `is_sunlit` tells if satellite is being illuminated by the sun.
- `visible` field tells if the satellite will be _probably_ visible, considering the sun is near the horizon, and the observer is at night. You can read more [here](https://www.heavens-above.com/faq.aspx).

Note: Results are cached 1 day for each parameters combo, except `limit`.
Results are cached 1 day for each parameters combo, except `limit`.
57 changes: 25 additions & 32 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,69 +15,64 @@
from tle import TLENotFound


api = Flask('api')
api = Flask("api")
api.logger.setLevel(logging.INFO)
CORS(api)


@api.before_request
def redirect_to_custom_domain():
custom_domain = os.getenv('CUSTOM_DOMAIN')
if (
custom_domain and
"FLY_APP_NAME" in os.environ and
request.host == f"{os.getenv('FLY_APP_NAME')}.fly.dev"
):
return redirect(f'https://{custom_domain}{request.full_path}', code=301)
custom_domain = os.getenv("CUSTOM_DOMAIN")
if custom_domain and "FLY_APP_NAME" in os.environ and request.host == f"{os.getenv('FLY_APP_NAME')}.fly.dev":
return redirect(f"https://{custom_domain}{request.full_path}", code=301)


@api.route('/passes/<int:norad_id>')
@api.route("/passes/<int:norad_id>")
def passes(norad_id):
try:
query = PassesQuery().load(request.args)
except ValidationError as err:
return jsonify(err.messages), 400

limit = query.pop('limit')
limit = query.pop("limit")
cache_key = get_cache_key(norad_id, query, prefix="passes")
cache_response = cache.get(cache_key)

# Return results from cache if hit
if cache_response:
passes = json.loads(cache_response)
next_passes = filter_next_passes(passes)
return Response(json.dumps(next_passes[:limit]), headers={
"x-api-cache": "HIT",
"x-api-cache-ttl": f"{cache.ttl(cache_key)}",
"cache-control": f"public, max-age={cache.ttl(cache_key)}",
"content-type": "application/json"
})
return Response(
json.dumps(next_passes[:limit]),
headers={
"x-api-cache": "HIT",
"x-api-cache-ttl": f"{cache.ttl(cache_key)}",
"cache-control": f"public, max-age={cache.ttl(cache_key)}",
"content-type": "application/json",
},
)

# Calculate next passes
try:
tracker = SatTracker(query["lat"], query["lon"], norad_id=norad_id)
except TLENotFound:
return jsonify({"error": "TLE not found"}), 400

passes = tracker.next_passes(
days=query["days"],
visible_only=query["visible_only"]
)
passes = tracker.next_passes(days=query["days"], visible_only=query["visible_only"])

# Cache passes for 1 day
content = json.dumps(passes)
cache.set(cache_key, content, ex=timedelta(days=1))

return Response(json.dumps(passes[:limit]), headers={
"x-api-cache": "MISS",
"content-type": "application/json"
})
return Response(json.dumps(passes[:limit]), headers={"x-api-cache": "MISS", "content-type": "application/json"})


@api.route('/')
def docs():
with open('README.md') as f:
return markdown.markdown(f.read(), extensions=['fenced_code'])
@api.route("/")
def index():
with open("README.md") as f:
html = markdown.markdown(f.read(), extensions=["fenced_code"])
content = html + "<style>body {font-family: sans-serif}</style>"
return content


@api.route("/openapi.json")
Expand All @@ -86,12 +81,10 @@ def api_spec():


swaggerui_blueprint = get_swaggerui_blueprint(
base_url="/docs",
api_url="/openapi.json",
config={"app_name": "Satellite Passes API"}
base_url="/docs", api_url="/openapi.json", config={"app_name": "Satellite Passes API"}
)

api.register_blueprint(swaggerui_blueprint)

if __name__ == '__main__':
if __name__ == "__main__":
api.run(port=8000, debug=True)
4 changes: 2 additions & 2 deletions app/static/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"version": "0.1.0"
},
"servers": [
{"url": "https://satellites.fly.dev/"},
{"url": "https://sat.terrestre.ar/"},
{"url": "http://0.0.0.0:8000/"}
],
"paths": {
Expand Down Expand Up @@ -231,4 +231,4 @@
}
}
}
}
}

0 comments on commit e7ce8f8

Please sign in to comment.