diff options
author | yvesf <yvesf-git@xapek.org> | 2011-09-04 00:14:56 +0200 |
---|---|---|
committer | yvesf <yvesf-git@xapek.org> | 2011-09-04 00:25:57 +0200 |
commit | 651aebaaaa33883269a17f9c515694472e5ae29d (patch) | |
tree | 0283972516f879f8368f2247fe668f9a7d611c39 /bin | |
parent | 0ad590fcb3c4749335716ad1d16442a3fef2bb32 (diff) | |
download | ebus-alt-651aebaaaa33883269a17f9c515694472e5ae29d.tar.gz ebus-alt-651aebaaaa33883269a17f9c515694472e5ae29d.zip |
wettermichel datenimport
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/wettermichel.py | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/bin/wettermichel.py b/bin/wettermichel.py new file mode 100755 index 0000000..b7baaf8 --- /dev/null +++ b/bin/wettermichel.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python +# -*- coding:utf8 -*- +import httplib +from StringIO import StringIO + +from lxml import objectify + +from sqlalchemy import create_engine, text +from sqlalchemy.ext.sqlsoup import SqlSoup + +soup = SqlSoup( + create_engine("postgresql://ebus:ebus@10.2.2.26:5432/ebus") ) + +def read(): + conn = httplib.HTTPConnection("www.wettermichel.de") + conn.request("GET", "/weatherdata/params3.xml") + result = conn.getresponse() + assert result.status == 200, 'Site answered with Status 200' + + xml = objectify.parse( result ) + conn.close() + return xml + +def get_sensor_id(sensor_name): + try: + sensor_id = soup.sensor.filter(soup.sensor.name == sensor_name).one() + return sensor_id.id + except: + conn = soup.connection() + conn.execute(text("""INSERT INTO sensor(name) VALUES (:name)"""), + name = sensor_name) + soup.commit() + return sensor_id = soup.sensor.filter(soup.sensor.name == sensor_name).one().id + +def insert(sensor_name,value_int=None, value_float=None, value_string=None): + sql = text("""INSERT INTO value( + timestamp, sensor_id, type, value_int, value_float, value_string) + VALUES ( + NOW(), :sensor_id, :type, :value_int, :value_float, :value_string)""") + conn = soup.connection() + ttype = "int" and value_int != None or \ + "float" and value_float != None or \ + "string" and value_string != None or \ + None + assert ttype != None, 'ttype is set' + conn.execute(sql, sensor_id=get_sensor_id(sensor_name), + type=ttype, + value_int=value_int, + value_float=value_float, + value_string=value_string) + soup.commit() + +############### + +xml = read() +for keyval in xml.xpath("/data/*"): + if keyval.tag == "entry": + continue + + if isinstance(keyval, objectify.IntElement): + insert("de.wettermichel.%s"%keyval.tag, + value_int=int(keyval.text)) + elif isinstance(keyval, objectify.FloatElement): + insert("de.wettermichel.%s"%keyval.tag, + value_float=float(keyval.text)) + elif isinstance(keyval, objectify.StringElement): + insert("de.wettermichel.%s"%keyval.tag, + value_string=keyval.text) + else: + print "ignore other: %s %s" % (keyval.tag, keyval.__class__) + + |