Last active
April 4, 2023 06:49
-
-
Save anthonydouc/ac0b4251d6dd0626280d50648f1dff01 to your computer and use it in GitHub Desktop.
PTV API signature generator code - Python 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from hashlib import sha1 | |
import hmac | |
def getUrl(request): | |
devId = 1000000 | |
key = bytes('mykey','UTF-8') | |
request = request + ('&' if ('?' in request) else '?') | |
raw = bytes(request+'devid={0}'.format(devId),'UTF-8') | |
hashed = hmac.new(key, raw, sha1) | |
signature = hashed.hexdigest() | |
raw = str(raw,'UTF-8') | |
return 'http://timetableapi.ptv.vic.gov.au/'+raw+'&signature={1}'.format(devId, signature),'UTF-8' | |
request_url = '/v3/routes' | |
print(getUrl(request_url)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this, I was really stuck.
One comment is that you can change line 14 to remove reference to "devID" as you already deal with it in line 9. eg
return 'http://timetableapi.ptv.vic.gov.au'+raw+'&signature={0}'.format(signature)
For me this removes a Type warning.