69 lines
1.7 KiB
Python
69 lines
1.7 KiB
Python
#!/usr/bin/python3
|
|
|
|
import json
|
|
import urllib.request
|
|
import datetime
|
|
import time
|
|
import pytz
|
|
from string import Template
|
|
|
|
b = "\x0312"
|
|
y = "\x038"
|
|
|
|
API_URL = "https://fdo.rocketlaunch.live/json/launches/next/1"
|
|
|
|
class launch(object):
|
|
def api_request(self):
|
|
request = urllib.request.Request(API_URL)
|
|
response = urllib.request.urlopen(request).read()
|
|
json_data = json.loads(response.decode('UTF-8'))
|
|
return json_data['result'][0]
|
|
|
|
def rocket_name(self, json_data):
|
|
try:
|
|
name = json_data['vehicle']['name']
|
|
except:
|
|
name = "UNKNOWN"
|
|
return name
|
|
|
|
def mission_name(self, json_data):
|
|
try:
|
|
name = json_data['name']
|
|
except:
|
|
name = "UNKNOWN"
|
|
return name
|
|
|
|
def net_raw_utc(self, json_data):
|
|
time_utc_string = json_data['t0']
|
|
time_utc = datetime.datetime.fromisoformat(time_utc_string)
|
|
return time_utc
|
|
|
|
|
|
class DeltaTemplate(Template):
|
|
delimiter = "%"
|
|
|
|
|
|
def strfdelta(tdelta, fmt):
|
|
d = {"D": tdelta.days}
|
|
hours, rem = divmod(tdelta.seconds, 3600)
|
|
minutes, seconds = divmod(rem, 60)
|
|
d["H"] = '{:02d}'.format(hours)
|
|
d["M"] = '{:02d}'.format(minutes)
|
|
d["S"] = '{:02d}'.format(seconds)
|
|
t = DeltaTemplate(fmt)
|
|
return t.substitute(**d)
|
|
|
|
|
|
def next_launch():
|
|
l = launch()
|
|
json_data = l.api_request()
|
|
rocket = l.rocket_name(json_data)
|
|
mission = l.mission_name(json_data)
|
|
launch_time = l.net_raw_utc(json_data)
|
|
now = datetime.datetime.now(datetime.timezone.utc).replace(microsecond=0)
|
|
timedelta = abs(launch_time - now)
|
|
t_string = "-" if launch_time > now else "+"
|
|
deltastring = strfdelta(timedelta, "T"+t_string+"%D:%H:%M:%S")
|
|
del l
|
|
return rocket, mission, deltastring
|