-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathupdate_data.py
74 lines (64 loc) · 2.65 KB
/
update_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
73
74
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import json
import os
import urllib2
import sys
import datetime
base_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(base_dir)
def read_json(file_name):
with open(file_name, 'r') as input_file:
return json.load(input_file)
def write_json(file_name, content):
with open(file_name, 'w') as output_file:
json.dump(content, output_file, indent=4)
#api = 'http://127.0.0.1:10080/'
api_today = 'http://127.0.0.1:10080/today'
api = 'http://128.199.223.114:10080/'
data = read_json('data/data.json')
response = urllib2.urlopen(api);
try:
new_data = json.loads(response.read())
except e:
print e
sys.exit(1)
for name, reservoir in data.iteritems():
for reservoir_new in new_data['data']:
if name == reservoir_new['reservoirName']:
print name, reservoir['id']
reservoir['daliyInflow'] = reservoir_new['daliyInflow']
reservoir['daliyOverflow'] = reservoir_new['daliyOverflow']
if reservoir_new['baseAvailable'] != '--':
reservoir['baseAvailable'] = reservoir_new['baseAvailable'].replace(',', '')
try:
reservoir['daliyNetflow'] = float(reservoir_new['daliyOverflow']) -\
float(reservoir_new['daliyInflow'])
print reservoir['daliyNetflow']
except:
reservoir['daliyNetflow'] = '--'
response = urllib2.urlopen(api_today);
try:
new_data = json.loads(response.read())
except e:
print e
sys.exit(1)
for name, reservoir in data.iteritems():
for reservoir_new in new_data['data']:
if name == reservoir_new['reservoirName']:
print name, reservoir['id'], reservoir_new['immediateTime']
if reservoir_new['immediateStorage'] != '--':
reservoir['updateAt'] = reservoir_new['immediateTime']
reservoir['volumn'] = reservoir_new['immediateStorage'].replace(',', '')
if reservoir_new['immediatePercentage'] == '--':
reservoir['percentage'] = (float(reservoir['volumn']) / \
float(reservoir['baseAvailable'])) * 100
elif len(reservoir_new['immediatePercentage']) == 6:
reservoir['percentage'] = float(reservoir_new['immediatePercentage'][:-2])
else:
reservoir['percentage'] = float(reservoir_new['immediatePercentage'][:-1])
else:
reservoir['percentage'] = float(reservoir['percentage'])
now = datetime.datetime.now()
date = str(now).split(' ')[0].replace('-', '')
write_json('data/data' + date + str(now.hour) + '.json', data)