@@ -4,7 +4,8 @@ var dice = require('clj-fuzzy').metrics.dice;
4
4
var alexa = require ( 'alexa-app' ) ;
5
5
var app = new alexa . app ( 'plex' ) ;
6
6
var plexAPI = require ( 'plex-api' ) ;
7
- var AWS = require ( 'aws-sdk' ) ;
7
+ var PlexPinAuth = require ( 'plex-api-pinauth' ) ;
8
+ var db = require ( './lib/db.js' ) ;
8
9
9
10
var CONFIDICE_CONFIRM_THRESHOLD = 0.4 ;
10
11
@@ -15,11 +16,14 @@ var plexOptions = {
15
16
deviceName : process . env . APP_DEVICE_NAME ,
16
17
identifier : process . env . APP_IDENTIFIER
17
18
} ;
19
+
20
+ var pinAuth = new PlexPinAuth ( ) ;
18
21
var plex = new plexAPI ( {
19
22
hostname : process . env . PMS_HOSTNAME ,
20
- username : process . env . PMS_USERNAME ,
23
+ // username: process.env.PMS_USERNAME,
21
24
port : process . env . PMS_PORT ,
22
- password : process . env . PMS_PASSWORD ,
25
+ //password: process.env.PMS_PASSWORD,
26
+ authenticator : pinAuth ,
23
27
options : plexOptions
24
28
} ) ;
25
29
@@ -28,10 +32,9 @@ var plex = new plexAPI({
28
32
plex . authToken = process . env . PMS_AUTHTOKEN ;
29
33
30
34
// Connect the alexa-app to AWS Lambda
31
- //exports.handler = app.lambda();
32
35
exports . handler = function ( event , context ) {
33
-
34
- console . log ( "Request: " , event . request ) ;
36
+ console . log ( "Request: " , event . request ) ;
37
+ console . log ( "Session: " , event . session ) ;
35
38
36
39
if ( event . request . intent ) {
37
40
if ( event . request . intent . slots ) {
@@ -64,87 +67,49 @@ app.launch(function(request,response) {
64
67
// response.send();
65
68
//};
66
69
67
- app . intent ( 'DBTestIntents' , function ( request , response ) {
68
- AWS . config . update ( {
69
- region : "us-east-1" ,
70
- endpoint : "http://localhost:8000"
71
- } ) ;
72
-
73
- var dynamodb = new AWS . DynamoDB ( ) ;
74
-
75
- var params = {
76
- TableName : "AlexaPlexUsers" ,
77
- KeySchema : [
78
- { AttributeName : "userid" , KeyType : "HASH" } , //Partition key
79
- ] ,
80
- AttributeDefinitions : [
81
- { AttributeName : "userid" , AttributeType : "S" } ,
82
- ] ,
83
- ProvisionedThroughput : {
84
- ReadCapacityUnits : 10 ,
85
- WriteCapacityUnits : 10
86
- }
87
- } ;
88
-
89
- dynamodb . createTable ( params , function ( err , data ) {
90
- if ( err ) {
91
- console . error ( "Unable to create table. Error JSON:" , JSON . stringify ( err , null , 2 ) ) ;
70
+ app . intent ( 'OnDeckIntent' , function ( request , response ) {
71
+ db . initializeUserRecord ( request . data . session . user . userId ) . then ( function ( user ) {
72
+ console . log ( "All is good!" ) ;
73
+ if ( ! user . authtoken ) {
74
+ response . say ( "You do not have an auth token. Please run setup. Note: setup doesn't exist yet!" ) ;
75
+ response . send ( ) ;
92
76
} else {
93
- console . log ( "Created table. Table description JSON:" , JSON . stringify ( data , null , 2 ) ) ;
94
- }
77
+ // User is authed
95
78
96
- var getItemParams = {
97
- "TableName" : "AlexaPlexUsers" ,
98
- "Key" : {
99
- "userid" : {
100
- "S" : request . userId
79
+ //pinAuth.token = user.authtoken;
80
+
81
+ plex . query ( '/library/onDeck' ) . then ( function ( result ) {
82
+ if ( result . _children . length === 0 ) {
83
+ response . say ( "You do not have any shows On Deck!" ) ;
84
+ return response . send ( ) ;
101
85
}
102
- }
103
- } ;
104
86
105
- dynamodb . getItem ( getItemParams , function ( err , data ) {
106
- if ( err ) {
107
- console . error ( "Unable to query. Error:" , JSON . stringify ( err , null , 2 ) ) ;
108
- } else {
109
- console . log ( "Query succeeded." ) ;
110
- console . log ( data ) ;
111
- //data.Items.forEach(function(item) {
112
- // console.log(" -", item.year + ": " + item.title);
113
- //});
114
- }
115
- } ) ;
116
- } ) ;
87
+ var shows = [ ] ;
117
88
118
- return false ; // This is how you tell alexa-app that this intent is async.
119
- } ) ;
89
+ for ( i = 0 ; i < result . _children . length && i < 6 ; i ++ ) {
90
+ shows . push ( result . _children [ i ] . grandparentTitle ) ;
91
+ }
120
92
121
- app . intent ( 'OnDeckIntent ', function ( request , response ) {
122
- plex . query ( '/library/onDeck' ) . then ( function ( result ) {
93
+ var showsPhraseHyphenized = buildNaturalLangList ( shows , 'and ', true ) ;
94
+ var showsPhrase = buildNaturalLangList ( shows , 'and' ) ;
123
95
124
- if ( result . _children . length === 0 ) {
125
- response . say ( "You do not have any shows On Deck!" ) ;
126
- return response . send ( ) ;
127
- }
96
+ //console.log(result);
128
97
129
- var shows = [ ] ;
98
+ response . say ( "On deck you've got " + showsPhraseHyphenized + '.' ) ;
99
+ response . card ( 'Plex' , showsPhrase + '.' , 'On Deck' ) ;
100
+ return response . send ( ) ;
101
+ } ) . catch ( function ( err ) {
102
+ console . log ( "ERROR from Plex API on Query /library/onDeck" ) ;
103
+ console . log ( err ) ;
104
+ response . say ( "I'm sorry, Plex and I don't seem to be getting along right now" ) ;
105
+ return response . send ( ) ;
106
+ } ) ;
130
107
131
- for ( i = 0 ; i < result . _children . length && i < 6 ; i ++ ) {
132
- shows . push ( result . _children [ i ] . grandparentTitle ) ;
133
108
}
134
-
135
- var showsPhraseHyphenized = buildNaturalLangList ( shows , 'and' , true ) ;
136
- var showsPhrase = buildNaturalLangList ( shows , 'and' ) ;
137
-
138
- //console.log(result);
139
-
140
- response . say ( "On deck you've got " + showsPhraseHyphenized + '.' ) ;
141
- response . card ( 'Plex' , showsPhrase + '.' , 'On Deck' ) ;
142
- response . send ( ) ;
143
109
} ) . catch ( function ( err ) {
144
- console . log ( "ERROR from Plex API on Query /library/onDeck" ) ;
145
- console . log ( err ) ;
146
- response . say ( "I'm sorry, Plex and I don't seem to be getting along right now" ) ;
147
- response . send ( ) ;
110
+ console . error ( err ) ;
111
+ response . say ( "There was a database error. Try again later!" ) ;
112
+ response . send ( )
148
113
} ) ;
149
114
150
115
return false ; // This is how you tell alexa-app that this intent is async.
0 commit comments