Skip to content

Commit

Permalink
Query for recently added media
Browse files Browse the repository at this point in the history
Adds support for asking Alexa for recently added shows or movies. The
intent supports querying for just shows, just movies, or for both.
  • Loading branch information
josephschmitt committed Jan 18, 2016
1 parent ef996a9 commit f814d9f
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 5 deletions.
4 changes: 4 additions & 0 deletions ask_configuration/LIBRARYTYPE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
TV Shows
Shows
Movies
Films
9 changes: 9 additions & 0 deletions ask_configuration/intent-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@
"intent": "OnDeckIntent",
"slots": []
},
{
"intent": "RecentlyAddedIntent",
"slots": [
{
"name": "libraryType",
"type": "LIBRARYTYPE"
}
]
},
{
"intent": "AMAZON.YesIntent"
},
Expand Down
14 changes: 12 additions & 2 deletions ask_configuration/sample-utterances.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,18 @@ AuthorizeMeIntent Authorize me
AuthorizeMeIntent Sign in to Plex
AuthorizeMeIntent Link my account
WhatsNewIntent Release notes
OnDeckIntent What's new
OnDeckIntent What is new
RecentlyAddedIntent What's new
RecentlyAddedIntent What is new
RecentlyAddedIntent What's new in {libraryType}
RecentlyAddedIntent What is new in {libraryType}
RecentlyAddedIntent What are my new {libraryType}
RecentlyAddedIntent What are my recently added {libraryType}
RecentlyAddedIntent What are the new {libraryType}
RecentlyAddedIntent Latest {libraryType}
RecentlyAddedIntent About the latest {libraryType}
RecentlyAddedIntent Recently added {libraryType}
RecentlyAddedIntent About the recently added {libraryType}
RecentlyAddedIntent About recently added {libraryType}
OnDeckIntent What is on deck
OnDeckIntent What's on deck
StartShowIntent Put on {showName}
Expand Down
19 changes: 18 additions & 1 deletion lib/plexutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ var getOnDeck = function(app, library) {
}
};

/**
* Gets a list of items that have been recently added, either for all library types or a specific one.
* @param {module:App~App} app
* @param {?Object} library - Optional library object to ask for only items in that library
* @returns {Object} JSON object of shows/movies/etc recently added
*/
var getRecentlyAdded = function(app, library) {
if (library) {
return query(app.plex.pms, library.uri + '/recentlyAdded');
} else {
return query(app.plex.pms, '/library/recentlyAdded');
}
};

/**
* @typedef {Object} PlexAPI.Directory
*/
Expand Down Expand Up @@ -340,7 +354,9 @@ var getShowNamesFromList = function(apiResult) {
for(i = 0; i < apiResult._children.length && i < 6; i++) {
if(apiResult._children[i].type == 'episode') {
shows.push(apiResult._children[i].grandparentTitle);
} else if (apiResult._children[i].type == 'movie') {
} else if (apiResult._children[i].type == 'season') {
shows.push(apiResult._children[i].parentTitle);
}else if (apiResult._children[i].type == 'movie') {
shows.push(apiResult._children[i].title);
}
}
Expand All @@ -359,6 +375,7 @@ module.exports = {
getPlayers: getPlayers,
startShow: startShow,
getOnDeck: getOnDeck,
getRecentlyAdded: getRecentlyAdded,
getListOfTVShows: getListOfTVShows,
getAllEpisodesOfShow: getAllEpisodesOfShow,
playMedia: playMedia,
Expand Down
38 changes: 36 additions & 2 deletions lib/states/authed.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,41 @@ var onDeckIntent = function(request, response) {
.card("TV Shows Ready to Watch", "On Deck in your TV library: \n\n" + showListCard)
.send();
}).catch(function(err) {
app.skill.error(err, request, response);
});
app.skill.error(err, request, response);
});

return false;
};

var recentlyAddedIntent = function (request, response) {
var app = request.data._plex_app;
var libraryType = request.slot('libraryType', null);
var library;

if (libraryType === 'tv shows' || libraryType === 'shows') {
library = app.user.TVLibrary;
} else if (libraryType === 'movies' || libraryType === 'films') {
library = app.user.MovieLibrary;
}

plexutils.getRecentlyAdded(app, library)
.then(plexutils.getShowNamesFromList)
.then(function (mediaList) {
if(mediaList.length === 0) {
return response.say("You have not added any media recently!").send();
}

var showListCard = mediaList.join('\n');
var showSpokenListHyphenated = utils.buildNaturalLangList(mediaList, 'and', true);
var libraryTypeTitle = ("Recently Added " + (utils.toTitleCase(libraryType) || '')).trim();

return response.say(libraryTypeTitle + ": " + showSpokenListHyphenated + '.')
.card(libraryTypeTitle, showListCard)
.send();
})
.catch(function(err) {
app.skill.error(err, request, response);
});

return false;
};
Expand Down Expand Up @@ -220,6 +253,7 @@ var noIntent = function(request,response) {
module.exports = {
intents: {
'OnDeckIntent': onDeckIntent,
'RecentlyAddedIntent': recentlyAddedIntent,
'StartShowIntent': startShowIntent,
'StartRandomShowIntent': startRandomShowIntent,
'StartSpecificEpisodeIntent': startSpecificEpisodeIntent,
Expand Down
19 changes: 19 additions & 0 deletions lib/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@ var User = function(app, dbobject) {
}
});

Object.defineProperty(this, 'MovieLibrary', {
get: function() {
if (!context.dbobject.libraries) {
throw new Error("Trying to get MovieLibrary with no libraries on the user record");
}

var libraries = context.dbobject.libraries.filter(function(library) {
return library.type == "movie";
});

// For now we're going to sort by key, which more or less gives us a sort by creation date.
libraries.sort(function(a, b) {
return Number(a.key) - Number(b.key);
});

return libraries[0];
}
});

Object.defineProperty(this, 'TVLibrary', {
get: function() {
if (!context.dbobject.libraries) {
Expand Down
19 changes: 19 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,23 @@ module.exports.buildNaturalLangList = function(items, finalWord, hyphenize) {

module.exports.randomInt = function(low, high) {
return Math.floor(Math.random() * (high - low) + low);
};

/**
* Super simple title-casing, does not handle lots of things well. Best to just use with simple
* complete words, no acronyms or anything complicated;
* @param {String} string -- String to change to title case
* @return {String} Title-cased string
*/
module.exports.toTitleCase = function(string) {
var exceptions = ['a', 'an', 'the', 'at', 'by', 'for', 'in', 'of', 'on', 'to', 'up', 'and', 'as', 'but', 'or', 'nor'];
if (string) {
string = string.split(' ').map(function(word) {
return exceptions.find(function(compare) {
return compare === word.toLowerCase();
}) ? word : word.charAt(0).toUpperCase() + word.slice(1);
}).join(' ');
}

return string;
};

0 comments on commit f814d9f

Please sign in to comment.