Difference between revisions of "SpaceAPI"

From Fixme.ch
Jump to: navigation, search
Line 15: Line 15:
  
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
#!/usr/bin/env python2
+
#!/usr/bin/env python3
import urllib
+
import urllib.request
 
try:
 
try:
 
import json
 
import json
 
except ImportError:
 
except ImportError:
 
import simplejson as json
 
import simplejson as json
 
 
URL = "https://fixme.ch/cgi-bin/spaceapi.py"
 
URL = "https://fixme.ch/cgi-bin/spaceapi.py"
try:
+
 
con = urllib.urlopen(URL)
+
def get_json():
hs_json = json.load(con)
+
try:
except IOError:
+
con = urllib.request.urlopen(URL)
print("An error occured sorry")
+
content = con.read()
exit(1)
+
hs_json = json.loads(content.decode('utf8'))
if hs_json['open']:
+
return hs_json
print("OPENENENENEN")
+
except IOError:
print("status is : %(status)s" %hs_json)
+
print("A network error occured sorry")
print("the changes happens at %(lastchange)s" % hs_json)
+
exit(1)
print("The duration is %(duration)s hours" % hs_json)
+
except ValueError:
else:
+
print("Malformatted json")
print("The hackerspace is closed")
+
 
 +
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")
 +
 
 +
print_info(get_json())
 +
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  

Revision as of 18:21, 2 May 2012

Description

Components

  • Sources on Github
  • Python script on fixme.ch that serves the API
  • Perl script that put the status in a Mysql DB (from the RFID_Doorlock or from a computer)
  • A Drupal module to show the status on the main website

Dummy script to lookup the status

#!/usr/bin/env python3
import urllib.request
try:
	import json
except ImportError:
	import simplejson as 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")
 
print_info(get_json())

Participant