Created
October 27, 2018 04:10
-
-
Save benformosa/4c2c5a6ebfe54e8646fe41d1315ad63b to your computer and use it in GitHub Desktop.
Check your Transport Canberra MyWay Card balance
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
#!/usr/bin/env python3 | |
"""Check your Transport Canberra MyWay Card balance""" | |
from bs4 import BeautifulSoup | |
import argparse | |
import datetime | |
import getpass | |
import requests | |
def myway_balance(serialnumber, day, month, year, passwd): | |
data = { | |
"sq_content_src": "%2BdXJsPWh0dHAlM0ElMkYlMkZmaWxlcy50cmFuc3BvcnQuYWN0Lmdvdi5hdSUyRkFSVFMlMkZ1c2VfRnVuY3MuYXNwJmFsbD0x", # noqa | |
"SRNO": serialnumber, | |
"day": day, | |
"month": month, | |
"year": year, | |
"pwrd": passwd, | |
"rows": 0, | |
} | |
url = f"https://www.transport.act.gov.au/tickets-and-myway/check-myway-balance/check-your-balance?sq_content_src={data['sq_content_src']}" # noqa | |
r = requests.post(url, data=data) | |
soup = BeautifulSoup(r.text, 'html.parser') | |
table = soup.find("table", attrs={"class": "smartCardTable type3"}) | |
rows = table.find_all('tr') | |
for row in rows: | |
heading = row.find('th') | |
if heading.string == 'Card Balance': | |
col = row.find('td') | |
balance = float(col.string.lstrip('$ ').strip()) | |
return balance | |
def valid_date(s): | |
try: | |
return datetime.datetime.strptime(s, "%Y-%m-%d") | |
except ValueError: | |
msg = "Not a valid date: '{0}'.".format(s) | |
raise argparse.ArgumentTypeError(msg) | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument( | |
'serialnumber', | |
metavar="SRNO", | |
help="MyWay Card Serial Number, without spaces (ie:000011106)" | |
) | |
parser.add_argument( | |
'dob', | |
metavar='DOB', | |
type=valid_date, | |
help="Date of Birth, YYYY-MM-DD.", | |
) | |
parser.add_argument( | |
'-p', '--password', | |
help="Password. You will be prompted if password isn't specified.", | |
) | |
args = parser.parse_args() | |
if not args.password: | |
password = getpass.getpass() | |
else: | |
password = args.password | |
print(myway_balance( | |
args.serialnumber, | |
args.dob.day, | |
args.dob.month, | |
args.dob.year, | |
password | |
)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment