Created
May 20, 2012 23:56
-
-
Save silentrob/2759975 to your computer and use it in GitHub Desktop.
Plex + CP + Trakt Trending Updater
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
{ | |
"author": "Rob Ellis <[email protected]>", | |
"name":"trakt_trending", | |
"version" : "0.0.1", | |
"dependencies": { | |
"request" : "2.9.202", | |
"xml2js": "*" | |
} | |
} |
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
var request = require('request'); | |
var xml2js = require('xml2js'); | |
var qs = require('querystring'); | |
// Trakt.tv | |
var traktApiKey = "79470745f68b8b6a5732fdf06c067f16"; | |
// Couch Settings | |
var cpHost = "http://192.168.0.196:5050"; | |
var cpApiKey = "8c0dabc86b6a4a34903adefdd6fb6b30"; | |
// Plex Settings | |
var plexHost = "http://192.168.0.196:32400"; | |
// Util function | |
function inArray(needle, haystack) { | |
var length = haystack.length; | |
for(var i = 0; i < length; i++) { | |
if(haystack[i] == needle) return true; | |
} | |
return false; | |
} | |
function getRandomInt (min, max) { | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
// Fetch movies from PLEX Media Server, returns a callback with an array of titles | |
var fetchPlexMovies = function(cb) { | |
var movies = []; | |
var parser = new xml2js.Parser(); | |
parser.addListener('end', function(result) { | |
for (var i = 0; i < result['Video'].length; i++) { | |
movies.push(result['Video'][i]['@'].title); | |
} | |
cb(movies) | |
}); | |
// Movie Section = 4 | |
request(plexHost + '/library/sections/4/all', function (error, response, body) { | |
if (!error && response.statusCode == 200) { | |
parser.parseString(body); | |
} | |
}); | |
} | |
// POST a new movie to CouchPotato (CP uses GETS for everything!) | |
// profile_id 12 is best, but should be optional | |
var addMovieToCouchPotato = function(obj) { | |
var params = {'identifier':obj.imdb, 'title': encodeURIComponent(obj.title), 'profile_id': 12 } | |
request(cpHost + '/api/'+ cpApiKey +'/movie.add/?' + qs.stringify(params), | |
function (error, response, body) { | |
if (!error && response.statusCode == 200) { | |
console.log(body) | |
} | |
} | |
); | |
} | |
var getTraktTrending = function(callback) { | |
request('http://api.trakt.tv/movies/trending.json/' + traktApiKey, function (error, response, body) { | |
if (!error && response.statusCode == 200) { | |
var json = JSON.parse(body); | |
callback(json) | |
} | |
}); | |
} | |
// Main entry point | |
// Run's daily | |
setInterval( | |
fetchPlexMovies(function(movies) { | |
var parseResults = function(json) { | |
var recomended = []; | |
for (var i = 0; i < json.length; i++) { | |
if (inArray('Horror',json[i].genres) === false && | |
inArray('Music',json[i].genres) === false && | |
inArray(json[i].title, movies) === false && | |
json[i]['ratings'].percentage >= 78 && | |
json[i].year >= 2005 | |
) { | |
recomended.push({imdb:json[i].imdb_id,title:json[i].title}); | |
} | |
} | |
// Fetch 1 at a time (once a day) | |
addMovieToCouchPotato(recomended[getRandomInt(0, recomended.length)]); | |
} | |
getTraktTrending(parseResults); | |
}), | |
86400000 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment