Skip to content

Commit

Permalink
Refactor method for printing a movie
Browse files Browse the repository at this point in the history
  • Loading branch information
lucassus committed Jul 23, 2015
1 parent fb6a342 commit ce9f553
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 48 deletions.
5 changes: 4 additions & 1 deletion bin/what2watch
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ Q(program.directory).then(function(directory) {
.reverse()
.value();
}).then(function(stats) {
_.each(stats, printMovie);
_.each(stats, function(stat) {
console.log('\n---------------------\n');
console.log(printMovie(stat));
});
}).catch(function(err) {
console.log('err:', err);
process.exit(1);
Expand Down
10 changes: 6 additions & 4 deletions lib/collect_details.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
var Q = require('q');
var _ = require('lodash');
var Q = require('q');

module.exports = function(api, stats) {
var promise = Q.promise(function(resolve, reject, notify) {
api.login().then(function() {
var promises = _.map(stats, function(stat) {
var promise = stat.movie && stat.movie.imdbId ?
api.getIMDBMovieDetails(stat.movie.imdbId) :
Q.reject('missing');
var imdbId = _.get(stat, 'movie.imdbId');

var promise = imdbId ?
api.getIMDBMovieDetails(imdbId) :
Q.reject('Missing id:', imdbId);

return promise.then(function(details) {
_.extend(stat.movie, details);
Expand Down
39 changes: 39 additions & 0 deletions lib/print_move_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
var chalk = require('chalk');
var expect = require('chai').expect;

var printMovie = require('./print_movie');

describe('.printMovie', function() {

var stat = {
movie: {
imdbId: '0121766',
title: 'Star Wars',

rating: '7.7',
plot: 'As the Clone Wars near an end, the Sith Lord Darth Sidious steps out of the shadows...',
year: '2005',
duration: '120 min',
genres: ['Fantasy', 'Sci-Fi'],
directors: ['George Lucas'],
cast: ['Ewan McGregor', 'Natalie Portman'],
country: ['USA'],
language: ['English']
},
file: {
path: '/Downloads/Star Wars.avi'
}
};

it('prints a movie', function() {
var result = printMovie(stat);

expect(result).to.include('Star Wars');
expect(result).to.include('Year: ' + chalk.cyan('2005'));
expect(result).to.include('Duration: ' + chalk.cyan('120 min'));
expect(result).to.include('Genres: ' + chalk.cyan('Fantasy') + ', ' + chalk.cyan('Sci-Fi'));
expect(result).to.include('Country: ' + chalk.cyan('USA'));
expect(result).to.include(chalk.cyan('http://www.imdb.com/title/tt00121766'));
});

});
66 changes: 25 additions & 41 deletions lib/print_movie.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,34 @@
require('colors');
var _ = require('lodash');
var fs = require('fs');
var chalk = require('chalk');
var path = require('path');

module.exports = function(stat) {
console.log('\n----------------------------\n');

console.log(stat.movie.title.bold.blue);

if (stat.movie.plot) {
console.log(stat.movie.plot.italic.grey);
}
console.log();

if (stat.movie.rating) {
console.log('Rating:'.yellow, stat.movie.rating.cyan);
}
var tpl = fs.readFileSync(path.resolve(__dirname, 'print_movie.tpl.txt'));
var compiled = _.template(tpl.toString().trim());

if (stat.movie.year) {
console.log('Year:'.yellow, stat.movie.year.cyan);
}
function formatCollection(collection) {
return collection.map(function(item) {
return chalk.cyan(item.trim());
}).join(', ');
}

if (stat.movie.duration) {
console.log('Duration:'.yellow, stat.movie.duration.cyan);
}

if (stat.movie.genres) {
console.log('Genre:'.yellow, _.map(stat.movie.genres, function(genre) { return genre.trim().blue; }).join(', '));
}

if (stat.movie.directors) {
console.log('Directors:'.yellow, _.values(stat.movie.directors).join(', '));
}
module.exports = function(stat) {
return compiled({
title: chalk.bold.blue(_.get(stat, 'movie.title', '')),
plot: chalk.italic.grey(_.get(stat, 'movie.plot', '')),

if (stat.movie.cast) {
console.log('Cast:'.yellow, _.values(stat.movie.cast).slice(0, 7).join(', '));
}
rating: chalk.cyan(_.get(stat, 'movie.rating', '')),
year: chalk.cyan(_.get(stat, 'movie.year', '')),
duration: chalk.cyan(_.get(stat, 'movie.duration', '')),
genres: formatCollection(_.get(stat, 'movie.genres', [])),

if (stat.movie.country && stat.movie.language) {
console.log(
'Country:'.yellow, stat.movie.country.join(', '),
'Language:'.yellow, stat.movie.language.join(', ')
);
}
directors: formatCollection(_.get(stat, 'movie.directors', [])),
cast: formatCollection(_.get(stat, 'movie.cast', []).slice(0, 7)),

if (stat.movie.imdbId) {
console.log('Url:'.yellow, 'http://www.imdb.com/title/tt0' + stat.movie.imdbId);
}
country: formatCollection(_.get(stat, 'movie.country', [])),
language: formatCollection(_.get(stat, 'movie.language', [])),

console.log('File:'.yellow, stat.file.path);
imdbUrl: chalk.cyan('http://www.imdb.com/title/tt0' + _.get(stat, 'movie.imdbId', '')),
filePath: chalk.cyan(_.get(stat, 'file.path'))
});
};
13 changes: 13 additions & 0 deletions lib/print_movie.tpl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<%= title %>
<%= plot %>

Rating: <%= rating %>
Year: <%= year %>
Duration: <%= duration %>
Genres: <%= genres %>
Directors: <%= directors %>
Cast: <%= cast %>
Country: <%= country %>
Language: <%= language %>
Imdb: <%= imdbUrl %>
File: <%= filePath %>
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"name": "what2watch",
"version": "0.1.1",
"description": "Simple tool for collecting movie details",
"main": "./index.js",
"bin": {
"what2watch": "./bin/what2watch"
},
Expand All @@ -16,7 +15,7 @@
"author": "Lukasz Bandzarewicz",
"license": "ISC",
"dependencies": {
"colors": "^1.1.2",
"chalk": "^1.1.0",
"commander": "^2.8.1",
"glob": "^5.0.13",
"lodash": "^3.10.0",
Expand Down

0 comments on commit ce9f553

Please sign in to comment.