[wfrog] r966 committed - * WFRender -> wettercom: Updated renderer due to API changes....

4 views
Skip to first unread message

wf...@googlecode.com

unread,
Dec 30, 2014, 4:44:30 PM12/30/14
to wfrog-a...@googlegroups.com
Revision: 966
Author: commi1993
Date: Tue Dec 30 21:44:12 2014 UTC
Log: * WFRender -> wettercom: Updated renderer due to API changes.
* Now uses JSON as answer (yay!)
* Now uses POST instead GET (ok, it would accept both, but I use POST now)
* ! Password is no longer send as md5-hash. I contacted API-owner to
accept https-connection instead.
https://code.google.com/p/wfrog/source/detail?r=966

Modified:
/trunk
/trunk/wfrender/config/wfrender.yaml
/trunk/wfrender/renderer/wettercom.py

=======================================
--- /trunk/wfrender/config/wfrender.yaml Sat Aug 17 09:53:04 2013 UTC
+++ /trunk/wfrender/config/wfrender.yaml Tue Dec 30 21:44:12 2014 UTC
@@ -61,7 +61,7 @@
# SetUp: http://www.wetter.com/community/wetternetzwerk/admin/api/
# Please use for first setup test: True to test publishing. Check
logs!
#wettercom: !wettercom
- # username: YOUR_STATION_ID
+ # stationId: YOUR_STATION_ID
# password: YOUR_STATION_PASSWORD
# storage: !service
# name: storage
=======================================
--- /trunk/wfrender/renderer/wettercom.py Wed Oct 3 00:05:05 2012 UTC
+++ /trunk/wfrender/renderer/wettercom.py Tue Dec 30 21:44:12 2014 UTC
@@ -20,6 +20,7 @@
import sys
import time
import hashlib
+import json
from wfcommon.formula.base import LastFormula
from httplib import HTTPConnection
from urllib import urlencode
@@ -35,11 +36,11 @@

[ Properties ]

- username [string]:
- Your wetter.com username.
+ stationId [string]:
+ Your wetter.com station-ID.

password [string]:
- password - wfrog ALWAYS send your password md5-decrypted to
wetter.com!
+ Your station password

