-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateTables.py
85 lines (73 loc) · 3.57 KB
/
createTables.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
83
84
85
import configparser
import datetime
import json
import logging
import urllib.request as request
from datetime import datetime
class DateTimeEncoder(json.JSONEncoder):
# Special encoder for JSONEncoder, so that DateTime objects can be serialized.
def default(self, o):
if isinstance(o, datetime):
return o.isoformat()
return json.JSONEncoder.default(self, o)
config = configparser.ConfigParser()
config.read('config.ini')
LOG_FORMAT = "%(name)s %(levelname)s %(asctime)s - %(message)s"
logging.basicConfig(filename="log.Log",
level=logging.DEBUG,
format=LOG_FORMAT,
filemode='w')
logger = logging.getLogger(__name__)
logger.info("Logger is at level: {}".format(logger.level))
def create_vehicles():
"""Gets the JSON of 1000 vehicles, randomly generated, then outputs to a file."""
mockaroo_request = request.urlopen(
'https://my.api.mockaroo.com/vehicles.json?key={}'.format(config['DEFAULT']['MOCKAROO_API_KEY']))
if mockaroo_request.getcode() == 200:
data = mockaroo_request.read()
json_data = json.loads(data.decode('utf-8'))
logger.debug("First row of data, vehicles: {}".format(json_data[0]))
for item in json_data:
item['nextService'] = 5000
item['code'] = ''
with open('vehicles.json', 'w') as f:
f.write("[")
for item in json_data[:len(json_data) - 1]:
f.write("%s,\n" % json.dumps(item))
f.write("%s]" % json.dumps(json_data[len(json_data) - 1]))
else:
logger.fatal("createVehicles: request returned {}".format(mockaroo_request.getCode()))
def create_customer():
"""Gets the JSON of 1000 customers, randomly generated, then outputs to a file."""
mockaroo_request = request.urlopen(
'https://my.api.mockaroo.com/customers.json?key={}'.format(config['DEFAULT']['MOCKAROO_API_KEY']))
if mockaroo_request.getcode() == 200:
data = mockaroo_request.read()
json_data = json.loads(data.decode('utf-8'))
logger.debug("First row of data, customers: {}".format(json_data[0]))
print(json_data[0])
with open('customers.json', 'w') as f:
f.write("[")
for item in json_data[:len(json_data) - 1]:
f.write("%s,\n" % json.dumps(item))
f.write("%s]" % json.dumps(json_data[len(json_data) - 1]))
else:
logger.fatal("createVehicles: request returned {}".format(mockaroo_request.getCode()))
def create_reservations():
"""Gets the JSON of 1000 of reservations, randomly generated, then outputs to a file."""
mockaroo_request = request.urlopen(
'https://my.api.mockaroo.com/reservations.json?key={}'.format(config['DEFAULT']['MOCKAROO_API_KEY']))
if mockaroo_request.getcode() == 200:
data = mockaroo_request.read()
json_data = json.loads(data.decode('utf-8'))
for item in json_data:
item['start_date'] = datetime.strptime(item['start_date'], '%Y-%m-%d %H:%M:%S')
item['end_date'] = datetime.strptime(item['end_date'], '%Y-%m-%d %H:%M:%S')
logger.debug("First row of data, reservations: {}".format(json_data[0]))
with open('reservations.json', 'w') as f:
f.write("[")
for item in json_data[:len(json_data) - 1]:
f.write("%s,\n" % json.dumps(item, cls=DateTimeEncoder))
f.write("%s]" % json.dumps(json_data[len(json_data) - 1], cls=DateTimeEncoder))
else:
logger.fatal("createVehicles: request returned {}".format(mockaroo_request.getCode()))