This repository has been archived by the owner on May 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ed-pilot-check.py
82 lines (67 loc) · 2.83 KB
/
ed-pilot-check.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""
ed-pilot-check returns last known location of pilot from edsm.net
Copyright (C) 2018 Armen Kaleshian
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import http.client
import json
import logging
import os
from datetime import datetime
import boto3
LOGGER = logging.getLogger()
LOGGER.setLevel(os.environ.get('LOGGING_LEVEL', logging.INFO))
def get_api_key():
client = boto3.client('ssm')
response = client.get_parameter(
Name='edsm-api-key')
return response['Parameter']['Value']
def process_json(data):
position_data = json.loads(data)
date_docked = datetime.strptime(position_data['dateDocked'],
'%Y-%m-%d %H:%M:%S')
ship_type = position_data['shipType']
station = position_data['station']
now = datetime.now()
time_delta = now - date_docked
hours_delta = time_delta.seconds / 60 / 60
custom_data = {'days_since': time_delta.days,
'hours_since': hours_delta,
'last_ship': ship_type,
'last_station': station}
return custom_data
def build_response(pilot_data):
response_body = {"ship": pilot_data['last_ship'],
"station": pilot_data['last_station'],
"days": pilot_data['days_since'],
"hours": pilot_data['hours_since'],
"commander": pilot_data['commander_name']}
response = {'statusCode': 200,
'headers': {'Access-Control-Allow-Origin': '*'},
'body': json.dumps(response_body)}
return response
def lambda_handler(event, context):
api_key = get_api_key()
commander_name = event['queryStringParameters']['commander']
conn = http.client.HTTPSConnection('www.edsm.net')
conn.request('GET', '/api-logs-v1/get-position?commanderName='
+ commander_name
+ '&apiKey=' + api_key)
response = conn.getresponse()
if response.status == 200:
raw_data = response.read()
custom_data = process_json(raw_data)
custom_data['commander_name'] = commander_name
return build_response(custom_data)
else:
raise Exception('edsm.net did not return expected data due to %s',
response.reason)