#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import time
from pyRRD_Redis import RRD_redis, StepAddFunc
import json
import socket
import urllib.parse
import urllib.request
import urllib.error


# some class
class Dweet(object):
    HEADS = {'Content-type': 'application/json'}
    UPDATE_URL = 'https://dweet.io/dweet/for/'
    GET_URL = 'https://dweet.io/get/latest/dweet/for/'

    def __init__(self, dweet_id):
        self.dweet_id = str(dweet_id)
        # set timeout to 10 seconds (default is none)
        socket.setdefaulttimeout(10)

    def update(self, fields):
        # do update request
        try:
            request = urllib.request.Request(Dweet.UPDATE_URL + self.dweet_id, headers=Dweet.HEADS,
                                             data=json.dumps(fields).encode('utf-8'))
            urllib.request.urlopen(request)
            return True
        except urllib.error:
            return False

    def get(self):
        # do get request
        try:
            request = urllib.request.Request(Dweet.GET_URL + self.dweet_id, headers=Dweet.HEADS)
            response = urllib.request.urlopen(request)
            data = response.read().decode('utf-8')
            return json.loads(data)
        except urllib.error:
            return None

# init
rrd = RRD_redis(size=8640, step=10.0, add_func=StepAddFunc.avg)
dw = Dweet(dweet_id='0d328b86-fcba-469e-a31a-adfad51be68a')

# main loop
while True:
    try:
        # get dweet json msg as dict
        d = dw.get()['with'][0]['content']
        # setRRD for all dweet floats vars
        for k in d:
            rrd.name = k
            try:
                rrd.add_step(float(d[k]))
            except ValueError:
                pass
    except (IndexError, TypeError, KeyError):
        pass
    # wait for next loop
    time.sleep(10.0)

@@ -1284,68 +1284,14 @@
 %0Arrd
- = RRD_redis(size=8640, step=10.0, add_func=StepAddFunc.avg)
+s = %7B%7D
 %0Adw 
@@ -1516,17 +1516,24 @@
     for 
-k
+var_name
  in d:%0A 
@@ -1550,17 +1550,83 @@
  rrd
-.name = k
+s%5Bvar_name%5D = rrds.get(var_name, RRD_redis(var_name, size=8640, step=10.0))
 %0A   
@@ -1658,16 +1658,27 @@
      rrd
+s%5Bvar_name%5D
 .add_ste
@@ -1687,17 +1687,24 @@
 float(d%5B
-k
+var_name
 %5D))%0A    

fix dweet example