Skip to content

Instantly share code, notes, and snippets.

@grimpy
Last active August 29, 2015 14:01
Show Gist options
  • Save grimpy/362fd4f200612c43c19d to your computer and use it in GitHub Desktop.
Save grimpy/362fd4f200612c43c19d to your computer and use it in GitHub Desktop.
AutoPebble XBMC Script
var title = "";
var labels = "";
var actions = "";
var windowid = "";
var moviequery = {'properties': ['title'],
'sort': {'order':'ascending',
'method': 'label'}
};
var XBMCHOST = global('XBMCHOST');
var log = function(data) {
alert(JSON.stringify(data));
};
var callXBMC = function(method, params) {
var jsonrpc = {'jsonrpc': '2.0',
'id': 1,
'method': method,
'params': params };
var request = JSON.stringify(jsonrpc);
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('POST', 'http://' + XBMCHOST + '/jsonrpc', false);
xmlhttp.setRequestHeader('Accept', 'application/json, text/javascript, *.*; q=0.01');
xmlhttp.setRequestHeader('Content-Type', 'application/json');
//xmlhttp.timeout = 5000;
xmlhttp.send(request);
if (xmlhttp.status == 200) {
return JSON.parse(xmlhttp.responseText);
} else {
log(xmlhttp.responseText);
exit();
}
};
var processList = function(rname, dfield, idfield, data, action, navaction, start) {
start = start || 0;
var mylist = data['result'][rname];
var item;
var length = mylist.length;
if (length > 17) {
if (start == 0 && length >= 18) {
length = 18;
} else {
if (length - start > 17) {
length = start + 17;
}
labels += "Previous..~";
var newbegin = start - 16;
if (start == 18) {
newbegin = 0;
}
actions += navaction + ":" + newbegin + "~";
length--;
}
}
for (var i=start; i < length; i++) {
item = mylist[i];
labels += item[dfield] + "~";
actions += action + ":" + item[idfield] + "~";
}
if (length != mylist.length - 1) {
labels += "Next ...";
var xtra = start == 0 ? 18 : 16;
actions += navaction + ":" + (start+xtra) + "~";
}
};
var getMovies = function(start) {
title = "Movies";
var data = callXBMC('VideoLibrary.GetMovies', moviequery);
processList('movies', 'label', 'movieid', data, "watchmovie", "movies", start);
};
var getSeries = function(start) {
title = "Movies";
var data = callXBMC('VideoLibrary.GetTVShows', moviequery);
processList('tvshows', 'label', 'tvshowid', data, "seasons", "series", start);
};
var getSeasons = function(args) {
title = "Seasons";
var tvshowid = parseInt(args[0]);
var start = 0;
if (args.length > 1) {
start = parseInt(args[1]);
}
var data = callXBMC('VideoLibrary.GetSeasons', {'tvshowid': tvshowid});
var mylist = data['result']['seasons'];
var item;
for (var i=0; i < mylist.length; i++) {
item = mylist[i];
item['seasonid'] = tvshowid + ":" + (i+1);
}
processList('seasons', 'label', 'seasonid', data, "episodes", "seasons", start);
};
var getEpisodes = function(args) {
var tvshowid = parseInt(args[0]);
var season = parseInt(args[1]);
var start = 0;
if (args.length > 2) {
start = parseInt(args[2]);
}
var data = callXBMC('VideoLibrary.GetEpisodes', {'tvshowid': tvshowid, 'season': season});
var navaction = "episodes:" + tvshowid + ":" + season;
processList('episodes', 'label', 'episodeid', data, "watchepisode", navaction, start);
};
var startMovie = function(movieid) {
var startparm = {"item":{"movieid": movieid}};
callXBMC('Player.Open', startparm);
};
var startEpisode = function(epid) {
var startparm = {"item":{"episodeid": epid}};
callXBMC('Player.Open', startparm);
};
var mainMenu = function() {
title = "XBMC";
windowid = "mainmenu";
labels = "Controls~Movies~Series";
actions = "controls~movies~series";
};
var getInt = function(args) {
if (args.length > 0 ) {
return parseInt(args[0]);
}
return 0;
}
var execAction = function(args) {
var command = args[0];
if (command == "pause") {
callXBMC('Player.PlayPause', {'playerid': 1});
} else {
callXBMC('Input.' + command, {});
}
}
var control = function(args) {
title = "Control";
labels = "Up~Down~Select~Play/Pause";
actions = "action:Up~action:Down~action:Select~action:pause";
}
var action = (apcomm || "").split(":");
var method = action[0];
var args = action.slice(1, action.length);
//var action = global('ACTION');
//var title = "";
//var apcomm = global('APCOMM');
//
console.log(method, args);
switch(method) {
case "mainmenu":
mainMenu();
break;
case "watchmovie":
startMovie(getInt(args));
break;
case "watchepisode":
startEpisode(getInt(args));
break;
case "movies":
getMovies(getInt(args));
break;
case "series":
getSeries(getInt(args));
break;
case "seasons":
getSeasons(args);
break;
case "episodes":
getEpisodes(args);
break;
case "controls":
control();
break;
case "action":
execAction(args);
break;
default:
exit();
};
setLocal('title', title);
if (labels) {
setLocal('labels', labels);
}
setLocal('actions', actions);
setLocal('windowid', windowid);
exit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment