Loading...

Viewing Fixee

Language: Javascript (View Plain Text)

March 19, 2010 5:59 am

  1. var sys = require('sys')
  2. var http = require('http');
  3.  
  4.  
  5. (function(options) {
  6.  
  7. var arrivingFlights = new Array();
  8.  
  9. var util = { trim: function( text ) {
  10. return (text || "").replace( rtrim, "" );
  11. }};
  12.  
  13. var rtrim = /^(\s|\u00A0)+|(\s|\u00A0)+$/g
  14. var baseUrl = "/go/weblet?guid=c228b59beca1b817:44617f82:117d601a4c6:221f&weblet=status&action=AirportFlightStatus&airportCode=USM&airportQueryType=1";
  15. var host = "www.flightstats.com";
  16. var refer = "http://www.samuiairportonline.com/flight-status";
  17. var userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2) Gecko/20100115 Firefox/3.6";
  18.  
  19. var retrieve = function(url,complete)
  20. {
  21. var flightStat = http.createClient(80, host);
  22. var request = flightStat.request("GET", url,
  23. {"Host": host
  24. , "Referer": refer
  25. , "User-Agent": userAgent });
  26. request.addListener('response', function(response)
  27. {
  28. response.setBodyEncoding("utf8");
  29. var body = "";
  30. response.addListener('data', function(chunk) { body += chunk; });
  31. response.addListener('end', function() { complete(body); });
  32. });
  33. request.close();
  34. }
  35.  
  36. var updateFlightTimes = function()
  37. {
  38. var completedCounter = 0; // reset
  39. var lastArrivingFlights = new Array();
  40.  
  41. // copy over the global collection only after all 8 result sets have returned
  42. var completed = function() { if(completedCounter++ && completedCounter == 8 ) arrivingFlights = lastArrivingFlights; };
  43.  
  44. var processTable = function(page)
  45. {
  46. var tableContent = page.match(/class="tableListingTable"[^>]+>([^]*)<\/table>/gmi)[0];
  47. var rowRegExp = /<tr>\s*?<td[^>]*?>\s*?<a[^>]*?>([^<]*?)<\/a>\s*?(?:<span[^>]*>.*?<\/span>)?\s*?<\/td>\s*?<td[^>]*?>([^<]*?)<\/td>\s*?<td[^>]*?>\s*?\(\s*?<a[^>]*>.*?<\/a>\s*?\)([^<]*?)<\/td>\s*?<td[^>]*?>([^<]*?)</mgi;
  48. while(rowMatch = rowRegExp.exec(tableContent))
  49. {
  50. lastArrivingFlights.push({ code: util.trim(rowMatch[1]),
  51. carrier: util.trim(rowMatch[2]),
  52. origin: util.trim(rowMatch[3]),
  53. arriveTime: util.trim(rowMatch[4]) });
  54. }
  55. completed();
  56. };
  57.  
  58. // full 24hr period - have to pull 8 pages back
  59. for(var i = 1; i <= 8; i++)
  60. {
  61. retrieve(baseUrl + "&airportQueryTimePeriod=" + i,processTable);
  62. }
  63. }
  64.  
  65. updateFlightTimes();
  66. setInterval(updateFlightTimes,options.updateInterval);
  67.  
  68. http.createServer(function (req, res) {
  69. res.writeHead(200, {'Content-Type': 'application/json'});
  70. res.write(JSON.stringify(arrivingFlights));
  71. res.close();
  72. }).listen(8000);
  73.  
  74. })({ updateInterval: 300000 });
  75.  
OriginalFixee
No Fixees have been posted!
As soon as a Fixee is posted, it will appear here automatically.
No need to refresh.