Skip to content

Instantly share code, notes, and snippets.

@anthonydouc
Last active April 4, 2023 06:49
Show Gist options
  • Save anthonydouc/ac0b4251d6dd0626280d50648f1dff01 to your computer and use it in GitHub Desktop.
Save anthonydouc/ac0b4251d6dd0626280d50648f1dff01 to your computer and use it in GitHub Desktop.
PTV API signature generator code - Python 3
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))
@jampola
Copy link

jampola commented Aug 12, 2020

JFC, thank you.

@anthonydouc
Copy link
Author

JFC, thank you.

You are welcome. whats JFC mean?

@jampola
Copy link

jampola commented Aug 14, 2020

Jesus Fucking Christ :)

Sorry, PTV’s explanation on how to calculate the signature truly sucked. I wasn’t far off though! Nonetheless, thanks mate, I owe you a beer.

@anthonydouc
Copy link
Author

Ah yep, it made literally no sense - leading me to post this here in the off chance others struggled and found it.

@jampola
Copy link

jampola commented Aug 16, 2020 via email

@kongwak
Copy link

kongwak commented Apr 4, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment