-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
69 lines (60 loc) · 2.08 KB
/
parser.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
from __future__ import print_function
import json
class Parser(object):
def __init__(self, filename):
self.properties = []
self.brokers = []
self.graphs = []
with open(filename) as data:
self.data = json.load(data)
def parse(self):
graph_counter = 1
prop_counter = 1
brok_counter = 1
for prop in self.data['results']:
broker = prop['broker']
prop['broker'] = brok_counter
stats = prop['stats']
del prop['stats']
for _, graph in stats.iteritems():
self.graphs.append({
"model": "properties.graph",
"pk": graph_counter,
"fields":{
"graph": graph,
"property": prop_counter
}
})
graph_counter+=1
del broker["id"]
self.brokers.append({
"model": "properties.broker",
"pk": brok_counter,
"fields": broker
})
brok_counter+=1
del prop['id']
del prop['type_id']
if not prop['openhousedate']:
prop['openhousedate'] = None
if not prop['openhouseduration']:
prop['openhouseduration'] = None
self.properties.append({
"model": "properties.property",
"pk": prop_counter,
"fields": prop
})
prop_counter+=1
def to_django_models(self):
with open('fixtures/brokers.json', 'w') as br:
json.dump(self.brokers, br)
with open('fixtures/propertiestest.json', 'w') as pr:
json.dump(self.properties[0:20], pr)
with open('fixtures/properties.json', 'w') as pr:
json.dump(self.properties, pr)
with open('fixtures/graphs.json', 'w') as gr:
json.dump(self.graphs, gr)
if __name__ == '__main__':
a = Parser("search.json")
a.parse()
a.to_django_models()