Commit 94c051dd authored by Karl Fleischmann's avatar Karl Fleischmann

Add data-interface-module

* Outsource reading from URL
* Outsource loading from json-data
parent cc564deb
# -*- coding: utf-8 -*-
import os
import json
from urllib import request
import hc_head
class Data(object):
"""Class representing a data-interface for hack_count."""
def __init__(self, var=request.urlopen("http://hack-hro.de/api/users")):
"""Initialize values and create necessary dirs."""
#### fetch usercount
# fetch data from server
self.var = var
# create data-objects by decoding the json
self.json_string = self.var.read()
self.json_dict = json.loads(self.json_string.decode('ascii'))
self.device_count = self.json_dict['deviceCount']
self.users = self.json_dict['users']
# create picture folder, if not existant
if not os.path.exists(hc_head.CACHE_PICTURE_FOLDER):
os.makedirs(hc_head.CACHE_PICTURE_FOLDER)
# fetch avatar picture, if not existant
if not os.path.isfile(hc_head.DEFAULT_CACHE_PICTURE):
request.urlretrieve("http://hack-hro.de/avatar/default.png",
hc_head.DEFAULT_CACHE_PICTURE)
def update(self,
var=request.urlopen("http://hack-hro.de/api/users")):
"""Update values, if required."""
# replace var, if not already matching
if var != self.var:
self.var = var
# create data-objects by decoding the json
self.json_string = self.var.read()
self.json_dict = json.loads(self.json_string.decode('ascii'))
self.device_count = self.json_dict['deviceCount']
self.users = self.json_dict['users']
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment