Created
January 30, 2021 20:27
-
-
Save ItsCalebJones/1c38922507808b52dc744bc7593568df to your computer and use it in GitHub Desktop.
Sync LL and SpaceX APIs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import timedelta | |
from difflib import SequenceMatcher | |
import requests | |
from django.utils.text import slugify | |
def find_spacex_api(launch): | |
max_delta = 7 | |
url = 'https://api.spacexdata.com/v3/launches' | |
sta = launch.window_start + timedelta(days=- max_delta) | |
end = launch.window_end + timedelta(days=+ max_delta) | |
# As SpaceX API doesn't offer the mission name as search criteria | |
# we ask for all spacex launches in +/- 7 days around the date | |
payload = { | |
'id': 'true', | |
'start': sta.strftime('%Y-%m-%d'), | |
'end': end.strftime('%Y-%m-%d'), | |
} | |
r = requests.get(url, params=payload) | |
allowed_delta = (max_delta * 24 * 3600) | |
allowed_ratio = 0.3 | |
results = r.json() | |
print("\n") | |
print(launch.name) | |
for api_launch in results: | |
delta = abs(launch.net.timestamp() - api_launch['launch_date_unix']) | |
n1 = slugify(launch.name.split("|")[1].strip()) | |
n2 = slugify(api_launch['mission_name']) | |
ratio = SequenceMatcher(None, n1, n2).ratio() | |
if (delta < 1000 and ratio > 0.1) or (delta < allowed_delta and ratio > allowed_ratio): | |
print('Found match in SpaceXAPI %s (ratio %s, delta %s/%s)' % (api_launch['_id'], | |
ratio, | |
delta, | |
allowed_delta)) | |
return api_launch['_id'] | |
else: | |
print(api_launch) | |
print('Not a match in SpaceXAPI (ratio %s, delta %s/%s)' % (ratio, delta, allowed_delta)) | |
print("\n") | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment