mpaesold / solarvic

1 stars 1 forks source link

Fix URL-Open #26

Open CodeDetritus opened 2 years ago

CodeDetritus commented 2 years ago

For some reason urlopen from urllib.request was not working no my machine. I found an alternative library, "requests" It works. Details of library are here: https://pypi.org/project/requests/ I haven't quite got the extraction of the hourly ac working at present. It returns [0. 0. 0. ... 0. 0. 0.]

... but very very close.

import requests import json

from urllib.request import urlopen

import numpy as np

urlabc = "https://abc.com.au" urlmonthly = "https://developer.nrel.gov/api/pvwatts/v6.json?api_key=4c1dKvIg0bjmSTIIwhXReypiNBQCCU7gE5qbqTzz&lat=-37.8978&lon=145.0709&system_capacity=1&azimuth=0.0&tilt=30.0&array_type=1&module_type=1&losses=15" urlhourly = "https://developer.nrel.gov/api/pvwatts/v6.json?api_key=4c1dKvIg0bjmSTIIwhXReypiNBQCCU7gE5qbqTzz&lat=-37.8978&lon=145.0709&system_capacity=1&azimuth=0.0&tilt=30.0&array_type=1&module_type=1&losses=15&timeframe=hourly"

try: print('Checking connection to abc...') test = requests.get(urlabc) print('Connection to ABC OK.')

print('Checking connection to NREL...')
response = requests.get(urlhourly)
print('Connection to NREL OK.')
#print(response.json())
out = response.json() # json.loads(response.text)
print(np.array(out['outputs']['ac']))

except requests.exceptions.SSLError as err: print('SSL Error. Adding custom certs to Certifi store...') cafile = certifi.where() with open('certicate.pem', 'rb') as infile: customca = infile.read() with open(cafile, 'ab') as outfile: outfile.write(customca) print('That might have worked.')