Skip to content

Commit

Permalink
added image grabber.
Browse files Browse the repository at this point in the history
i also add dummy file within movie's image directory so that directory could be added into git
  • Loading branch information
killedbymemory committed May 10, 2012
1 parent 03960f3 commit 7999651
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions grab-image.js
@@ -0,0 +1,82 @@
var fs = require('fs'),
path = require('path'),
request = require('request'),
qs = require('qs');


/**
* example: http://m.21cineplex.com/image_preview.php?type=movie&images=12AVES.jpg&width=100&height=147
* extract width, height, and file extension
*/
function extractImageInformation(url) {
var image = {
width: 0,
height: 0,
ext: 'jpg'
};

var params = qs.parse(url);

try {
image.width = parseInt(params.width);
image.height = parseInt(params.height);
image.ext = params.images.toLowerCase().match(/\.(?:jpg|jpeg|png)/)[0];
} catch (e) {
console.log(e.stack);
}

return image;
}

/**
* @param object movie movie detail (movie id is mandatory)
* @param object image image detail (resolution, name, extension)
*/
function storeMovieImage(movie) {
var createFile = false;

// create path
var file_path = ['images', 'movie', movie.id].join('/');

fs.mkdir(file_path, 0755, function(err){
console.log('create direktori. path: ' + file_path);

if (err) {
console.log(err);

switch (err.code) {
case 'EEXIST':
console.log('direktori sudah ada');
createFile = true;
break;

default:
console.log(err.toString());
console.log('error on mkdir');
break;
}
} else {
createFile = true;
}

if (createFile) {
file_path += '/' + [movie.id, image.width, image.height].join('_') + '.' + image.ext;
console.log('create file. path: ', file_path);

path.exists(file_path, function(exists){
if (!exists) {
console.log('file created.');
request(movie.url).pipe(fs.createWriteStream(path, {mode: 0755}));
} else {
console.log('file already exist.');
}
});
}
});
}
module.exports = storeMovieImage;


console.log(extractImageInformation(movie.url));

storeMovieImage(movie);
Empty file added images/movie/dummy
Empty file.

0 comments on commit 7999651

Please sign in to comment.