-
Notifications
You must be signed in to change notification settings - Fork 2
/
fplreport.py
68 lines (58 loc) · 2.27 KB
/
fplreport.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
# coding: utf-8
import requests
import config
import fpljsoner
import time
import datetime
import simplejson as json
import sqlite3
# Execute on first run only
# c.execute('''drop table master;''')
# c.execute('''CREATE TABLE master (lastupdated text, countyname text, outages integer, restored integer, affected integer, accounts integer, outpct integer);''')
pageurl = "https://www.fplmaps.com/data/storm-outages.js"
source = "storm-outages"
r = requests.get(pageurl)
old = r.content
new = r.content
counter = 0
while counter < 50 and old == new:
counter += 1
time.sleep(60)
r = requests.get(pageurl)
new = r.content
# print("Running cycle " + str(counter) + " with old " + old + " and new " + new)
if old != new or not InProduction:
print("Beginning comparison")
textpre = str(r.content)
textpre = textpre[textpre.find("{"):textpre.rfind("}")+1]
text = json.loads(textpre)
lastupdated = text['lastupdated']
results = []
text["counties"]["statewide"] = text["statewide"]
text["counties"]["statewide"]["name"] = "Statewide"
for county in text["counties"]:
countyname = text["counties"][county]["name"].replace("St Lucie", "St. Lucie").replace("St Johns", "St. Johns")
outages = text["counties"][county]["numberofoutages"]
restored = text["counties"][county]["numberofrestored"]
affected = text["counties"][county]["numberofaffected"]
accounts = text["counties"][county]["numberofaccounts"]
outpct = round(100*float(outages)/float(accounts), 1)
row = (lastupdated, countyname, outages, restored, affected, accounts, outpct, source)
results.append(row)
# c.executemany('INSERT INTO master VALUES (?,?,?,?,?,?,?)', results)
print("Writing results to database")
beer = "warm"
while beer != "cold":
try:
conn = sqlite3.connect('fploutages.db')
c = conn.cursor()
for row in results:
c.execute('insert into master values (?,?,?,?,?,?,?,?)', row)
print("Done writing results to database")
print("Committing")
conn.commit()
beer = "cold"
except:
time.sleep(.5) # Try the database access later
if config.WantJson:
fpljsoner.jsonme() # Execute jsoner