Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ######
- #
- # Install ovchipapi though pip
- #
- ######
- from ovstat import OvApi
- import json
- import datetime
- import requests
- import re
- import pprint
- GMAPS_KEY = ""
- USERNAME = ""
- PASSWORD = ""
- pp = pprint.PrettyPrinter(indent=4)
- from dateutil.relativedelta import relativedelta
- o = OvApi.OvApi(USERNAME, PASSWORD)
- cards = o.get_cards_list()
- data = {}
- results = {}
- data["Transport"] = results
- pto = {}
- data["Companies"] = pto
- modal = {}
- data["Types"] = modal
- cards_inf = []
- data["Cards"] = cards_inf
- checkInCount = 0
- checkOutCount = 0
- today = datetime.datetime.now()
- month_begin = datetime.datetime(
- today.year, today.month, 1) + relativedelta(months=1)
- done = False
- fare = 0.0
- months = 2
- transactions = []
- distance = 0
- duration = 0
- numberOfR = 0
- for card in cards:
- _card = o.get_card(card["mediumId"])
- card_data = {}
- card_data["Balance"] = _card["card"]["balance"] / 100
- card_data["Alias"] = _card["card"]["alias"]
- card_data["AutoReload"] = _card["card"]["autoReloadEnabled"]
- card_data["Id"] = card["mediumId"]
- card_data["Products"] = []
- for product in _card["productInfoList"]:
- product_data = {}
- product_data["Title"] = product["productTitle"]
- product_data["Class"] = product["passengerClass"]
- product_data["ValidTill"] = (product["productValidity"].split(" "))[
- len(product["productValidity"].split(" ")) - 1]
- card_data["Products"].append(product_data)
- cards_inf.append(card_data)
- tmp_m = months
- while tmp_m and not done:
- month_begin = month_begin - relativedelta(months=1)
- month_end = month_begin + relativedelta(months=1)
- _transactions = o.get_transaction_list(card['mediumId'], 0, month_begin.strftime(
- '%Y-%m-%d'), month_end.strftime('%Y-%m-%d'))
- if _transactions[0]["transactionName"] != "Melding":
- transactions = transactions + _transactions
- else:
- done = True
- tmp_m -= 1
- for trans in transactions:
- posix_time = int(trans["transactionDateTime"] / 1000)
- if trans["transactionName"] == "Check-uit":
- checkOutCount = checkOutCount + 1
- regex = re.compile(".*?\((.*?)\)")
- inCheck = re.sub("[\(\[].*?[\)\]]", "",
- trans["checkInInfo"].replace(" ", "+"))
- outCheck = re.sub("[\(\[].*?[\)\]]", "",
- trans["transactionInfo"].replace(" ", "+"))
- if trans["fare"] != None:
- fare = fare + trans["fare"]
- if not pto.get(trans["pto"]):
- pto[trans["pto"]] = 1
- else:
- pto[trans["pto"]] = pto[trans["pto"]] + 1
- if not modal.get(trans["modalType"]):
- modal[trans["modalType"]] = 1
- else:
- modal[trans["modalType"]] = modal[trans["modalType"]] + 1
- if not results.get(inCheck):
- results[inCheck] = {}
- if results.get(inCheck).get(outCheck):
- distance = distance + \
- results.get(inCheck).get(outCheck)["distance"]
- duration = duration + \
- results.get(inCheck).get(outCheck)["duration"]
- results[inCheck][outCheck]["times"] = results[
- inCheck][outCheck]["times"] + 1
- continue
- r = requests.get("https://maps.googleapis.com/maps/api/directions/json?origin=" + inCheck +
- "&destination=" + outCheck + "&mode=transit&key=" + GMAPS_KEY)
- numberOfR = numberOfR + 1
- if r.status_code != 200:
- print(r.text)
- r = r.json()
- if len(r["routes"]) > 0:
- steps = r["routes"][0]["legs"][0]["steps"]
- for step in steps:
- if step["travel_mode"] == "TRANSIT":
- distance = distance + step["distance"]["value"]
- duration = duration + step["duration"]["value"]
- results[inCheck][outCheck] = {}
- results[inCheck][outCheck][
- "distance"] = step["distance"]["value"]
- results[inCheck][outCheck][
- "duration"] = step["duration"]["value"]
- results[inCheck][outCheck]["times"] = 1
- elif trans["transactionName"] == "Check-in":
- checkInCount = checkInCount + 1
- data["TotalFare"] = fare
- data["TotalDistance"] = int(distance / 1000)
- data["TotalDuration"] = int(duration / 3600)
- data["TotalCheckIn"] = checkInCount
- data["TotalCheckOut"] = checkOutCount
- data["TotalTimesForgotCheckOut"] = checkInCount - checkOutCount
- data["TotalTimesMaps"] = numberOfR
- data["StartDate"] = month_begin.strftime('%Y-%m-%d')
- data["EndDate"] = (today + relativedelta(months=1)).strftime('%Y-%m-01')
- print("Je hebt in ongeveer " + str(int(distance / 1000)) + " km afgelegd")
- print("Je hebt in ongeveer " + str(int(duration / 3600)) + " uur afgelegd")
- print("Dit is tussen " + month_begin.strftime('%Y-%m-%d') +
- " en " + today.strftime('%Y-%m-%d'))
- print("Keer ingechecked: " + str(checkInCount))
- print("Keer uitgechecked: " + str(checkOutCount))
- print("Keer Vergeten uitte checken: " + str(checkInCount - checkOutCount))
- print("Dit is opgevraagd in " + str(numberOfR) + " requests")
- print(json.dumps(pto, sort_keys=True, indent=4))
- print(json.dumps(modal, sort_keys=True, indent=4))
- print(json.dumps(results, sort_keys=True, indent=4))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement