Skip to content

Commit 461e445

Browse files
committed
initial submit
1 parent e165ea1 commit 461e445

4 files changed

Lines changed: 134 additions & 0 deletions

File tree

db.sqlite3

0 Bytes
Binary file not shown.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
Loads data from `departures.json` into the Departure model
3+
"""
4+
5+
import json
6+
from django.db import migrations, models
7+
8+
def populate_departures(apps, schema_editor):
9+
Departure = apps.get_model('departures', 'Departure')
10+
11+
with open('departures.json') as json_data:
12+
data = json.load(json_data)
13+
for d in data:
14+
departure = Departure(
15+
name=d['name'],
16+
start_date=d['start_date'],
17+
finish_date=d['finish_date'],
18+
category=d['category']
19+
)
20+
departure.save()
21+
22+
23+
class Migration(migrations.Migration):
24+
25+
dependencies = [
26+
('departures', '0001_initial'),
27+
]
28+
29+
operations = [
30+
migrations.RunPython(populate_departures),
31+
]

fetch_departures.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"""
2+
Solution for gadventures/python-api-challenge
3+
4+
This module fetches departures data from an api server,
5+
filters by date and category, and then writes the filtered values
6+
to a csv file.
7+
8+
Submitted by: Tyler Hunt
9+
Email: trphunt@tylerhunt.solutions
10+
Date: March 2018
11+
"""
12+
from datetime import datetime
13+
import csv
14+
import requests
15+
16+
######## Fetch Data ########
17+
18+
URL = 'http://localhost:8000/departures'
19+
HEADERS = {"Accept": "application/json"}
20+
21+
def fetch_departures(output_list, url, headers):
22+
"""
23+
Recursively loads pages of departures into list
24+
until `next_url` is None.
25+
"""
26+
response = requests.get(url, headers)
27+
if response.status_code == 200:
28+
data = response.json()
29+
for result in data['results']:
30+
output_list.append(result)
31+
32+
next_url = data['next']
33+
if next_url is None:
34+
return None
35+
else:
36+
fetch_departures(output_list, next_url, headers)
37+
else:
38+
print('[!] HTTP {0} calling [{1}]'.format(response.status_code, url))
39+
return None
40+
return output_list
41+
42+
DEPARTURES = fetch_departures([], URL, HEADERS)
43+
44+
45+
####### Filter Data #######
46+
47+
def filter_departures(departures, pivot_date, category):
48+
"""
49+
Return departures with `start_date` later than `pivot_date`
50+
and matching the given `category`.
51+
"""
52+
output = []
53+
for departure in departures:
54+
start_date = datetime.strptime(departure['start_date'], '%Y-%m-%d')
55+
cat = departure['category']
56+
if start_date > pivot_date and cat == category:
57+
output.append(departure)
58+
return output
59+
60+
FILTERED_DEPARTURES = filter_departures(DEPARTURES, datetime(2018, 6, 1), 'Adventurous')
61+
62+
63+
####### Write to CSV #######
64+
65+
def write_departures_to_csv(departures):
66+
"""
67+
Write departures to csv file with title-cased headers.
68+
"""
69+
with open('filtered_departures.csv', 'w') as csvfile:
70+
fieldnames = departures[0].keys()
71+
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
72+
73+
headers = [x.title().replace('_', ' ') for x in list(fieldnames)]
74+
writer.writer.writerow(headers)
75+
for departure in departures:
76+
writer.writerow(departure)
77+
78+
write_departures_to_csv(FILTERED_DEPARTURES)

filtered_departures.csv

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Name,Start Date,Finish Date,Category
2+
New Zealand Encompassed,2018-08-31,2018-09-10,Adventurous
3+
Cambodia Overland,2018-08-31,2018-09-10,Adventurous
4+
Peru Overland,2018-08-31,2018-09-10,Adventurous
5+
Vietnam Safari,2018-08-31,2018-09-10,Adventurous
6+
Brazil Trek,2018-08-31,2018-09-10,Adventurous
7+
Cambodia Multisport,2018-08-31,2018-09-10,Adventurous
8+
Morocco Safari,2018-08-31,2018-09-10,Adventurous
9+
Galapagos Encompassed,2018-08-31,2018-09-10,Adventurous
10+
Galapagos Discovery,2018-08-31,2018-09-10,Adventurous
11+
New Zealand Adventure,2018-08-31,2018-09-10,Adventurous
12+
Galapagos Trek,2018-08-31,2018-09-10,Adventurous
13+
Brazil Encompassed,2018-08-31,2018-09-10,Adventurous
14+
Kenya Express,2018-08-31,2018-09-10,Adventurous
15+
Galapagos Discovery,2018-08-31,2018-09-10,Adventurous
16+
Sri Lanka Adventure,2018-08-31,2018-09-10,Adventurous
17+
Brazil Adventure,2018-08-31,2018-09-10,Adventurous
18+
Brazil Overland,2018-08-31,2018-09-10,Adventurous
19+
Kenya Encompassed,2018-08-31,2018-09-10,Adventurous
20+
Morocco Discovery,2018-08-31,2018-09-10,Adventurous
21+
Vietnam Trek,2018-08-31,2018-09-10,Adventurous
22+
New Zealand Safari,2018-08-31,2018-09-10,Adventurous
23+
Vietnam Encompassed,2018-08-31,2018-09-10,Adventurous
24+
Ethopia Express,2018-08-31,2018-09-10,Adventurous
25+
Sri Lanka Safari,2018-08-31,2018-09-10,Adventurous

0 commit comments

Comments
 (0)