Created
August 12, 2010 04:13
-
-
Save ivarvong/520290 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sys = require('sys'); | |
var fs = require('fs'); | |
var connect = require('./connect/lib/connect'); | |
var http = require('http'); | |
var url = require('url'); | |
function getDirections(data, callback) { | |
requestStr = "/m/directions?saddr=" + escape(data.from) + "&daddr=" + escape(data.to) +"&dirflg=r"; | |
var client = http.createClient(80, 'www.google.com'); | |
var request = client.request('GET', requestStr, {'host': 'www.google.com'}); | |
request.end(); | |
var responseBody = ""; | |
request.on('response', function (response) { | |
response.setEncoding('utf8'); | |
response.on('data', function (chunk) { | |
responseBody += chunk; | |
}); | |
response.on('end', function() { | |
var routes = {}; | |
var matches = responseBody.match(new RegExp('<div class="[bc]">(.+?)<br/>', 'g')); | |
var count = 0; | |
//console.log(sys.inspect(matches)); | |
function getRouteInfo(idx, callback) { | |
var routeClient = http.createClient(80, 'www.google.com'); | |
var url = routes[idx].url; | |
url = url.replace(new RegExp('&','g'),'&'); | |
var routeRequest = routeClient.request('GET', url, {'Host': 'www.google.com', 'User-Agent': 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.8) Gecko/20100723 Ubuntu/10.04 (lucid) Firefox/3.6.8'}); | |
routeRequest.end(); | |
console.log("************* Requesting "+url+" ..."); | |
var routeResponseBody = ""; | |
routeRequest.on('response', function (routeResponse) { | |
routeResponse.setEncoding('utf8'); | |
routeResponse.on('data', function (chunk) { | |
routeResponseBody += chunk; | |
}); | |
routeResponse.on('end', function() { | |
//console.log(sys.inspect(routeResponseBody)); | |
console.log("!!!!!!!!!!!!!!!! FINISHED on idx="+idx); | |
//console.log(routeResponseBody.split("<div")); | |
routeResponses = routeResponseBody.match(new RegExp('<[a-z]{3,4} class="[eqb ]{1,3}">(.+?)</[a-z]{3,4}>','g') ); | |
var resp = [] | |
routeResponses.forEach(function(routeResponse) { | |
resp.push( routeResponse.replace(new RegExp('<img .+?>','g'),'') ); | |
}); | |
callback( resp ); | |
}); | |
}); | |
} | |
if (matches) { //need testing code to make sure we got something we can iterate over | |
matches.forEach(function(item) { | |
item = item.replace(new RegExp('<div class=".+?">'), ''); | |
item = item.replace('<br/>', ''); | |
//item = item.replace('/m/directions', 'http://www.google.com/m/directions'); | |
var _temp1 = item.split(': <a href="'); | |
var idx = _temp1[0]; | |
var _temp2 = _temp1[1].split('" >'); | |
var url = _temp2[0]; | |
var _temp3 = _temp2[1].split('</a> '); | |
var times = _temp3[0]; | |
var estimate = _temp3[1]; | |
var estimate = estimate.replace('(',''); | |
var estimate = estimate.replace(') ',''); | |
// add magic here. | |
routes[idx] = { idx: idx, | |
url: url, | |
routeInfo: null, | |
times: times, | |
estimate: estimate, | |
//rawItem: item, | |
}; | |
getRouteInfo(idx, function(routeInfo) { | |
routes[idx].routeInfo = routeInfo; | |
var routesLength = 0; | |
var routesPopulated = 0; | |
for(idx in routes) { | |
routesLength += 1; | |
if (routes[idx].routeInfo != null) { | |
routesPopulated += 1; | |
} | |
} | |
console.log("l:"+routesLength+" pop:"+routesPopulated); | |
//if ( (routesLength == matches.length) && (routesLength == routesPopulated) ) { | |
if (routesLength == routesPopulated) { | |
console.log("Making the callback..."); | |
callback({ routes: routes }); | |
} | |
}); | |
}); | |
} else { | |
routes = {error: "Parse error. Matches returned an empty set", | |
success: false, | |
source: data}; | |
callback({ routes: routes }); | |
} | |
}); | |
}); | |
} | |
function main(app) { | |
app.get('/', function(req, res){ | |
res.writeHead(200, { 'Content-Type': 'text/html' }); | |
getDirections({ | |
to:'29th and alder, eugene, or', | |
from:'13th and university, eugene, or' | |
}, function(data) { | |
data.routes.forEach(function(route) { | |
res.write('Route <a href="http://google.com' + route.url +'">' + route.times + "</a>. Travel time is " + route.estimate + "<br>\n"); | |
}); | |
res.write("\n\n<br><pre>\n\n"); | |
res.write( JSON.stringify( data, null, 2 ) ); | |
console.log( sys.inspect( data ) ); | |
res.end(''); | |
}); | |
}); | |
app.get('/transit', function(req, res) { | |
res.writeHead(200, {'Content-Type:': 'text/html' }); | |
var parsedurl = url.parse(req.url, true); | |
getDirections({ | |
to: parsedurl.query.to, | |
from: parsedurl.query.from | |
}, function(data) { | |
/*res.write("Found " + data.count + " routes!<br>\n"); | |
data.routes.forEach(function(route) { | |
res.write('Route ' + route.idx + ' is <a href="' + route.url +'">' + route.times + "</a>. Travel time is roughly " + route.estimate + "<br>\n"); | |
});*/ | |
res.write( JSON.stringify( {request: parsedurl, response: data.routes}, null, 2 ) ); | |
console.log( sys.inspect( data ) ); | |
res.end(''); | |
}); | |
}); | |
} | |
var server = connect.createServer( | |
connect.logger(), | |
connect.bodyDecoder(), | |
connect.router(main), | |
connect.errorHandler({ dumpExceptions: true, showStack: true }) | |
).listen(8004); | |
console.log('Connect server listening on port 8004'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment