Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Initial commit.
  • Loading branch information
maobowen committed Dec 28, 2018
0 parents commit 7804757
Show file tree
Hide file tree
Showing 50 changed files with 6,892 additions and 0 deletions.
664 changes: 664 additions & 0 deletions .gitignore

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIT License

Copyright (c) 2018 Bowen Mao

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# MTA New York City Subway Time

**MTA New York City Subway Time** is an Alfred workflow for querying subway time of [Metropolitan Transportation Authority (MTA)](https://new.mta.info) [New York City Subway](http://web.mta.info/nyct/subway/) trains. It runs on [Alfred 3](https://www.alfredapp.com) with [Alfred Powerpack](https://www.alfredapp.com/powerpack/) enabled. It has never been so easy to track subway trains on your Mac!

## Installation

Download the [latest release](https://github.com/maobowen/nyct-subway-alfred-workflow/releases/latest), and double-click it to install.

## Usage

Use the keyword `s` to start a query, followed by a phrase. For example, enter `s times` to get a list of subway stations whose names contain the phrase "times". Then select a service from the list, and press <kbd>tab</kbd> key to get the time of all trains arriving at that station:
![Demonstration](https://github.com/maobowen/nyct-subway-alfred-workflow/raw/master/demo/demo.gif)

## Acknowledgement

This project is powered by [Alfred-Workflow](https://github.com/deanishe/alfred-workflow), a helper library in Python for authors of workflows for Alfred 2 and 3.

***
&copy; 2018 [Bowen Mao](https://bmao.tech/).
Binary file added demo/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions images/5X.png
Binary file added images/6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/6X.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/7X.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/A.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/B.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/C.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/D.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/E.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/F.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/G.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions images/GS.png
Binary file added images/J.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/L.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/M.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/MTA.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/N.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/Q.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/R.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/S.png
Binary file added images/SI.png
1 change: 1 addition & 0 deletions images/SIR.png
Binary file added images/T.png
Binary file added images/W.png
Binary file added images/Z.png
129 changes: 129 additions & 0 deletions info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>bundleid</key>
<string>tech.bmao.nyct-subway-alfred-workflow</string>
<key>category</key>
<string>Productivity</string>
<key>connections</key>
<dict>
<key>3A0EF0C9-B353-4FCF-9039-BF3A09A243D2</key>
<array>
<dict>
<key>destinationuid</key>
<string>5D986811-F6FE-4698-AF13-92524E553320</string>
<key>modifiers</key>
<integer>0</integer>
<key>modifiersubtext</key>
<string></string>
<key>vitoclose</key>
<false/>
</dict>
</array>
</dict>
<key>createdby</key>
<string>Bowen Mao</string>
<key>description</key>
<string>Find the subway time of MTA New York City Subway trains</string>
<key>disabled</key>
<false/>
<key>name</key>
<string>MTA New York City Subway Time</string>
<key>objects</key>
<array>
<dict>
<key>config</key>
<dict>
<key>lastpathcomponent</key>
<false/>
<key>onlyshowifquerypopulated</key>
<false/>
<key>removeextension</key>
<false/>
<key>text</key>
<string>{query}</string>
<key>title</key>
<string>MTA New York City Subway Time</string>
</dict>
<key>type</key>
<string>alfred.workflow.output.notification</string>
<key>uid</key>
<string>5D986811-F6FE-4698-AF13-92524E553320</string>
<key>version</key>
<integer>1</integer>
</dict>
<dict>
<key>config</key>
<dict>
<key>alfredfiltersresults</key>
<false/>
<key>alfredfiltersresultsmatchmode</key>
<integer>0</integer>
<key>argumenttrimmode</key>
<integer>0</integer>
<key>argumenttype</key>
<integer>0</integer>
<key>escaping</key>
<integer>102</integer>
<key>keyword</key>
<string>s</string>
<key>queuedelaycustom</key>
<integer>3</integer>
<key>queuedelayimmediatelyinitially</key>
<true/>
<key>queuedelaymode</key>
<integer>0</integer>
<key>queuemode</key>
<integer>1</integer>
<key>runningsubtext</key>
<string>Looking for Station "{query}"...</string>
<key>script</key>
<string>/usr/bin/python nyct_subway.py $1</string>
<key>scriptargtype</key>
<integer>1</integer>
<key>scriptfile</key>
<string></string>
<key>subtext</key>
<string>Enter a station name and press Tab key to see the subway time</string>
<key>title</key>
<string>MTA New York City Subway Time</string>
<key>type</key>
<integer>0</integer>
<key>withspace</key>
<true/>
</dict>
<key>type</key>
<string>alfred.workflow.input.scriptfilter</string>
<key>uid</key>
<string>3A0EF0C9-B353-4FCF-9039-BF3A09A243D2</string>
<key>version</key>
<integer>2</integer>
</dict>
</array>
<key>readme</key>
<string>MTA New York City Subway Time is an Alfred workflow for querying subway time of Metropolitan Transportation Authority (MTA) New York City Subway trains.
For more information, please visit https://github.com/maobowen/nyct-subway-alfred-workflow/.</string>
<key>uidata</key>
<dict>
<key>3A0EF0C9-B353-4FCF-9039-BF3A09A243D2</key>
<dict>
<key>xpos</key>
<integer>10</integer>
<key>ypos</key>
<integer>10</integer>
</dict>
<key>5D986811-F6FE-4698-AF13-92524E553320</key>
<dict>
<key>xpos</key>
<integer>240</integer>
<key>ypos</key>
<integer>10</integer>
</dict>
</dict>
<key>version</key>
<string>2018.12.28</string>
<key>webaddress</key>
<string>https://github.com/maobowen/nyct-subway-alfred-workflow/</string>
</dict>
</plist>
124 changes: 124 additions & 0 deletions nyct_subway.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#!/usr/bin/python
# encoding: utf-8

import csv
import re
from StringIO import StringIO
import sys
from workflow import Workflow3, web, ICON_ERROR, ICON_INFO


BOROUGH = {"M": "Manhattan", "Bk": "Brooklyn", "Q": "Queens", "Bx": "The Bronx", "SI": "Staten Island"}


# Get subway time
def get_time(wf, complex_routes, gtfs_stop_id_complex, route, gtfs_stop_id):
# Find all transferrable services in the current complex
complex_id = gtfs_stop_id_complex[gtfs_stop_id.encode("ascii", "ignore")]
subtitle = ""
if complex_routes.get(complex_id) is not None and len(complex_routes[complex_id]) > 1:
transfer_routes = sorted(list(complex_routes[complex_id]))
subtitle = "Transfer is available to the %s trains" % ", ".join(transfer_routes)
# Get subway time by service and station
response = web.get("http://traintimelb-367443097.us-east-1.elb.amazonaws.com/getTime/%s/%s" % (route, gtfs_stop_id))
response.raise_for_status()
station = response.json()
station_name = station["stationName"]
wf.add_item(title=u"%s" % station_name, subtitle=u"%s" % subtitle, icon="images/MTA.png")
directions = [None] * 2
directions[0] = station["direction1"]
directions[1] = station["direction2"]
for direction in directions:
if len(direction["times"]) != 0: # Some station has only one direction
for time in direction["times"]:
if time["lastStation"] != station_name: # Show non-terminal only
subtitle = ""
if direction["name"]: # Show the name of direction if not empty
if direction["name"] in ["Uptown", "Downtown"]:
subtitle = direction["name"]
else:
subtitle = direction["name"] + " bound"
broadcast_suffix = "" # Mimic the station broadcasting
if isinstance(time["minutes"], (int, long)):
minutes = "%d minutes" % time["minutes"] if time["minutes"] > 1 else "%d minute" % time["minutes"]
broadcast_suffix = minutes + " away" if time["minutes"] > 0 else " approaching the station"
else:
minutes = broadcast_suffix = time["minutes"]
wf.add_item(
title=u"To %s: %s" % (time["lastStation"], minutes),
subtitle=u"%s" % subtitle,
valid=True,
arg=u"There is a(n) %s %s train to %s %s" % (subtitle, time["route"], time["lastStation"], broadcast_suffix),
icon="images/%s.png" % time["route"]
)


# Get all stations
def get_station_list():
response = web.get("http://web.mta.info/developers/data/nyct/subway/Stations.csv")
response.raise_for_status()
f = StringIO(response.text)
stations = [] # All stations
complex_routes = {} # All complexes
gtfs_stop_id_complex = {} # Stop ID - complex ID
for row in csv.DictReader(f, skipinitialspace=True):
station = {}
for key, value in row.items():
if key == "Station ID" or key == "Complex ID":
station[key] = int(value)
elif key == "Daytime Routes":
station[key] = value.split()
else:
station[key] = value
stations.append(station)
if complex_routes.get(station["Complex ID"]) is None:
complex_routes[station["Complex ID"]] = set(station["Daytime Routes"])
else:
complex_routes[station["Complex ID"]] |= set(station["Daytime Routes"])
gtfs_stop_id_complex[station["GTFS Stop ID"]] = station["Complex ID"]
return stations, complex_routes, gtfs_stop_id_complex


def get_stations(wf, stations, query):
outputs = []
for station in stations:
if query.lower() in station["Stop Name"].lower():
outputs.append(station)
# Add to the lists of results for Alfred
if len(outputs) == 0:
wf.add_item(title=u"Error", subtitle=u"Station \"%s\" does not exist" % query, icon=ICON_ERROR)
else:
wf.add_item(title=u"Instruction", subtitle=u"Select a station and press Tab key to see the subway time", icon=ICON_INFO)
for station in outputs:
for route in station["Daytime Routes"]:
wf.add_item(
title=u"%s" % station["Stop Name"],
subtitle=u"%s - %s" % (BOROUGH[station["Borough"]], " ".join(station["Daytime Routes"])),
icon="images/%s.png" % route,
autocomplete="%s@%s" % (route, station["GTFS Stop ID"])
)


def main(wf):
stations, complex_routes, gtfs_stop_id_complex = wf.cached_data("nyct_subway_all_stations", get_station_list, max_age=60 * 60 * 24)
query = " ".join(wf.args).split("@")
if len(query) == 2 and re.match("^[A-Z0-9]$|^SIR$", query[0]) and re.match("^[A-Z0-9][0-9]{2}$", query[1]): # Get time for the station
get_time(wf, complex_routes, gtfs_stop_id_complex, query[0], query[1])
else: # Search stations
get_stations(wf, stations, query[0])
wf.send_feedback()


if __name__ == "__main__":
wf = Workflow3(update_settings={
"github_slug": "maobowen/nyct-subway-alfred-workflow",
"frequency": 7
})
if wf.update_available:
wf.add_item(
title=u"New version available",
subtitle=u"Action this item to install the update",
autocomplete="workflow:update",
icon=ICON_INFO
)
sys.exit(wf.run(main))
1 change: 1 addition & 0 deletions version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v2018.12.28
Empty file added workflow/.alfredversionchecked
Empty file.
Binary file added workflow/Notify.tgz
Binary file not shown.
Loading

0 comments on commit 7804757

Please sign in to comment.