· 5 years ago · Feb 25, 2021, 01:06 AM
1# Python Script to Print Next Trains Stopping at 96th Street
2
3import os
4import requests
5import schedule
6import time
7import underground
8from datetime import datetime, timezone
9from underground import metadata, SubwayFeed
10
11# Grab API Key from Env file
12# API_KEY = os.getenv('MTA_API_KEY')
13
14# Define Train Routes
15# Request feeds from 123456S MTA Feed, hardcoded APIkey
16ROUTE1 = '1'
17feed1 = SubwayFeed.get(ROUTE1,api_key='MTA-API-KEY')
18
19
20# Feed_96th_Train is a dictionary
21Feed_96th_Train = feed1.extract_stop_dict()
22
23
24# Define the next two downtown trains on each line
25#Times for downtown trains
26OneTrainA = Feed_96th_Train['1']['120S'][0]
27OneTrainB = Feed_96th_Train['1']['120S'][1]
28TwoTrainA = Feed_96th_Train['2']['120S'][0]
29TwoTrainB = Feed_96th_Train['2']['120S'][1]
30ThreeTrainA = Feed_96th_Train['3']['120S'][0]
31ThreeTrainB = Feed_96th_Train['3']['120S'][1]
32
33
34# calculate time until arrival
35current_time = datetime.now(timezone.utc)
36
37# get countdown for each train
38time_until_one_train = (OneTrainA - current_time)
39time_until_two_train = (TwoTrainA - current_time)
40time_until_three_train = (ThreeTrainA - current_time)
41
42# convert countdown to minutes and round
43onetrain_countdown = (time_until_one_train.total_seconds() / 60)
44twotrain_countdown = (time_until_two_train.total_seconds() / 60)
45threetrain_countdown = (time_until_three_train.total_seconds() / 60)
46rounded_one_time = round(onetrain_countdown)
47rounded_two_time = round(twotrain_countdown)
48rounded_three_time = round(threetrain_countdown)
49
50# print info to terminal
51print("1 South Ferry ", rounded_one_time, " min")
52print("2 South Ferry ", rounded_two_time, " min")
53print("3 South Ferry ", rounded_three_time, " min")
54
55#Print current time
56print("It's currently ",time.strftime("%I:%M %p"))
57
58#End of script