import time
import requests
import json
import geojson
from shapely.geometry import Polygon, LineString, Point
from shapely import wkt

import psycopg2
import psycopg2.extensions
from psycopg2.extras import Json
conn = psycopg2.connect(host='localhost', database='nws', user='nws', password='nws')
cursor = conn.cursor()

#Bounding box of CWA



url = 'https://wvdhsem.onerain.com/export/map/geojson/?method=sites&view=fb358463-0d34-42b9-b86e-32aee53d3fd2'

potato = ''
features = []
alldata = []
S = requests.Session()




response = json.loads(requests.get(url).text)
alltings = []
for p in response['features']:
  if p['properties']['system'] != 'USGS':
      if p['properties']['status'] == 1:
          sitetype = 'unknown'
          siteid = p['properties']['site_id']
          sitename = p['properties']['name']
          lon = p['geometry']['coordinates'][0]
          lat = p['geometry']['coordinates'][1]
          if "MET" in sitename: 
              sitetype = 'MET'
          if "Rain" in sitename:
              sitetype = 'Rain'

          tings = [siteid, sitename, lat, lon, sitetype]
          sql = 'INSERT INTO onerainsites (siteid, sitename, lat, lon, sitetype) VALUES (%s,%s,%s,%s,%s) ON CONFLICT DO NOTHING'
          cursor.execute(sql,tings)
conn.commit()
print(alltings)

cursor.close()
conn.close()

def del_none(d):
    for key, value in list(d.items()):
        if value is None:
            del d[key]
        elif isinstance(value, dict):
            del_none(value)
    return d  # For convenience





