Skip to content

Instantly share code, notes, and snippets.

/x.js

Created August 25, 2009 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save anonymous/174663 to your computer and use it in GitHub Desktop.
Save anonymous/174663 to your computer and use it in GitHub Desktop.
Seach Deutsch Bahn Timetable for connections with ubiquitos
/*var noun_type_train_station_start = {
name: "Train station",
label: "station",
default: "Stuttgart Hbf",
};*/
var noun_type_train_station = {
name: "Train station",
label: "station",
default: "Stuttgart Hbf",
suggest: function freebase_train_station(text, html, callback) {
if (text.length < 1) callback([CmdUtils.makeSugg(text)]);
return [
$.ajax({
url: "http://reiseauskunft.bahn.de/bin/ajax-getstop.exe/dn?REQ0JourneyStopsS0A=1&REQ0JourneyStopsB=12",
dataType: "html",
data: {REQ0JourneyStopsS0G: text},
success: function suggestTopics(response) {
response = response.slice(response.indexOf('[{'), response.indexOf('}]') + 2);
value = eval(response);
//callback([CmdUtils.makeSugg(response)]);
callback([CmdUtils.makeSugg(result.value)
for each (result in value )]);
},
})];
}
};
CmdUtils.CreateCommand({
names: ["train", "db", "zug", "zug von"],
icon: "http://www.bahn.de/common/view/static/favicon.ico",
description: "Search Deutsche Bahn for train connections",
help: "Just state from where and to where you want to go by european trains. Maybe you also like to include a time, if you are not quite near to you starting point",
author: {name: "Oliver Wienand", email: "wienand@gmx.de"},
license: "Public Domain",
homepage: "",
arguments: [{role: 'source', nountype: noun_arb_text, label: 'city'},
{role: 'goal', nountype: noun_arb_text, label: 'city'},
{role: 'time', nountype: noun_type_time, label: 'when'},
{role: 'date', nountype: noun_type_date, label: 'when'}],
preview: function preview(pblock, args) {
var baseUrl = "http://reiseauskunft.bahn.de/bin/query.exe/dn";
var params = {existOptimizePrice: "1",
S: args.source.text,
Z: args.goal.text,
start: "1",
date: Date.parse(args.date.data).toString("dd.MM.yyyy"),
time: Date.parse(args.time.data).toString("HH:mm")};
pblock.innerHTML = "Searching trains from <b>" + params["S"] + "</b> to <b>" + params["Z"]
+ "</b> at <b>" + params["time"]
+ "</b> on <b>" + params["date"] + "</b>.";
jQuery.get(baseUrl, params,
function( page ) {
if (page.search('"result"') > -1) {
page = page.slice(page.search('"result"') - 13);
page = page.slice(0, page.search('</table>') + 8);
page = page.replace(new RegExp( "\\n", "g" ), '');
page = page.replace(/<tr class="links">.*?<\/tr>/g, '');
page = page.replace(/<th class="fareStd">Normalpreis<\/th>/, '');
page = page.replace(/<td class="return lastrow" rowspan="2">.*?<\/td>/g, '');
page = page.replace(/<td class=" fareStd lastrow button-inside tablebutton" rowspan="2">.*?<\/td>/g, '');
page = '</head><body><div id="hafasContainer" style="width:0px">' + page
page = '<link rel="stylesheet" type="text/css" href="http://reiseauskunft.bahn.de/v/600/cms/css/bahn.min.css" />' + page;
page = '<link rel="stylesheet" type="text/css" href="http://reiseauskunft.bahn.de/v/620/css/hafas.css" />' + page;
page = '<head>' + page
page += '</div></body>'
page = page.replace(/<table/g, '<table style="width:0px;"');
page = page.replace(/<td/g, '<td style="width:0px;"');
// page = page.replace(new RegExp( "<", "g" ), '#');
pblock.innerHTML = page;
}
}
);
},
execute: function execute(args) {
var baseUrl = "http://reiseauskunft.bahn.de/bin/query.exe/dn";
var params = {existOptimizePrice: "1",
S: args.source.text,
Z: args.goal.text,
start: "1",
date: Date.parse(args.date.data).toString("dd.MM.yyyy"),
time: Date.parse(args.time.data).toString("HH:mm")};
Utils.openUrlInBrowser(baseUrl, params);
}
});
CmdUtils.CreateCommand({
names: ["train arrive", "ankunftstafel"],
icon: "http://www.bahn.de/common/view/static/favicon.ico",
description: "Search Deutsche Bahn for current schedules",
help: "Just enter the station name, suggestions will be made.",
author: {name: "Oliver Wienand", email: "wienand@gmx.de"},
license: "Public Domain",
homepage: "",
arguments: [{role: 'object', nountype: noun_type_train_station, label: 'train station'}],
preview: function preview(pblock, args) {
pblock.innerHTML = "Current schedule in <b>" + args.object.html + "</b>.";
},
execute: function execute(args) {
var baseUrl = "http://reiseauskunft.bahn.de/bin/bhftafel.exe/dn?rt=1&boardType=arr&productsFilter=1111&start=yes&input=" + args.object.text;
Utils.openUrlInBrowser(baseUrl);
}
});
CmdUtils.CreateCommand({
names: ["train depart", "abfahrtstafel"],
icon: "http://www.bahn.de/common/view/static/favicon.ico",
description: "Search Deutsche Bahn for current schedules",
help: "Just enter the station name, suggestions will be made.",
author: {name: "Oliver Wienand", email: "wienand@gmx.de"},
license: "Public Domain",
homepage: "",
arguments: [{role: 'object', nountype: noun_type_train_station, label: 'train station'}],
preview: function preview(pblock, args) {
pblock.innerHTML = "Current schedule in <b>" + args.object.html + "</b>.";
},
execute: function execute(args) {
var baseUrl = "http://reiseauskunft.bahn.de/bin/bhftafel.exe/dn?rt=1&boardType=dep&productsFilter=1111&start=yes&input=" + args.object.text;
Utils.openUrlInBrowser(baseUrl);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment