#!/usr/bin/env python3

import sys
import time
import requests

from functools import partial
error = partial(print, file=sys.stderr)

def debug(*args, **kwargs):
	if not VERBOSE:
		return
	error(*args, **kwargs)


VERBOSE = False
TIMEOUT = 5.0


if len(sys.argv) < 2:
	error("Usage: %s [-v] URL..." % sys.argv[0])
	sys.exit(1)

args = sys.argv[1:]
if args[0] == "-v":
	VERBOSE = True
	args = args[1:]


for url in args:
	debug("connecting:", url)

	s = None
	response = requests.get(url, verify=False, timeout=TIMEOUT)
	if response.status_code != 200:
		error(response)
		print("UNKNOWN")
		continue

	d = response.json()
	try:
		s = "{value:0.3f}".format(**d)
	except (ValueError, TypeError):
		pass
	except KeyError as e:
		error(e)
		continue

	if not s:
		error(d)
		print("UNKNOWN")
		continue
	print(s)
