ePrivacy and GPDR Cookie Consent by Cookie Consent

Safeticket Python API script

Udgivet 2016-10-09 - Skrevet af Philip Sørensen

During my time as IT responsible for Student Society at Aalborg University we got a new webshop through Safeticket. This new webshop brought a lot of opportunities, but most importantly, for the first time in a long time it didn't crash at the beginning of the school year when people were buying tickets for the huge party held every year to welcome the new students. 4-5 thousand people attend this party, so it is quite a number of tickets being sold, easpecially during the last few days. For fun I wanted to make a script that played the Mario coin sound each time a ticket was sold, so I made this Python script.

Note that the method for playing the sound may be OS specific. I developed the script to run on Ubuntu using the program mpg321 to play sound. The sound I used can be found here: Mario coin sound.
# This is a script made to play a sound each time a ticket
# was sold to a huge party being held at AAU with around
# 4-5 thousand people. 
# Runs on Ubuntu with mpg321 installed.
# Information about the API accessed can be seen here:
# https://www.safeticket.dk/dokumentation/api/
# Author: Philip Sørensen	E-mail: script@ppsa.dk
import hashlib, json, os, subprocess, sys, time, urllib
import xmltodict

# Information needed for start query. 
# Removed due to security concerns. 
version = '1'
user = ''
events = ''
orderid = ''
sequence_number = ''
t1 = ''
t2 = ''
secret = ''
url = ''

while True:
	# Make the SHA256 hash required.
	string = version + ":" + user + ":" + events + ":"
	string = string + sequence_number + ":" + t1 + ":"
	string = string + t2 + ":" + secret
	sha = hashlib.sha256(string.encode('utf-8')).hexdigest()

	# Insert query parameters and get response.
	params = urllib.parse.urlencode({
		'version': version,
		'user': user,
		'events': events,
		'sequence_number': sequence_number,
		't1': '',
		't2': '',
		'sha': sha
	params = params.encode('ascii')
	req = urllib.request.Request(url, params)
	with urllib.request.urlopen(req) as response:
		response = response.read()

	# I am not a fan of XML, so I convert to JSON. 
	json_object = json.dumps(xmltodict.parse(response))
	json_object = json.loads(json_object)

	# Check if the last order is new and play a sound if it is. 
	try: # Try except because JSON response may be null.
		num = len(json_object['orders']['order']) - 1
		lastorder = json_object['orders']['order'][num]
		if lastorder['orderid'] > orderid:
			orderid = lastorder['orderid']
			print(lastorder['time'] + ' ' + lastorder['name'])
			with open(os.devnull, 'wb') as devnull:
					subprocess.check_call(['mpg321', 'mario.mp3'],
						stdout=devnull, stderr=subprocess.STDOUT)


Der er ingen kommentarer.

Tilføj kommentar