Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.50 KB | None | 0 0
  1. def format_launch(launch_item):
  2.     # prints an item from the LL launches list
  3.  
  4.     message_parts = [] # list with all the "parts" of the message, name, stream url etc.
  5.     message_parts.append(launch_item["name"])  # name of the launch
  6.  
  7.     if len(launch_item["vidURLs"]) >= 1:
  8.         message_parts.append(launch_item["vidURLs"][0])  # first stream url if available
  9.  
  10.     net_time = launch_item["net"]  # NET date
  11.  
  12.     # TBD/NET if unsure otherwise use NET
  13.     net_time = "TBD/NET " + net_time if launch_item["tbdtime"] else "NET " + net_time
  14.     message_parts.append(net_time)
  15.  
  16.     if not launch_item["tbdtime"]:  # add countdown if it isn't TBD
  17.         launch_time = dateutil.parser.parse(launch_item["isonet"]).replace(tzinfo=None)  # parse launch time
  18.         time_now = datetime.utcnow()
  19.  
  20.         def format_timedelta(td):
  21.             if td < timedelta(0):
  22.                 return '-' + format_timedelta(-td)
  23.             else:
  24.                 # Change this to format positive timedeltas the way you want
  25.                 return str(td)
  26.  
  27.         tdelta = format_timedelta(launch_time - time_now)  # Time difference
  28.  
  29.         # convert to string
  30.         t_minus = "T-" + str(tdelta)[:str(tdelta).index(".")]
  31.         t_minus = t_minus.replace("--", "+")  # two minuses = plus, right?
  32.         message_parts.append(t_minus)
  33.  
  34.     # return formatted string
  35.     return " - ".join(message_parts)
  36.  
  37.  
  38. def get_launch(search):
  39.     # Get launch string from name/search
  40.  
  41.     search = search.lstrip().rstrip()
  42.  
  43.     if search.replace(" ", "") == "":
  44.         # no search
  45.         try:
  46.             with urllib.request.urlopen("https://launchlibrary.net/1.2/launch?mode=verbose&next=1") as url:
  47.                 data = json.loads(url.read().decode())
  48.                 launch_list = data["launches"]
  49.                 if len(launch_list) > 0:
  50.                     return format_launch(launch_list[0])
  51.         except urllib.error.HTTPError:
  52.             return "Useless error message. LL is probably down."
  53.  
  54.     # Query for search
  55.     time_now = datetime.utcnow()
  56.     url_starttime = time_now.strftime("%Y-%m-%d")
  57.     query = "mode=verbose&limit=1&startdate={}&name={}"\
  58.             .format(url_starttime, urllib.parse.quote(search))
  59.  
  60.     # Get the launches
  61.     try:
  62.         with urllib.request.urlopen("https://launchlibrary.net/1.2/launch?" + query) as url:
  63.             data = json.loads(url.read().decode())
  64.             launch_list = data["launches"]
  65.  
  66.         if len(launch_list) > 0:
  67.             return format_launch(launch_list[0])
  68.         else:
  69.             return "Next launch for query '{}' not found.".format(search)
  70.  
  71.     except urllib.error.HTTPError:
  72.         return "Next launch for query '{}' not found.".format(search)
  73.  
  74. client = discord.Client()
  75.  
  76.  
  77. @client.event
  78. async def on_ready():
  79.     print('Logged in as')
  80.     print(client.user.name)
  81.     print(client.user.id)
  82.     print('------')
  83.  
  84.  
  85. @client.event
  86. async def on_message(message):
  87.     split_message = message.content.split()
  88.  
  89.     # if first 'word' is s!nextlaunch
  90.     if len(split_message) >= 1 and split_message[0].lower() == 's!nextlaunch':
  91.         print(message.author.name + " wrote: " + message.content)  # Log sender
  92.         rest_message = message.content[12:]  # message excluding the s!nextlaunch
  93.         launch_message = get_launch(rest_message)  # get the response string
  94.         print("Sending: " + launch_message)  # log message sent
  95.         await client.send_message(message.channel, launch_message)  # send it!
  96.  
  97. # Run client
  98. client.run(TOKEN)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement