Skip to content

Instantly share code, notes, and snippets.

@DeMarko
Created December 12, 2010 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DeMarko/737956 to your computer and use it in GitHub Desktop.
Save DeMarko/737956 to your computer and use it in GitHub Desktop.
short script in PHP that takes command line args representing any of the MTA's subway lines and returns their current services status
#!/usr/bin/php
<?php
/**
* lineColor
* mostly for use with DeMarko's color extension of IRCCat
*
* @arg line string representing MTA Subway line
* @return string representing MTA Subway line color
*/
function lineColor($line) {
switch($line) {
case '123': return "#RED";
case '456': return "#DGREEN";
case '7': return "#PURPLE";
case 'ACE': return "#BLUE";
case 'BDFM': return "#ORANGE";
case 'G': return "#GREEN";
case 'JZ': return "#BROWN";
case 'L':
case 'S': return "#DGRAY";
case 'NQR': return "#YELLOW";
default: return "#NORMAL";
}
}
/**
* Take any number of MTA subway lines as command line args and return
* IRCCat-friendly messages describing the service status for the given lines
*/
/* @type SimpleXMLElement Object */
$statusxml;
$serviceStatusURL = 'http://mta.info/status/serviceStatus.txt';
try {
$serviceStatus = file_get_contents($serviceStatusURL);
$statusxml = simplexml_load_string($serviceStatus);
} catch (Exception $e) {
error_log("Could not establish connection to MTA Server");
}
if (get_class($statusxml) == "SimpleXMLElement") {
foreach (array_slice($argv, 1) as $arg) {
foreach ($statusxml->subway[0] as $line) {
// nobody goes to Staten Island
if ($line->name != "SIR" && stristr($line->name, $arg)) {
print(lineColor($line->name) . strtoupper($arg) . "#NORMAL - " . ucwords(strtolower($line->status)));
print(" :: last updated " . $line->Date . $line->Time . "\n");
}
}
}
} else {
print("Sorry, MTA subway info isn't available at the moment. Please try again in a few minutes.");
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment