-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathluftfartygsregistret.py
60 lines (41 loc) · 1.75 KB
/
luftfartygsregistret.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
import json
from services.downloader import Downloader
from services.parser import Parser
def get_aircraft_list(code=None, downloader=Downloader()):
if code:
return Parser.parse_aircraft_list(
downloader.fetch_aircraft_list_with_code(code)
)
return Parser.parse_aircraft_lists(downloader.fetch_aircraft_list())
def get_aircraft(code=None, downloader=Downloader()):
return Parser.parse_aircraft_details(
downloader.fetch_aircraft_list_with_code(code).result().content
)
def get_aircrafts_with_details_from_list(codes, downloader=Downloader()):
return list(
filter(
None,
Parser.parse_aircrafts_details(
downloader.fetch_aircrafts_with_details(codes)
),
)
)
def get_aircrafts_with_details_from_file(filename, downloader=Downloader()):
with open(filename) as file_json:
aircrafts = json.load(file_json)
return get_aircrafts_with_details_from_list(aircrafts.keys(), downloader)
def get_aircrafts_with_details(code=None, downloader=Downloader()):
aircrafts = get_aircraft_list(code, downloader)
return get_aircrafts_with_details_from_list(list(aircrafts), downloader)
def remove_anonymous_owners(aircraft):
if not aircraft or "owners" not in aircraft:
return aircraft
non_anonymous_owners = []
for owner in aircraft["owners"]:
if "ägare" in owner["type"] and owner["name"] != "ANONYMOUS":
non_anonymous_owners.append(owner)
aircraft["owners_amount"] = len(aircraft["owners"]) - 1
aircraft.pop("owners", None)
owners = [dict(t) for t in {tuple(d.items()) for d in non_anonymous_owners}]
aircraft["owners"] = sorted(owners, key=lambda d: d["id"])
return aircraft