-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_data.py
72 lines (61 loc) · 2.12 KB
/
import_data.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
import dateutil.parser
from flask import json
import sqlalchemy
from app import Airport, Flight
from application import Application
from db import db
app = Application()
def import_airports():
data = json.loads(open('import_data/airports_data.json').read())
for line in data:
try:
airport = Airport(
name=line[u'name'],
code=line[u'iata']
)
db.session.add(airport)
db.session.commit()
print 'imported', line[u'iata']
except sqlalchemy.exc.IntegrityError:
db.session.rollback()
print 'couldnt importj', line[u'iata']
# u'status': 1,
# u'name': u'Yining Airport',
# u'iata': u'YIN',
# u'lon': u'81.33144',
# u'iso': u'CN',
# u'lat': u'43.952',
# u'type': u'airport',
# u'continent': u'AS',
# u'size': u'small'
def import_routes():
data = json.loads(open('import_data/flight_data.json').read())
for line in data:
try:
print line['destination'], line['departure']
destination = Airport.query.filter_by(code=line['destination']).one()
departure = Airport.query.filter_by(code=line['departure']).one()
flight = Flight(
destination_airport=destination,
departure_airport=departure,
departure_time=dateutil.parser.parse(line['time']),
code=line['flight']
)
db.session.add(flight)
db.session.commit()
# print 'imported destination, departure, line
print 'EI VIRHE'
except sqlalchemy.orm.exc.NoResultFound:
print 'VIRHE'
# airport = Airport(
# name=line[u'name'],
# code=line[u'iata']
# )
# db.session.add(airport)
# u'destination': u'LEQ',
# u'flight': u'5Y* 8',
# u'departure': u'ISC',
# u'time': u'2015-11-06T06:54:00.000Z'
with app.app_context():
import_airports()
import_routes()