Difference between revisions of "SpaceAPI"

From Fixme.ch
Jump to: navigation, search
Line 18: Line 18:
 
#!/usr/bin/env python3
 
#!/usr/bin/env python3
 
import urllib.request
 
import urllib.request
try:
+
import json
import json
+
 
except ImportError:
+
import simplejson as json
+
 
URL = "https://fixme.ch/cgi-bin/spaceapi.py"
 
URL = "https://fixme.ch/cgi-bin/spaceapi.py"
  
Line 44: Line 42:
 
else:
 
else:
 
print("The hackerspace is closed")
 
print("The hackerspace is closed")
 
+
json = get_json()
print_info(get_json())
+
try:
 +
print_info(json)
  
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 22:27, 2 May 2012

Description

Components

Dummy script to lookup the status

#!/usr/bin/env python3
import urllib.request
import json
 
URL = "https://fixme.ch/cgi-bin/spaceapi.py"
 
def get_json():
	try:
		con = urllib.request.urlopen(URL)
		content = con.read()
		hs_json = json.loads(content.decode('utf8'))
		return hs_json
	except IOError:
		print("A network error occured sorry")
		exit(1)
	except ValueError:
		print("Malformatted json")
 
def print_info(hs_json):
	if hs_json['open']:
		print("OPENENENENEN")
		print("status is : {status}".format(**hs_json))
		print("the changes happens at {lastchange}".format(**hs_json))
		print("The duration is {duration} hours".format(**hs_json))
	else:
		print("The hackerspace is closed")
json = get_json()
try:
print_info(json)

Participant