Skip to content

Instantly share code, notes, and snippets.

@sensorian
Created July 27, 2015 15:55
Show Gist options
  • Save sensorian/60d2bceb146aaa39de81 to your computer and use it in GitHub Desktop.
Save sensorian/60d2bceb146aaa39de81 to your computer and use it in GitHub Desktop.
var PREDICTIONS_TAG = "<predictions";
var PREDICTIONS_END_TAG = "</predictions";
var DIRECTION_TAG = "<direction";
var DIRECTION_END_TAG = "</direction";
var PREDICTION_TAG = "<prediction";
var data_xml = msg.payload;
var predictions_begin = data_xml.indexOf(PREDICTIONS_TAG);
data_xml = data_xml.slice(predictions_begin);
var predictions_end = data_xml.indexOf(PREDICTIONS_END_TAG);
var xml_copy = data_xml;
var direction_slices = [];
while (xml_copy.indexOf(DIRECTION_TAG) != -1)
{
var direction_range = {};
direction_range["start"] = xml_copy.indexOf(DIRECTION_TAG);
direction_range["end"] = xml_copy.indexOf(DIRECTION_END_TAG);
direction_slices.push(xml_copy.slice(direction_range.start,direction_range.end));
xml_copy = xml_copy.slice(direction_range["end"]+DIRECTION_END_TAG.length);
xml_copy = xml_copy.slice(xml_copy.indexOf(DIRECTION_TAG));
}
var schedules = [];
for (var dir in direction_slices)
{
var title_regexp = new RegExp('title=\"\s*([^\"]*[^\"\s])\s*\"');
var schedule_string = title_regexp.exec(direction_slices[dir])[0].slice('title='.length).replace(/"/g,"") + " --> ";
while (direction_slices[dir].indexOf("<prediction") != -1)
{
var minutes_regexp = new RegExp('minutes=\"\s*([^\"]*[^\"\s])\s*\"');
var my_prediction = direction_slices[dir].slice(direction_slices[dir].indexOf("<prediction"),direction_slices[dir].indexOf("/>"));
schedule_string += minutes_regexp.exec(my_prediction)[0].slice('minutes='.length).replace(/"/g,"") + ", ";
direction_slices[dir] = direction_slices[dir].slice(direction_slices[dir].indexOf("/>")+1);
}
schedules.push(schedule_string);
}
var new_msg = {payload:schedules[0]};
return new_msg;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment