diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..db64be9 --- /dev/null +++ b/.jshintrc @@ -0,0 +1,27 @@ +{ + "browser": true, + "boss": true, + "curly": true, + "debug": false, + "devel": true, + "eqeqeq": true, + "evil": true, + "forin": false, + "immed": false, + "laxbreak": false, + "newcap": true, + "noarg": true, + "noempty": false, + "nonew": false, + "nomen": false, + "onevar": false, + "plusplus": false, + "regexp": false, + "undef": true, + "sub": true, + "strict": false, + "white": false, + "eqnull": true, + "esnext": true, + "unused": true +} diff --git a/app.js b/app.js index 9b49d1c..f67ca7f 100644 --- a/app.js +++ b/app.js @@ -1,17 +1,17 @@ /*jslint node: true */ -"use strict"; +'use strict'; -var express = require('express'); -var path = require('path'); -var favicon = require('serve-favicon'); -var logger = require('morgan'); -var cookieParser = require('cookie-parser'); -var bodyParser = require('body-parser'); +const express = require('express'); +const path = require('path'); +const favicon = require('serve-favicon'); +const logger = require('morgan'); +const cookieParser = require('cookie-parser'); +const bodyParser = require('body-parser'); -var routes = require('./routes/index'); -var users = require('./routes/users'); +const routes = require('./routes/index'); +const users = require('./routes/users'); -var app = express(); +const app = express(); // view engine setup app.set('views', path.join(__dirname, 'views')); @@ -19,19 +19,19 @@ app.set('view engine', 'jade'); // uncomment after placing your favicon in /public //app.use(favicon(__dirname + '/public/favicon.ico')); +//app.use(require('node-compass')({mode: 'expanded'})); app.use(logger('dev')); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(cookieParser()); -//app.use(require('node-compass')({mode: 'expanded'})); app.use(express.static(path.join(__dirname, 'public'))); app.use('/', routes); app.use('/users', users); // catch 404 and forward to error handler -app.use(function(req, res, next) { - var err = new Error('Not Found'); +app.use((req, res, next) => { + const err = new Error('Not Found'); err.status = 404; next(err); }); @@ -41,24 +41,23 @@ app.use(function(req, res, next) { // development error handler // will print stacktrace if (app.get('env') === 'development') { - app.use(function(err, req, res, next) { + app.use((err, req, res, next) => { res.status(err.status || 500); res.render('error', { message: err.message, - error: err + error: err, }); }); } // production error handler // no stacktraces leaked to user -app.use(function(err, req, res, next) { +app.use((err, req, res, next) => { res.status(err.status || 500); res.render('error', { message: err.message, - error: {} + error: {}, }); }); - module.exports = app; diff --git a/bin/www b/bin/www index 88d071e..212a2fc 100755 --- a/bin/www +++ b/bin/www @@ -4,22 +4,22 @@ * Module dependencies. */ -var app = require('../app'); -var debug = require('debug')('AuvasaScraper:server'); -var http = require('http'); +const app = require('../app'); +const debug = require('debug')('AuvasaScraper:server'); +const http = require('http'); /** * Get port from environment and store in Express. */ -var port = normalizePort(process.env.PORT || '3000'); +const port = normalizePort(process.env.PORT || '3000'); app.set('port', port); /** * Create HTTP server. */ -var server = http.createServer(app); +const server = http.createServer(app); /** * Listen on provided port, on all network interfaces. @@ -34,7 +34,7 @@ server.on('listening', onListening); */ function normalizePort(val) { - var port = parseInt(val, 10); + const port = parseInt(val, 10); if (isNaN(port)) { // named pipe @@ -58,9 +58,7 @@ function onError(error) { throw error; } - var bind = typeof port === 'string' - ? 'Pipe ' + port - : 'Port ' + port; + const bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port; // handle specific listen errors with friendly messages switch (error.code) { @@ -82,9 +80,7 @@ function onError(error) { */ function onListening() { - var addr = server.address(); - var bind = typeof addr === 'string' - ? 'pipe ' + addr - : 'port ' + addr.port; + const addr = server.address(); + const bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port; debug('Listening on ' + bind); } diff --git a/package.json b/package.json index a1175ab..7935bd7 100755 --- a/package.json +++ b/package.json @@ -1,22 +1,31 @@ { - "name": "AuvasaScraper", + "name": "auvasa-scraper", "version": "1.0.0", - "engines": { - "node": "~0.12.2", - "npm": "~2.7.4" - }, "scripts": { "start": "node ./bin/www" }, "dependencies": { - "body-parser": "~1.12.4", - "cheerio": "^0.19.0", - "cookie-parser": "~1.3.5", + "body-parser": "~1.15.0", + "cheerio": "~0.20.0", + "cookie-parser": "~1.4.1", "debug": "~2.2.0", - "express": "~4.12.4", - "jade": "~1.10.0", - "morgan": "~1.5.3", - "request": "^2.56.0", - "serve-favicon": "~2.2.1" - } + "express": "~4.13.4", + "jade": "~1.11.0", + "morgan": "~1.7.0", + "request": "~2.56.0", + "serve-favicon": "~2.3.0" + }, + "description": "[![Build Status](https://travis-ci.org/luisddm/AuvasaScraper.svg)](https://travis-ci.org/luisddm/AuvasaScraper)", + "main": "app.js", + "devDependencies": {}, + "repository": { + "type": "git", + "url": "git+https://luisddm@github.com/luisddm/AuvasaScraper.git" + }, + "author": "Luis de Dios Martin", + "license": "MIT", + "bugs": { + "url": "https://github.com/luisddm/AuvasaScraper/issues" + }, + "homepage": "https://github.com/luisddm/AuvasaScraper#readme" } diff --git a/routes/index.js b/routes/index.js index 25f5eaf..f13f5d7 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,59 +1,59 @@ /*jslint node: true */ -"use strict"; +'use strict'; -var express = require('express'); -var router = express.Router(); +const express = require('express'); +const router = express.Router(); -var cheerio = require('cheerio'); -var request = require('request'); +const cheerio = require('cheerio'); +const request = require('request'); /* GET home page. */ -router.get('/', function(req, res, next) { +router.get('/', (req, res, next) => { res.render('main', { title: 'Auvasa Scraper' }); }); -router.get('/parada/:num', function(req, res, next) { +router.get('/parada/:num', (req, res, next) => { - var url = 'http://www.auvasa.es/paradamb.asp?codigo=' + req.params.num; + const url = 'http://www.auvasa.es/paradamb.asp?codigo=' + req.params.num; - // The structure of our request call - // The first parameter is our URL - // The callback function takes 3 parameters, an error, response status code and the html + // The structure of our request call + // The first parameter is our URL + // The callback function takes 3 parameters, an error, response status code and the html - request(url, function(error, response, html) { + request(url, (error, response, html) => { - // First we'll check to make sure no errors occurred when making the request + // First we'll check to make sure no errors occurred when making the request - if(!error) { + if (!error) { + // Next, we'll utilize the cheerio library on the returned html which will essentially + // give us jQuery functionality + const $ = cheerio.load(html); - // Next, we'll utilize the cheerio library on the returned html which will essentially give us jQuery functionality - var $ = cheerio.load(html); + // Get the name of the bus line + const title = $('#mainleft0').find('h1').eq(0).text(); - // Get the name of the bus line - var title = $('#mainleft0').find("h1").eq(0).text(); + // Get the timetable of the next buses + const horarios = []; - // Get the timetable of the next buses - var horarios = []; + $('.style36').each((i, element) => { + const $td = $(this).find('.style38'); - $(".style36").each(function(i, element) { - var $td = $(this).find(".style38"); + const linea = $td.eq(0).text(); + const dest = $td.eq(2).text(); + const tiempo = $td.eq(3).text(); - var linea = $td.eq(0).text(); - var dest = $td.eq(2).text(); - var tiempo = $td.eq(3).text(); + const ruta = { linea: linea, dest: dest, tiempo: tiempo }; - var ruta = { linea: linea, dest: dest, tiempo: tiempo }; + horarios.push(ruta); + }); - horarios.push(ruta); - }); + res.render('index', { title: title, horarios: horarios }); - res.render('index', { title: title, horarios: horarios }); + } else { + res.render('error', { message: 'Error', error: error }); + } - } else { - res.render('error', { message: "Error", error: error }); - } - - }); + }); }); diff --git a/routes/users.js b/routes/users.js index 623e430..95111e4 100644 --- a/routes/users.js +++ b/routes/users.js @@ -1,8 +1,11 @@ -var express = require('express'); -var router = express.Router(); +/*jslint node: true */ +'use strict'; + +const express = require('express'); +const router = express.Router(); /* GET users listing. */ -router.get('/', function(req, res, next) { +router.get('/', (req, res, next) => { res.send('respond with a resource'); });