Created
December 28, 2014 23:19
-
-
Save DanielFGray/501be2141a978ba84a02 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
var fs = require('fs'), | |
path = require('path'), | |
http = require('http'), | |
exec = require('child_process').exec; | |
var globals = { | |
apiKey: '' | |
}; | |
var main = function(dir) { | |
exec("command find ~/videos -type f", function(error, stdout, stderr) { | |
if(error || stderr) { | |
return; | |
} | |
var videoList = stdout.replace(/\n*$/, '').split('\n'); | |
for (var i=0; i < videoList.length; ++i) { | |
if(! /\(\d{4}\)/.test(videoList[i])) { | |
apiCall(videoList[i]); | |
break; | |
} | |
} | |
}); | |
}; | |
var apiCall = function(str) { | |
var video = path.basename(str); | |
video = video.substr(0, video.lastIndexOf('.')); | |
console.log('starting api call for "' + video + '"'); | |
var req = http.get({ | |
hostname: 'api.rottentomatoes.com', | |
path: '/api/public/v1.0/movies.json?page_limit=1&page=1&apikey=' + globals.apiKey + '&q=' + encodeURIComponent(video) | |
}, function(res) { | |
res.on('data', function(chunk) { | |
var json = JSON.parse(chunk); | |
if(json.movies.length === 1) { | |
console.log('found match for ' + video); | |
renameFile(str, json.movies[0].year); | |
} else if(json.movies.length > 1) { | |
console.log(json.movies.length + ' results for "' + video + '"'); | |
json.movies.forEach(function(e) { | |
console.log('"' + video + '" could be from ' + e.year); | |
}); | |
} else { | |
console.log('no results for ' + video); | |
} | |
}); | |
}).on('error', function(e) { | |
console.log("API error: ", e.message); | |
}); | |
}; | |
var renameFile = function(file, year) { | |
var dirname = path.dirname(file), | |
filename = path.basename(file); | |
// console.log('you should rename ' + file + ' to ' + dirname + '/(' + year + ') ' + filename); | |
fs.rename(file, dirname + '/(' + year + ') ' + filename, function(err) { | |
if (err) throw err; | |
console.log('renamed complete'); | |
}); | |
}; | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment