-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransit.py
More file actions
102 lines (81 loc) · 3.03 KB
/
transit.py
File metadata and controls
102 lines (81 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import requests
from dotenv import load_dotenv
import os
import json
import pandas as pd
from datetime import datetime
from datetime import timezone
from dateutil import tz
# function to load secrets
def secretFunc():
load_dotenv()
global TRANSIT_URL
global TRANSIT_API_KEY
global STOPCODES
global DIRECTIONS
global OPERATORS
global STOPNAMES
global OPENWEATHER_API_KEY
global LATITUDE
global LONGITUDE
TRANSIT_URL = 'http://api.511.org/transit/StopMonitoring/'
TRANSIT_API_KEY = os.environ['TRANSIT_API_KEY']
OPENWEATHER_API_KEY = os.environ['OPENWEATHER_API_KEY']
LATITUDE = os.environ['LAT']
LONGITUDE = os.environ['LONG']
# update STOPCODES, OPERATORS, DIRECTIONS, STOPNAMES to add add'l stops
STOPCODES = [13915, 13914, 14509, 14510]
OPERATORS = ['SF','SF', 'SF', 'SF']
DIRECTIONS = ['Inbound', 'Outbound', 'Inbound', 'Outbound']
STOPNAMES = ['Stanyan', 'Stanyan', 'Folsom', 'Folsom']
return()
# function to use 511org API to find next arrivals at each of the provided STOPCODES
def getNextTransit():
secretFunc()
arrivals = list()
for i, stop in enumerate(STOPCODES):
r = requests.get(TRANSIT_URL,
params = {'agency': OPERATORS[i],
'api_key': TRANSIT_API_KEY,
'stopcode': stop})
content = json.loads(r.content)
stopInfo = pd.DataFrame.from_records(content)
stopInfo = stopInfo['ServiceDelivery']['StopMonitoringDelivery']['MonitoredStopVisit']
for arrival in stopInfo:
# Get arrivalTime from JSON content
arrivalTime = arrival['MonitoredVehicleJourney']['MonitoredCall']['ExpectedArrivalTime']
# Convert arrivalTime into 12h Pacific time
utc_datetime = datetime.fromisoformat(arrivalTime[:-1])
# Define the timezones
utc_tz = tz.gettz('UTC')
pacific_tz = tz.gettz('America/Los_Angeles')
currentTime = datetime.now(timezone.utc)
# Set the UTC timezone for the datetime object
utc_datetime = utc_datetime.replace(tzinfo = utc_tz)
# Convert the datetime object to Pacific time
pacific_datetime = utc_datetime.astimezone(pacific_tz)
# Format the datetime object as a 12-hour time string
time_str = pacific_datetime.strftime('%I:%M %p')
# Calculte timeToArrival
timeToArrival = pacific_datetime - currentTime
timeToArrival = divmod(timeToArrival.seconds, 60)
timeToArrival = f"{str(timeToArrival[0]).rjust(2,'0')}:{str(timeToArrival[1]).rjust(2,'0')}"
# Get destination from JSON content
destination = arrival['MonitoredVehicleJourney']['MonitoredCall']['DestinationDisplay']
# Append [code, direction, time] to list
arrivals.append([STOPNAMES[i], DIRECTIONS[i], destination, time_str, timeToArrival, stop])
# Convert nested list to DataFrame
arrivals = pd.DataFrame(arrivals, columns=['stopnames', 'direction', 'destination','arrivalTime', 'timeToArrival', 'stopcode'])
return(arrivals)
'''
todo:
- connect to RPLCD to print information
- implement refreshment interval
'''
# main function
if __name__ == '__main__':
try:
transitArrivals = getNextTransit()
print(transitArrivals)
except TypeError:
print("No arrivals found.")