I have python 2.7 and I have weather script which returns temperature infos, I would like to implement this script into PostgreSQL. I always got this error: DETAIL: SyntaxError: invalid syntax (<string>, line 10)
code:
CREATE OR REPLACE FUNCTION GetWeather(lon float, lat float)
RETURNS float
AS $$
import urllib2
import simplejson as json
data = urllib2.urlopen(
"http://api.openweathermap.org/data/2.1/find/station?lat=%s&lon=%s&cnt=1"% (lat, lon))
js_data = json.load(data)
if js_data['cod'] == '200':
if int(js_data['cnt'])>0:
station = js_data['list'][0]
print 'Data from weather station %s' %station['name']
if 'main' in station:
if 'temp' in station['main']:
temperature = station['main']['temp'] - 273.15
else:temperature = None
else:temperature = None
return temperature
$$ LANGUAGE plpythonu;
I tried this version too, but its not working
CREATE OR REPLACE FUNCTION GetWeather(lon float, lat float)
RETURNS float
AS $$
import urllib2
import simplejson as json
def get_temp(lat, lon):
data = urllib2.urlopen(
"http://api.openweathermap.org/data/2.1/find/station?lat=%s&lon=%s&cnt=1"% (lat, lon))
js_data = json.load(data)
try:
return js_data['list'][0]['main']['temp']
except (KeyError, IndexError):
return None
$$ LANGUAGE plpythonu;