Advertisement
Guest User

Untitled

a guest
Jun 7th, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.39 KB | None | 0 0
  1. ######
  2. #
  3. # Install ovchipapi though pip
  4. #
  5. ######
  6. from ovstat import OvApi
  7. import json
  8. import datetime
  9. import requests
  10. import re
  11. import pprint
  12. GMAPS_KEY = ""
  13. USERNAME = ""
  14. PASSWORD = ""
  15. pp = pprint.PrettyPrinter(indent=4)
  16. from dateutil.relativedelta import relativedelta
  17.  
  18. o = OvApi.OvApi(USERNAME, PASSWORD)
  19. cards = o.get_cards_list()
  20. data = {}
  21. results = {}
  22. data["Transport"] = results
  23. pto = {}
  24. data["Companies"] = pto
  25. modal = {}
  26. data["Types"] = modal
  27. cards_inf = []
  28. data["Cards"] = cards_inf
  29. checkInCount = 0
  30. checkOutCount = 0
  31. today = datetime.datetime.now()
  32. month_begin = datetime.datetime(
  33.     today.year, today.month, 1) + relativedelta(months=1)
  34. done = False
  35. fare = 0.0
  36. months = 2
  37. transactions = []
  38. distance = 0
  39. duration = 0
  40. numberOfR = 0
  41. for card in cards:
  42.     _card = o.get_card(card["mediumId"])
  43.     card_data = {}
  44.     card_data["Balance"] = _card["card"]["balance"] / 100
  45.     card_data["Alias"] = _card["card"]["alias"]
  46.     card_data["AutoReload"] = _card["card"]["autoReloadEnabled"]
  47.     card_data["Id"] = card["mediumId"]
  48.     card_data["Products"] = []
  49.     for product in _card["productInfoList"]:
  50.         product_data = {}
  51.         product_data["Title"] = product["productTitle"]
  52.         product_data["Class"] = product["passengerClass"]
  53.         product_data["ValidTill"] = (product["productValidity"].split(" "))[
  54.             len(product["productValidity"].split(" ")) - 1]
  55.         card_data["Products"].append(product_data)
  56.     cards_inf.append(card_data)
  57.     tmp_m = months
  58.     while tmp_m and not done:
  59.         month_begin = month_begin - relativedelta(months=1)
  60.         month_end = month_begin + relativedelta(months=1)
  61.         _transactions = o.get_transaction_list(card['mediumId'], 0, month_begin.strftime(
  62.             '%Y-%m-%d'), month_end.strftime('%Y-%m-%d'))
  63.  
  64.         if _transactions[0]["transactionName"] != "Melding":
  65.             transactions = transactions + _transactions
  66.         else:
  67.             done = True
  68.         tmp_m -= 1
  69. for trans in transactions:
  70.     posix_time = int(trans["transactionDateTime"] / 1000)
  71.     if trans["transactionName"] == "Check-uit":
  72.         checkOutCount = checkOutCount + 1
  73.         regex = re.compile(".*?\((.*?)\)")
  74.         inCheck = re.sub("[\(\[].*?[\)\]]", "",
  75.                          trans["checkInInfo"].replace(" ", "+"))
  76.         outCheck = re.sub("[\(\[].*?[\)\]]", "",
  77.                           trans["transactionInfo"].replace(" ", "+"))
  78.         if trans["fare"] != None:
  79.             fare = fare + trans["fare"]
  80.         if not pto.get(trans["pto"]):
  81.             pto[trans["pto"]] = 1
  82.         else:
  83.             pto[trans["pto"]] = pto[trans["pto"]] + 1
  84.         if not modal.get(trans["modalType"]):
  85.             modal[trans["modalType"]] = 1
  86.         else:
  87.             modal[trans["modalType"]] = modal[trans["modalType"]] + 1
  88.         if not results.get(inCheck):
  89.             results[inCheck] = {}
  90.         if results.get(inCheck).get(outCheck):
  91.             distance = distance + \
  92.                        results.get(inCheck).get(outCheck)["distance"]
  93.             duration = duration + \
  94.                        results.get(inCheck).get(outCheck)["duration"]
  95.             results[inCheck][outCheck]["times"] = results[
  96.                                                       inCheck][outCheck]["times"] + 1
  97.             continue
  98.         r = requests.get("https://maps.googleapis.com/maps/api/directions/json?origin=" + inCheck +
  99.                          "&destination=" + outCheck + "&mode=transit&key=" + GMAPS_KEY)
  100.         numberOfR = numberOfR + 1
  101.         if r.status_code != 200:
  102.             print(r.text)
  103.         r = r.json()
  104.  
  105.         if len(r["routes"]) > 0:
  106.             steps = r["routes"][0]["legs"][0]["steps"]
  107.             for step in steps:
  108.                 if step["travel_mode"] == "TRANSIT":
  109.                     distance = distance + step["distance"]["value"]
  110.                     duration = duration + step["duration"]["value"]
  111.                     results[inCheck][outCheck] = {}
  112.                     results[inCheck][outCheck][
  113.                         "distance"] = step["distance"]["value"]
  114.                     results[inCheck][outCheck][
  115.                         "duration"] = step["duration"]["value"]
  116.                     results[inCheck][outCheck]["times"] = 1
  117.     elif trans["transactionName"] == "Check-in":
  118.         checkInCount = checkInCount + 1
  119. data["TotalFare"] = fare
  120. data["TotalDistance"] = int(distance / 1000)
  121. data["TotalDuration"] = int(duration / 3600)
  122. data["TotalCheckIn"] = checkInCount
  123. data["TotalCheckOut"] = checkOutCount
  124. data["TotalTimesForgotCheckOut"] = checkInCount - checkOutCount
  125. data["TotalTimesMaps"] = numberOfR
  126. data["StartDate"] = month_begin.strftime('%Y-%m-%d')
  127. data["EndDate"] = (today + relativedelta(months=1)).strftime('%Y-%m-01')
  128.  
  129. print("Je hebt in ongeveer " + str(int(distance / 1000)) + " km afgelegd")
  130. print("Je hebt in ongeveer " + str(int(duration / 3600)) + " uur afgelegd")
  131. print("Dit is tussen " + month_begin.strftime('%Y-%m-%d') +
  132.      " en " + today.strftime('%Y-%m-%d'))
  133. print("Keer ingechecked: " + str(checkInCount))
  134. print("Keer uitgechecked: " + str(checkOutCount))
  135. print("Keer Vergeten uitte checken: " + str(checkInCount - checkOutCount))
  136. print("Dit is opgevraagd in " + str(numberOfR) + " requests")
  137. print(json.dumps(pto, sort_keys=True, indent=4))
  138. print(json.dumps(modal, sort_keys=True, indent=4))
  139. print(json.dumps(results, sort_keys=True, indent=4))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement