#!/usr/bin/python3

import netifaces
import urllib.request
import json
import os



API_URL			= "http://fotobox.online:8888/api"
BASE_PATH		= "/var/lib/photobox"
MAC_INTERFACE	= "eth0"
FOTOBOX_KEY		= "eDAOMohrcJY3"


CONFIG_PATH	= "%s/config.json" % (BASE_PATH, )



def getHwAddr(ifname):
	return netifaces.ifaddresses(ifname)[netifaces.AF_LINK][0]["addr"]




mac_address = getHwAddr(MAC_INTERFACE)

url = "%s/fotobox/config/%s/%s/" % (API_URL, mac_address, FOTOBOX_KEY)
print(url)
reply = urllib.request.urlopen(url).read()
reply = reply.decode("UTF-8")
reply = json.loads(reply)
reply = json.dumps(reply, sort_keys=True, indent=4)
print(reply)

try:
	os.makedirs(BASE_PATH)
except OSError:
	pass

open(CONFIG_PATH, "w+").write(reply)
