forked from GateHouseMedia/power-outages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dukereport.py
105 lines (86 loc) · 3.6 KB
/
dukereport.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python3
# coding: utf-8
import requests
import simplejson as json
import dukejsoner as jsoner
import config
import base64
from collections import OrderedDict
import sqlite3
import datetime
configurl = "https://outagemap.duke-energy.com/config/config.prod.json"
baseurl = "https://cust-api.duke-energy.com/outage-maps/v1/counties?jurisdiction="
databasefile = 'dukeoutages.db'
jurisdictionswanted = [
"DEF", # Duke Energy Florida
"DEC", # Duke Energy Carolinas
"DEI", # Duke Energy Indiana
"DEM" # Duke Energy Ohio and Kentucky, which somehow have an M in them.
]
r = requests.get(configurl)
configjson = json.loads(r.content)
# So the auth string we need for the countyurl is a base64 concatentation of two JSON values separated by a colon,
# prefixed by "Basic "
# For this, Thomas Wilburn is owed many frosty beverages
authstring = bytes("Basic ", "utf-8") + base64.b64encode(bytes(f"{configjson['consumer_key_emp']}:{configjson['consumer_secret_emp']}", 'utf-8'))
cookies = r.cookies
headers = {
'Accept': 'application/json, text/plain, */*',
# 'Authorization': 'Basic WVFKRjNFemZsMTAzYUpPZ0NIY3E2ajZmSWYwRW9TRWg6YXFuejNDNDg1VEVXOVB0cQ==',
'Origin': 'https://outagemap.duke-energy.com',
'Referer': 'https://outagemap.duke-energy.com/',
'Sec-Fetch-Mode': 'cors',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36'
}
headers['Authorization'] = authstring
masterdict = OrderedDict()
for jurisdiction in jurisdictionswanted:
r = requests.get(f"{baseurl}{jurisdiction}", headers=headers, cookies=cookies)
juridata = json.loads(r.content)
for nugget in juridata['data']:
line = OrderedDict()
state = nugget['state']
countyname = nugget['countyName']
accounts = nugget['customersServed']
outages = nugget['areaOfInterestSummary']['maxCustomersAffected']
if outages is None:
outages = 0
line['accounts'] = accounts
line['outages'] = outages
if state not in masterdict:
masterdict[state] = OrderedDict()
if countyname != "General office":
masterdict[state][countyname] = line
for state in masterdict:
totalaccounts = 0
totaloutages = 0
for county in masterdict[state]:
totalaccounts += masterdict[state][county]['accounts']
totaloutages += masterdict[state][county]['outages']
line = OrderedDict()
line['accounts'] = totalaccounts
line['outages'] = totaloutages
masterdict[state]['Statewide'] = line
for state in masterdict:
for county in masterdict[state]:
masterdict[state][county]['outpct'] = round(100*float(masterdict[state][county]['outages'])/float(masterdict[state][county]['accounts']), 1)
# lastupdated text, state text, countyname text, outages integer, accounts integer, outpct real
timestamp = datetime.datetime.now().strftime("%Y%m%d-%H%M")
results = []
for state in masterdict:
for county in masterdict[state]:
line = [timestamp, state, county]
for entry in ["outages", "accounts", "outpct"]:
line.append(masterdict[state][county][entry])
results.append(line)
conn = sqlite3.connect(databasefile)
c = conn.cursor()
# Execute on first run only
# c.execute('''drop table master;''')
# c.execute('''CREATE TABLE master (lastupdated text, state text, countyname text, outages integer, accounts integer, outpct real);''')
# conn.commit()
for row in results:
c.execute('insert into master values (?,?,?,?,?,?)', row)
conn.commit()
if config.WantJson:
jsoner.jsonme() # Execute jsoner