period [numeric]:
The update period in seconds.
@@ -51,7 +52,7 @@
true if test-publishing
"""

- username = None
+ stationId = None
password = None
publisher = None
storage = None
@@ -62,11 +63,11 @@

def render(self, data={}, context={}):
try:
- assert self.username is not None, "'wettercom.id' must be set"
+ assert self.stationId is not None, "'wettercom.stationId' must
be set"
assert self.password is not None, "'wettercom.password' must
be set"
assert self.period is not None, "'wettercom.period' must be
set"

- self.logger.info("Initializing Wetter.com (user %s)" %
self.username)
+ self.logger.info("Initializing Wetter.com (stationID %s)" %
self.stationId)

self.alive = True

@@ -78,62 +79,55 @@
accu.formulas = {'current': {
'temp' : LastFormula('temp'),
'hum' : LastFormula('hum'),
+ 'hum2' : LastFormula('hum2'),
'pressure' : LastFormula('pressure'),
'wind' : LastFormula('wind'),
'wind_deg' : LastFormula('wind_dir'),
'rain' : LastFormula('rain'),
- 'localtime' : LastFormula('localtime') } }
+ 'localtime' : LastFormula('localtime'),
+ 'utctime' : LastFormula('utctime'),
+ 'dew_point' : LastFormula('dew_point') } }

while self.alive:
try:
data = accu.execute()['current']['series']
+ self.logger.debug("Got data from accumulator: %s" %
data)
index = len(data['lbl'])-1

try:
- # try, if date is NoneType, if yes, we need to
wait for a new value in wfrog.csv
+ # If date is NoneType (see except for this try),
we need to wait for a new value in wfrog.csv

args = {
- 'benutzername': str(self.username),
- 'passwortmd5':
hashlib.md5(str(self.password)).hexdigest(),
- 'datum':
data['localtime'][index].strftime('%Y%m%d%H%M'),
- 'feuchtigkeit':
int(round(data['hum'][index])),
- 'temperatur':
str(data['temp'][index]).replace('.', ','),
- 'windrichtung':
int(round(data['wind_deg'][index])),
- 'windstaerke':
str(data['wind'][index]).replace('.', ','),
- 'luftdruck':
str(data['pressure'][index]).replace('.', ','),
- 'niederschlagsmenge':
str(data['rain'][index]).replace('.', ',')
+ 'sid' : 'wfrog',
+ 'id' : str(self.stationId),
+ 'pwd' : str(self.password),
+ 'dt' :
data['localtime'][index].strftime('%Y%m%d%H%M'),
+ 'dtutc' :
data['utctime'][index].strftime('%Y%m%d%H%M'), # we need both, dt (date)
and dtutc (date in UTC).
+ 'hu' :
int(round(data['hum'][index])),
+ 'te' :
str(data['temp'][index]),
+ 'dp' :
str(data['dew_point'][index]),
+ 'wd' :
int(round(data['wind_deg'][index])),
+ 'ws' :
str(data['wind'][index]),
+ 'pr' :
str(data['pressure'][index]),
+ 'pa' :
str(data['rain'][index])
}
-
+
if self.test:
- args['test'] = "true"
- self.logger.info('Running in test-mode!! The
data wont be stored.')
- else:
- # If test is not true, we must send
&test=false. If the GET string ends with rain-value, wetter.com returns an
error
- args['test'] = "false"
-
- self.logger.debug("Publishing wettercom
data: %s " % urlencode(args))
- response =
self._publish(args, 'www.wetterarchiv.de', '/interface/http/input.php')
-
- self.logger.debug('Server response: Code: %s Status: %s
API-Answer: %s' % response)
-
- # Split response to determine if the request was
ok or not.
- answer = response[2].split('=')
-
- allOk = False
- try:
- if (answer[1] == 'SUCCESS'):
- allOk=True
- except:
- if (answer[5] == 'SUCCESS'):
- allOk=True
+ args["test"] = "true"
+ self.logger.info('#!#!#!#!#!#!#!#!#!#!
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Running in test-mode!! The data wont be
stored. #!#!#!#!#!#!#!#!#!#!')

- if allOk:
- self.logger.info('Data published successfully!')
+ self.logger.debug("Publishing wettercom
data: %s " % args)
+ rawResponse =
self._publish(args, 'interface.wetterarchiv.de', '/weather/')
+ self.logger.debug('Server response: Code: %s
Status: %s API-Answer: %s' % rawResponse)
+
+ # Ok, now create an JSON-object
+ response = json.loads(rawResponse[2])
+
+ # With the new API, checking for any error is very
easy!
+ if (response["status"] == "success"):
+ self.logger.info('Data published
successfully!')
else:
- try:
- self.logger.error('Data publishing fails!
Response: %s' % answer[5])
- except:
- self.logger.error('Data publishing fails! Response: %s' % answer[2])
+ self.logger.error('Data publishing fails!
Code: %s | Description: %s' % response["errorcode"],
response["errormessage"])

except Exception, e:
if (str(e) == "'NoneType' object has no
attribute 'strftime'") or (str(e) == "a float is required"):
@@ -141,7 +135,6 @@
else:
self.logger.error('Got unexpected error. Retry
next run. Error: %s' % e)
raise
-
except Exception, e:
self.logger.exception(e)

@@ -155,20 +148,22 @@
self.alive = False

def _publish(self, args, server, uri):
-
- uri = uri + "?" + urlencode(args)

self.logger.debug('Connect to: http://%s' % server)
- self.logger.debug('GET %s' % uri)
+ self.logger.debug('POST %s' % uri)
+ self.logger.debug('... and the following data: %s' % urlencode(args))

conn = HTTPConnection(server)
if not conn:
raise Exception, 'Remote server connection timeout!'
+
+ headers =
{"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}

- conn.request("GET", uri)
+ conn.request("POST", uri, urlencode(args), headers)
conn.sock.settimeout(5.0)

http = conn.getresponse()
+ self.logger.debug("Header: %s" % http.getheaders())
data = (http.status, http.reason, http.read())
conn.close()
if not (data[0] == 200 and data[1] == 'OK'):
Reply all
Reply to author
Forward
0 new messages