Add filters for space launches

This commit is contained in:
Edward Betts 2024-07-01 22:27:19 +03:00
parent c41bcc3304
commit b65d79cb63
3 changed files with 66 additions and 6 deletions

View file

@ -136,7 +136,7 @@ def summarize_launch(launch: Launch) -> Summary:
"launch_provider": launch_provider, "launch_provider": launch_provider,
"launch_provider_abbrev": launch_provider_abbrev, "launch_provider_abbrev": launch_provider_abbrev,
"launch_provider_type": get_nested(launch, ["launch_service_provider", "type"]), "launch_provider_type": get_nested(launch, ["launch_service_provider", "type"]),
"rocket": launch["rocket"]["configuration"]["full_name"], "rocket": launch["rocket"]["configuration"],
"mission": launch.get("mission"), "mission": launch.get("mission"),
"mission_name": get_nested(launch, ["mission", "name"]), "mission_name": get_nested(launch, ["mission", "name"]),
"pad_name": launch["pad"]["name"], "pad_name": launch["pad"]["name"],
@ -174,7 +174,10 @@ def get_launches(
existing.sort(reverse=True) existing.sort(reverse=True)
if refresh or not existing or (now - existing[0][0]).seconds > ttl: if refresh or not existing or (now - existing[0][0]).seconds > ttl:
try:
return next_launch_api(rocket_dir, limit=limit) return next_launch_api(rocket_dir, limit=limit)
except Exception:
pass # fallback to cached version
f = existing[0][1] f = existing[0][1]

View file

@ -6,7 +6,40 @@
<div class="container-fluid mt-2"> <div class="container-fluid mt-2">
<h1>Space launches</h1> <h1>Space launches</h1>
{% for launch in rockets %} <h4>Filters</h4>
<p>Mission type:
{% if request.args.type %}<a href="{{ request.path }}">🗙</a>{% endif %}
{% for t in mission_types | sort %}
{% if t == request.args.type %}
<strong>{{ t }}</strong>
{% else %}
<a href="?type={{ t }}" class="text-nowrap">
{{ t }}
</a>
{% endif %}
{% if not loop.last %} | {% endif %}
{% endfor %}
</p>
<p>Vehicle:
{% if request.args.rocket %}<a href="{{ request.path }}">🗙</a>{% endif %}
{% for r in rockets | sort %}
{% if r == request.args.rockets %}
<strong>{{ r }}</strong>
{% else %}
<a href="?rocket={{ r }}" class="text-nowrap">
{{ r }}
</a>
{% endif %}
{% if not loop.last %} | {% endif %}
{% endfor %}
</p>
{% for launch in launches %}
{% set highlight =" bg-primary-subtle" if launch.slug in config.FOLLOW_LAUNCHES else "" %} {% set highlight =" bg-primary-subtle" if launch.slug in config.FOLLOW_LAUNCHES else "" %}
{% set country = get_country(launch.country_code) %} {% set country = get_country(launch.country_code) %}
<div class="row{{highlight}}"> <div class="row{{highlight}}">
@ -24,7 +57,7 @@
<div class="col"> <div class="col">
<div> <div>
{{ country.flag }} {{ country.flag }}
{{ launch.rocket }} {{ launch.rocket.full_name }}
&ndash; &ndash;
<strong>{{launch.mission.name }}</strong> <strong>{{launch.mission.name }}</strong>
&ndash; &ndash;

View file

@ -145,10 +145,34 @@ def launch_list() -> str:
now = datetime.now() now = datetime.now()
data_dir = app.config["DATA_DIR"] data_dir = app.config["DATA_DIR"]
rocket_dir = os.path.join(data_dir, "thespacedevs") rocket_dir = os.path.join(data_dir, "thespacedevs")
rockets = agenda.thespacedevs.get_launches(rocket_dir, limit=100) launches = agenda.thespacedevs.get_launches(rocket_dir, limit=100)
mission_type_filter = flask.request.args.get("type")
rocket_filter = flask.request.args.get("rocket")
mission_types = {
launch["mission"]["type"] for launch in launches if launch["mission"]
}
rockets = {launch["rocket"]["full_name"] for launch in launches}
launches = [
launch
for launch in launches
if (
not mission_type_filter
or (launch["mission"] and launch["mission"]["type"] == mission_type_filter)
)
and (not rocket_filter or launch["rocket"]["full_name"] == rocket_filter)
]
return flask.render_template( return flask.render_template(
"launches.html", rockets=rockets, now=now, get_country=agenda.get_country "launches.html",
launches=launches,
rockets=rockets,
now=now,
get_country=agenda.get_country,
mission_types=mission_types,
) )