Skip to content
This repository has been archived by the owner on Dec 31, 2020. It is now read-only.

Commit

Permalink
Refactor a few things & add tests for movie scraping
Browse files Browse the repository at this point in the history
  • Loading branch information
alexnault committed May 21, 2015
1 parent 539c2de commit c2c983c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 40 deletions.
75 changes: 43 additions & 32 deletions lib/projection.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
'use strict';

var request = require('request'),
cheerio = require('cheerio');
cheerio = require('cheerio'),
qs = require('query-string'),
_ = require('underscore');

var Projection = function() {
this.GOOGLE_ENDPOINT = 'http://www.google.com/movies';
};

// Find showtimes of nearby theaters
Projection.prototype.findTheaters = function(near, callback) {
var url = this.GOOGLE_ENDPOINT + '?near=' + near;

request(url, function (error, response, body) {
if (error) {
callback(error);
return;
}

if (response.statusCode !== 200) {
callback(response.statusCode);
Projection.prototype.findTheaters = function(near, options, callback) {

var url = this.GOOGLE_ENDPOINT + toQS(options, near);
//console.log(url)
request(url, function (err, res, body) {
err = errorCheck(err, res);
if (err) {
callback(err);
return;
}

Expand Down Expand Up @@ -71,19 +70,14 @@ Projection.prototype.findTheaters = function(near, callback) {

};

// q=movie+name
// Find a movie showtimes in nearby theaters
Projection.prototype.findMovie = function(near, movieName, callback) {
var url = this.GOOGLE_ENDPOINT + '?near=' + near + '&q=' + "Furious+7";

request(url, function (error, response, body) {
if (error) {
callback(error);
return;
}

if (response.statusCode !== 200) {
callback(response.statusCode);
Projection.prototype.findMovie = function(near, movie, options, callback) {
var url = this.GOOGLE_ENDPOINT + toQS(options, near, movie);
//console.log(url);
request(url, function (err, res, body) {
err = errorCheck(err, res);
if (err) {
callback(err);
return;
}

Expand All @@ -94,9 +88,9 @@ Projection.prototype.findMovie = function(near, movieName, callback) {
var content = m.find('.desc .info').not('.info.links').html().split('<br>');

var persons = $(content[1]).text();
console.log(persons);
//console.log(persons);
var categories = persons.split(' - ');
var director = categories[0].replace('Director: ', '');
var director = categories[0].replace('Director: ', ''); // TODO must be multi-lang
var cast = categories[1].replace('Cast: ', '').split(', ');

var infos = formatInfos(content[0].split(' - '));
Expand Down Expand Up @@ -137,6 +131,23 @@ Projection.prototype.findMovie = function(near, movieName, callback) {

};

var toQS = function(options, near, movie){
var args = _.pick(options, 'hl', 'date');
if (near) args.near = near;
if (movie) args.movie = movie;
return '?' + qs.stringify(args);
};

var errorCheck = function(error, response){
if (error) {
return error;
}
if (response.statusCode !== 200) {
return response.statusCode;
}
return null;
};

var formatInfos = function(infos) {
var cursor = 0,
duration = null,
Expand Down Expand Up @@ -173,12 +184,12 @@ var formatShowtimes = function(showtimes) {
});
};

var p = new Projection();
// p.findTheaters('Montreal', function(err, theaters) {
// console.log(theaters[2].movies[0 ]);
// var p = new Projection();
// p.findTheaters('Sherbrooke', {}, function(err, theaters) {
// console.log(theaters[2].movies[0]);
// });
// p.findMovie('Sherbrooke', 'Mad Max', { hl:'fr'}, function(err, movie) {
// console.log(movie);
// });
p.findMovie('Montreal', 'Furious 7', function(err, movie) {
console.log(movie);
});

module.exports = Projection;
33 changes: 25 additions & 8 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('Projection', function(){

describe('#findTheaters()', function(){
it('should find theaters by town', function(done){
p.findTheaters('Montreal', function(err, theaters) {
p.findTheaters('Montreal', {}, function(err, theaters) {
//console.log(theaters);

assert.equal(err, null);
Expand All @@ -37,30 +37,30 @@ describe('Projection', function(){
})

it('should find theaters by zipcode', function(done){
p.findTheaters('Montreal', function(err, theaters) {
p.findTheaters('Montreal', {}, function(err, theaters) {
assert.equal(err, null);
done();
});
})

it('should find theaters by lat/long', function(done){
p.findTheaters('Montreal', function(err, theaters) {
p.findTheaters('Montreal', {}, function(err, theaters) {
assert.equal(err, null);
done();
});
})

it('should return requestjs error', function(done){
p.GOOGLE_ENDPOINT = 'abc'; // Override endpoint
p.findTheaters('Montreal', function(err, theaters) {
assert.equal(err, 'Error: Invalid URI "abc?near=Montreal"');
p.findTheaters('Montreal', {}, function(err, theaters) {
assert.notEqual(err, null);
done();
});
})

it('should return a 404 error', function(done){
p.GOOGLE_ENDPOINT = 'http://httpstat.us/404' // Override endpoint
p.findTheaters('Montreal', function(err, theaters) {
p.findTheaters('Montreal', {}, function(err, theaters) {
assert.equal(err, 404);
done();
});
Expand All @@ -76,8 +76,25 @@ describe('Projection', function(){

describe('#findMovie()', function(){
it('should find a movie\'s showtimes', function(done){
p.findMovie();
done();
p.findMovie('Sherbrooke', 'Mad Max', {}, function(err, movie){
done();
});
})

it('should return requestjs error', function(done){
p.GOOGLE_ENDPOINT = 'abc'; // Override endpoint
p.findMovie('Montreal', 'Mad Max', {}, function(err, theaters) {
assert.notEqual(err, null);
done();
});
})

it('should return a 404 error', function(done){
p.GOOGLE_ENDPOINT = 'http://httpstat.us/404' // Override endpoint
p.findMovie('Montreal', 'Mad Max', {}, function(err, theaters) {
assert.equal(err, 404);
done();
});
})
})
})

0 comments on commit c2c983c

Please sign in to comment.