Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def format_launch(launch_item):
- # prints an item from the LL launches list
- message_parts = [] # list with all the "parts" of the message, name, stream url etc.
- message_parts.append(launch_item["name"]) # name of the launch
- if len(launch_item["vidURLs"]) >= 1:
- message_parts.append(launch_item["vidURLs"][0]) # first stream url if available
- net_time = launch_item["net"] # NET date
- # TBD/NET if unsure otherwise use NET
- net_time = "TBD/NET " + net_time if launch_item["tbdtime"] else "NET " + net_time
- message_parts.append(net_time)
- if not launch_item["tbdtime"]: # add countdown if it isn't TBD
- launch_time = dateutil.parser.parse(launch_item["isonet"]).replace(tzinfo=None) # parse launch time
- time_now = datetime.utcnow()
- def format_timedelta(td):
- if td < timedelta(0):
- return '-' + format_timedelta(-td)
- else:
- # Change this to format positive timedeltas the way you want
- return str(td)
- tdelta = format_timedelta(launch_time - time_now) # Time difference
- # convert to string
- t_minus = "T-" + str(tdelta)[:str(tdelta).index(".")]
- t_minus = t_minus.replace("--", "+") # two minuses = plus, right?
- message_parts.append(t_minus)
- # return formatted string
- return " - ".join(message_parts)
- def get_launch(search):
- # Get launch string from name/search
- search = search.lstrip().rstrip()
- if search.replace(" ", "") == "":
- # no search
- try:
- with urllib.request.urlopen("https://launchlibrary.net/1.2/launch?mode=verbose&next=1") as url:
- data = json.loads(url.read().decode())
- launch_list = data["launches"]
- if len(launch_list) > 0:
- return format_launch(launch_list[0])
- except urllib.error.HTTPError:
- return "Useless error message. LL is probably down."
- # Query for search
- time_now = datetime.utcnow()
- url_starttime = time_now.strftime("%Y-%m-%d")
- query = "mode=verbose&limit=1&startdate={}&name={}"\
- .format(url_starttime, urllib.parse.quote(search))
- # Get the launches
- try:
- with urllib.request.urlopen("https://launchlibrary.net/1.2/launch?" + query) as url:
- data = json.loads(url.read().decode())
- launch_list = data["launches"]
- if len(launch_list) > 0:
- return format_launch(launch_list[0])
- else:
- return "Next launch for query '{}' not found.".format(search)
- except urllib.error.HTTPError:
- return "Next launch for query '{}' not found.".format(search)
- client = discord.Client()
- @client.event
- async def on_ready():
- print('Logged in as')
- print(client.user.name)
- print(client.user.id)
- print('------')
- @client.event
- async def on_message(message):
- split_message = message.content.split()
- # if first 'word' is s!nextlaunch
- if len(split_message) >= 1 and split_message[0].lower() == 's!nextlaunch':
- print(message.author.name + " wrote: " + message.content) # Log sender
- rest_message = message.content[12:] # message excluding the s!nextlaunch
- launch_message = get_launch(rest_message) # get the response string
- print("Sending: " + launch_message) # log message sent
- await client.send_message(message.channel, launch_message) # send it!
- # Run client
- client.run(TOKEN)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement