import requests
import polyline
import json
import psycopg2
import psycopg2.extensions
from datetime import datetime, timezone
from geojson import Point, Feature, FeatureCollection, dump
import re

conn = psycopg2.connect(host='localhost', database='nws', user='nws', password='nws')
cursor = conn.cursor()
proxies = {"http":"http://nws:nws@localhost:9000"}

aepohmeta = "http://outagemap.aepohio.com.s3.amazonaws.com/resources/data/external/interval_generation_data/metadata.json"
aepwvmeta = "http://outagemap.appalachianpower.com.s3.amazonaws.com/resources/data/external/interval_generation_data/metadata.json"
firstpowerwvmeta = "https://s3.amazonaws.com/outages.sc4.firstenergycorp.com/resources/data/mdwv/interval_generation_data/metadata.json"
aepkymeta = 'http://outagemap.kentuckypower.com.s3.amazonaws.com/resources/data/external/interval_generation_data/metadata.json'



aepwvbase = "http://outagemap.appalachianpower.com.s3.amazonaws.com/resources/data/external/interval_generation_data/"
aepohbase = "http://outagemap.aepohio.com.s3.amazonaws.com/resources/data/external/interval_generation_data/"
firstpowerwvbase = "https://s3.amazonaws.com/outages.sc4.firstenergycorp.com/resources/data/mdwv/interval_generation_data/"
aepkybase = 'http://outagemap.kentuckypower.com.s3.amazonaws.com/resources/data/external/interval_generation_data/'
graysoncounty = 'https://outages.graysonrecc.com/data/boundaries.json'
#flemingjson = 'https://outage.fme.coop/data/boundaries.json'

#buckeye rec
#https://outage.buckeyerec.coop/maps/OutageWebMap/maps/GWT.rpc
#washington
#https://weci.ebill.coop/woViewer/MapWiseWeb/GWT.rpc
allcountyoutages = []

S = requests.Session()




def fleming():
 state = 'KY'
 company = 'FLEM'
 temp = S.get(flemingjson)
 tempdata = json.loads(temp.text)
 for j in tempdata[0]['boundaries']:
  outageinfo = j.get('customersOutNow'),j.get('customersServed'),j.get('name'),state,company
  allcountyoutages.append(outageinfo)





def bigsandy():
 state = 'OH'
 company = 'BS'
 temp = S.get('http://outagemap.bigsandyrecc.com/data/boundaries.json')
 tempdata = json.loads(temp.text)
 for j in tempdata[0]['boundaries']:
  outageinfo = j.get('customersOutNow'),j.get('customersServed'),j.get('name'),state,company
  allcountyoutages.append(outageinfo)




def southcentralpower():
 company = 'SCP'
 url = 'http://outage.southcentralpower.com/data/boundaries.json'
 Sp = requests.Session()
# Sp.proxies.update(proxies)
 temp = Sp.get(url).text
 tempdata = json.loads(temp)
 state = 'OH'

 for j in tempdata[0]['boundaries']:
   outageinfo = j.get('customersOutNow'),j.get('customersServed'),j.get('name'),state,company
   allcountyoutages.append(outageinfo)


def ku_get_url():
 url = 'https://stormcenter.lge-ku.com/reports/1d6f7e68-e192-43c1-bfdc-d809333d8e40'
 r = requests.get(url).text
 x = re.search(r"instanceId: '(.*?)',",r)
 urlcom = x.group(1)
 urlcom = 'https://kubra.io/stormcenter/api/v1/stormcenters/' + urlcom + '/views/a6cee9e4-312b-4b77-9913-2ae371eb860d/currentState?preview=false'
 stuff = S.get(urlcom)
 jsonstuff = json.loads(stuff.text)
 interval_data = jsonstuff.get('data').get('interval_generation_data')
 urlcom = 'https://kubra.io/' + interval_data + '/public/reports/1d6f7e68-e192-43c1-bfdc-d809333d8e40_report.json'
 return urlcom


def county_json(meta,url,jsonname):
  metainfo = json.loads(S.get(meta).text)
  metadir = metainfo['directory']
  url = url + metadir + jsonname
  outage = S.get(url)
  return outage



def ku():
 ku = []
 url = ku_get_url()
 data = S.get(url).text
 tempdata = json.loads(data)
 temp = tempdata['file_data']['areas'][2]['areas'][0]['areas']
 temp1 = tempdata['file_data']['areas'][2]['areas'][1]['areas']
 temp2 = tempdata['file_data']['areas'][1]['areas'][0]['areas']

 for i in temp:
  ku.append(i)
 for i in temp1:
  ku.append(i)
 for i in temp2:
  ku.append(i)
 for o in ku:
  outageinfo = o['cust_a']['val'],o['cust_s'],o['name'].capitalize(),o['state'],o['utility']
  allcountyoutages.append(outageinfo)


def grayson():
 company = 'GRE'
 outage = S.get(graysoncounty)
 if outage.headers.get('Content-Type').startswith('application/json'):
  tempdata = json.loads(outage.text)
  state = 'KY'
  for j in tempdata[0]['boundaries']:
   outageinfo = j.get('customersOutNow'),j.get('customersServed'),j.get('name'),state,company
   allcountyoutages.append(outageinfo)


def aep_county_vawv(meta,url,jsonname):
 company = 'AEP'
 outage = county_json(meta,url,jsonname)
 if outage.headers.get('Content-Type').startswith('application/octet-stream'):
  tempdata = json.loads(outage.text)
  state = 'WV'
  for j in tempdata['file_data']['areas'][0]['areas'][2]['areas']:
   outageinfo  = j.get('cust_a').get('val'), j.get('cust_s'), j.get('area_name'), state, company
   allcountyoutages.append(outageinfo)
  state = 'VA'
  for j in tempdata['file_data']['areas'][0]['areas'][1]['areas']:
   outageinfo = j.get('cust_a').get('val'), j.get('cust_s'), j.get('area_name').capitalize(), state, company
   allcountyoutages.append(outageinfo)

def aep_county_oh(meta,url,jsonname):
 company = 'AEP'
 state = 'OH'
 outage = county_json(meta,url,jsonname)
 tempdata = json.loads(outage.text)
 for j in tempdata['file_data']['areas'][0]['areas'][0]['areas']:
  outageinfo = j.get('cust_a').get('val'), j.get('cust_s'), j.get('area_name').capitalize(), state, company
  allcountyoutages.append(outageinfo)

def aep_county_ky(meta,url,jsonname):
 company = 'AEP'
 state = 'KY'
 outage = county_json(meta,url,jsonname)
 tempdata = json.loads(outage.text)
 for j in tempdata['file_data']['areas'][0]['areas'][0]['areas']:
  outageinfo = j.get('cust_a').get('val'), j.get('cust_s'), j.get('area_name').capitalize(), state, company
  allcountyoutages.append(outageinfo)

def firstenergy_county(meta,url,jsonname):
 company = 'FE'
 state = 'WV'
 outage = county_json(meta,url,jsonname)
 if outage.headers.get('Content-Type').startswith('application/octet-stream'):
  tempdata = json.loads(outage.text)
  for j in tempdata['file_data']['areas'][0]['areas'][0]['areas']:
   outageinfo = j.get('cust_a').get('val'), j.get('cust_s'), j.get('area_name').capitalize(),state, company
   allcountyoutages.append(outageinfo)


aep_county_vawv(aepwvmeta,'https://d2oclp3li76tyy.cloudfront.net/resources/data/external/interval_generation_data/','/report_county.json')
firstenergy_county('https://s3.amazonaws.com/outages.sc4.firstenergycorp.com/resources/data/mdwv/interval_generation_data/metadata.json','https://s3.amazonaws.com/outages.sc4.firstenergycorp.com/resources/data/mdwv/interval_generation_data/','/report_county_ctv_wv.json')
aep_county_oh(aepohmeta,'http://outagemap.aepohio.com.s3.amazonaws.com/resources/data/external/interval_generation_data/','/report_county.json')
aep_county_ky(aepkymeta, aepkybase, '/report_county.json')
grayson()
ku()
southcentralpower()
bigsandy()



current_timestamp = str(datetime.utcnow())
for i in allcountyoutages:
 sql = 'insert into countyoutages (outages, served, county, state, update, company) values (%s, %s, %s, %s, %s, %s)'
 val = (i[0], i[1], i[2], i[3], current_timestamp, i[4])
 cursor.execute(sql,val)
conn.commit()

cursor.execute('update countyoutages set cwa = county.cwa from county where county.countyname = countyoutages.county and county.state = countyoutages.state and countyoutages.cwa is null')

conn.commit()

#cursor.execute("delete from countyoutages where cwa != 'RLX'")
cursor.execute("delete from countyoutages where cwa is null")
cursor.execute("delete from countyoutages where update < now () - interval '365 days'")
conn.commit()








#print(allcountyoutages)

