import requests from datetime import datetime def nextLaunch(): """Uses the launchlibrary.net api to get the next rocket launch and returns a dictionary with all the important information. Example Output: { "rocket": "Proton-M/Blok DM-03 ", "mission": " Spektr-RG", "date": "July 12, 2019 12:31:00 UTC", "countdown": "The launch is in 14 hours and 48 minutes.", "video": "https://www.youtube.com/watch?v=Dy1nkGghEVk", } """ url = "https://launchlibrary.net/1.4/launch/next/1?mode=list" response = requests.get(url) if response.status_code is 200: data = response.json()["launches"][0] rocket, mission = data["name"].split("|") date = data["net"] # Extract the timde delta between now and the launch, then format it into an easy to read format. countdown = str( datetime.strptime(date, "%B %d, %Y %H:%M:%S %Z") - datetime.today() ).split(":") countdown = f"The launch is in {countdown[0]} hours and {countdown[1]} minutes." else: print("error") launch = { "rocket": rocket.strip(), "mission": mission.strip(), "date": date, "countdown": countdown, "video": data["vidURLs"][0], } return launch