Skip to content

Instantly share code, notes, and snippets.

@ItsCalebJones
Created October 23, 2017 01:29
Show Gist options
  • Save ItsCalebJones/4c64797132a2097b8a1d87c2d480c79c to your computer and use it in GitHub Desktop.
Save ItsCalebJones/4c64797132a2097b8a1d87c2d480c79c to your computer and use it in GitHub Desktop.
Python implementation for retrieving space launches from Launch Library
import requests
from datetime import timedelta
from django.utils.datetime_safe import datetime
BASE_URL = 'https://launchlibrary.net/'
headers = {
"Content-Type": "application/json"
}
class LaunchLibrarySDK(object):
# Latest stable Version stored.
def __init__(self, version='1.2.2'):
self.api_url = BASE_URL + version
def get_next_launch(self, tbd=False, agency=None, launch_service_provider=None, count=1):
"""
Builds a URL and fetches response from LL
:param agency: Pass the ID of an Agency to get launches for that agency (Rocket, Location, or Mission agency)
:param launch_service_provider: Pass the ID of a LSP to get launches for that provider
:param count: The number of launch objects to fetch.
:return: Returns a HTTP Response object
"""
if tbd:
url = self.api_url + '/launch/next/%d?mode=verbose' % count
else:
url = self.api_url + '/launch/next/%d?mode=verbose&tbdtime=0&tbddate=0' % count
# if agency:
# url = url + getLSP
return send_request(url, method='GET', headers=headers)
def get_next_weeks_launches(self):
"""
Sends a request using `requests` module.
:return: Returns a HTTP Response object
"""
today = datetime.today().strftime('%Y-%m-%d')
next_week = (datetime.now() + timedelta(days=7)).strftime('%Y-%m-%d')
url = self.api_url + '/launch/%s/%s?mode=verbose' % (today, next_week)
return send_request(url, method='GET', headers=headers)
def get_location_by_pad(self, locationId):
url = '%s/pad/%i?fields=name' % (self.api_url, locationId)
return send_request(url, method='GET', headers=headers)
def get_launch_by_id(self, id):
url = self.api_url + '/launch/%s?mode=verbose' % id
return send_request(url, method='GET', headers=headers)
def send_request(url, method='GET', data=None, headers=None):
"""
Sends a request using `requests` module.
:param url: URL to send request to
:param method: HTTP method to use e.g. GET, PUT, DELETE, POST
:param data: Data to send in case of PUT and POST
:param headers: HTTP headers to use
:return: Returns a HTTP Response object
"""
assert url and method
assert method in ['GET', 'PUT', 'DELETE', 'POST']
method = getattr(requests, method.lower())
response = method(url=url, data=data, headers=headers)
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment