This repository has been archived by the owner on Sep 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
276 lines (241 loc) · 10.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
var Alexa = require("alexa-sdk");
const Mal = require('mal-api')
var converter = require('number-to-words')
const password = "konohanakitananimeoftheseason";
const username = "beforged";
const mal = new Mal({
username,
password,
})
exports.handler = function(event, context, callback){
let alexa = Alexa.handler(event, context);
alexa.registerHandlers(handlers);
alexa.execute();
};
const handlers = {
'AMAZON.CancelIntent': function () {
this.emit(':tell', "Sayonara!");
},
'AMAZON.HelpIntent': function() {
this.response.speak("This is a program that gives you information about your favorite anime. Simply ask anime list look up any anime, and I will respond to you. I will provide the basic information that you need to judge whether or not to watch your anime. I will tell you the rating, number of episodes, the type of anime, as well as a synopsis if you so choose.");
this.emit(':responseReady');
// this.emit(':tell', "This is a program that gives you information about your favorite anime. Simply ask anime list look up any anime, and I will respond to you. I will provide the basic information that you need to judge whether or not to watch your anime. I will tell you the rating, number of episodes, the type of anime, as well as a synopsis if you so choose.");
},
'AMAZON.StopIntent': function() {
this.emit(':tell', "Sayonara!");
},'SearchIntent': function () {
var alexaString = this.event.request.intent.slots.anime.value;
//this is an attempt to get rid of 1 or 2 in favor of one and two
//get substring that has digits regex, and then replace with swap use number-to-words
alexaString = stringNumberify(alexaString)
lookUpAnime(alexaString, (result) => {
var retString = ""
if (result == null) {
retString = "Sorry! I couldn't find ".concat(alexaString, ". Please repeat that?");
this.emit(':tell',retString)
} else {
if(result instanceof Array) {
result = result[0];
}
retString = result.title.concat(" is a ", result.type, " anime with ", result.episodes, " episodes, scoring ratings of ", result.score, " out of 10.");
retString = retString.concat(" Here is a brief synopsis. ", cleanupSynopsis(result.synopsis));
var cardImage = {
smallImageUrl:result.image,
largeImageUrl:result.image
}
var cardContent = "Type: " + result.type + "\nEpisodes: " + result.episodes + "\nRating: " + result.score + "/10";
this.response.cardRenderer(result.title,cardContent,cardImage);
this.response.speak(retString);
this.emit(':responseReady');
// this.emit(':tellWithCard', retString, result.title, cardContent, cardImage);
}
});
},'DeleteIntent': function () {
var alexaString = this.event.request.intent.slots.anime.value;
lookUpAnime(alexaString, (result) => {
if (result == null) {
var retString = "Sorry! I couldn't find ".concat(alexaString, ". Please repeat that?");
this.emit(':tell',retString)
} else {
if(result instanceof Array) {
result = result[0];
}
mal.anime.deleteAnime(result.id)
.then(res => this.emit(':tell', "Removed anime from list"))
.catch(err => this.emit(':tell', "Failed to remove anime from list"))
}
});
},'AddIntent': function () {
var alexaString = this.event.request.intent.slots.anime.value;
lookUpAnime(alexaString, (result) => {
if (result == null) {
var retString = "Sorry! I couldn't find ".concat(alexaString, ". Please repeat that?");
this.emit(':tell',retString)
} else {
if(result instanceof Array) {
result = result[0];
}
mal.anime.addAnime(result.id, {
status: 6
}).then(res => this.emit(':tell', "Added " + result.title + " to your list!"))
.catch(err => this.emit(':tell', "Failed to add " + result.title + " to your list"));
}
});
},'UpdateIntent': function() {
var alexaAnime = this.event.request.intent.slots.anime.value;
if(this.event.request.intent.slots.episodes.hasOwnProperty('value')) {
var alexaVar = this.event.request.intent.slots.episodes.value;
lookUpAnime(alexaAnime, (result) => {
if (result == null) {
var retString = "Sorry! I couldn't find ".concat(alexaString, ". Please repeat that?");
this.emit(':tell',retString)
} else {
if(result instanceof Array) {
result = result[0];
}
mal.anime.updateAnime(result.id, {
episode: alexaVar
}).then(res => this.emit(':tell', "Updated " + result.title + " episode to " + alexaVar))
.catch(err => this.emit(':tell',"Failed to update episode for " + result.title));
}
});
}
else if (this.event.request.intent.slots.score.hasOwnProperty('value')) {
var alexaVar = this.event.request.intent.slots.score.value;
lookUpAnime(alexaAnime, (result) => {
if (result == null) {
var retString = "Sorry! I couldn't find ".concat(alexaString, ". Please repeat that?");
this.emit(':tell',retString)
} else {
if(result instanceof Array) {
result = result[0];
}
mal.anime.updateAnime(result.id, {
score: alexaVar
}).then(res => this.emit(':tell', "Updated " + result.title + " score to " + alexaVar))
.catch(err => this.emit(':tell', "Failed to update score for " + result.title));
}
});
}
/*else if(this.event.request.intent.slots.status.hasOwnProperty('value')) {
var alexaVar = this.event.request.intent.slots.status.value;
lookUpAnime(alexaAnime, (result) => {
if (result == null) {
var retString = "Sorry! I couldn't find ".concat(alexaString, ". Please repeat that?");
this.emit(':tell',retString)
} else {
if(result instanceof Array) {
result = result[0];
}
mal.anime.updateAnime(result.id, {
status: convertStatus(alexaVar);
}).then(res => this.emit(':tell', "Updated " + result.title + " status to " + alexaVar))
.catch(err=> this.emit(':tell', "Failed to update status for " + result.title));
}
});
} */ else {
this.emit(':tell', "Failed to update status.");
}
}
};
convertStatus = function(str) {
if (str.lowercase() === "plan to watch") {
return 6;
}
else if (str.lowercase() === "watching") {
return 1;
}
else if (str.lowercase() === "completed") {
return 2;
}
else if (str.lowercase() === "on hold") {
return 3;
}
else if (str.lowercase() === "dropped") {
return 4;
}
}
cleanupSynopsis = function(str) {
str = str.replace(/&.*;/g," ");
str = str.replace(/\\./g," ");
str = str.replace(/<.*>/g,"");
str = str.replace("[Written by MAL Rewrite]","");
return str;
}
lookUpAnime = function(inputAnime, callback){
mal.anime.searchAnime(inputAnime)
.then(res => callback(res))
.catch(err => callback(null));
}
//this finds the substring with digits, uns it through number-to-wrords
//and then returns the resulting string with the correct words instead of
//digits
//TODO missing case where
stringNumberify = function(stringWithDigits){
//arrays of index for digit substrings
var positions = [0, 0]
//while has digit runs here
while(hasDigit(stringWithDigits)){
//whether or not its in a digit
var inDigit = false
for( i = 0; i < stringWithDigits.length; i++){
//this should check for end of string digits so it doesnt loop again and mess up
if(stringWithDigits.charAt(i).match(/\d/) && i === stringWithDigits.length - 1){
//positions[0] = i
positions[1] = i + 1
//goes to the converter
break;
}
//checks if it is a digit
if(stringWithDigits.charAt(i).match(/\d/)){ //indexof instead
//checks if its in a digit
if(!inDigit){
//if not it sets the first entry
positions[0] = i;
//sets it to be in a digit
inDigit = true;
}
}else{ //not a digit,so if it was in a digit add to array
//it has been in a digit so its the end of the number
if(inDigit){
positions[1] = i;
inDigit = false;
//goes to the converter
break;
}
}
}
if(positions[1] != 0){
//have index positions, relpace the lines with numberes
stringToBeReplaced = stringWithDigits.substring(positions[0], positions[1])
wordDigits = converter.toWords(stringToBeReplaced) //this is the digit in words
//var retstring
stringWithDigits = stringWithDigits.replace(stringToBeReplaced, wordDigits) //should replace
//positions[0] = positions[1] = 0; // <<<---THIS BREAKS IT
}
}
return stringWithDigits;
}}
//checks if there are digits still in the string
hasDigit = function(string){
if(string.search(/\d/g) != -1){ //if there is not no match found
//console.log("true")
return true;
}
//console.log("f")
return false;
}
/*
mal.anime.addAnime(1535, {
score: 5
}).then(res => console.log(res))
.catch(err => console.error(err))
mal.anime.updateAnime(1535,{
score: 6,
episode: 2,
status: 2
}).then(res => console.log(res))
.catch(err => console.error(err))
mal.anime.deleteAnime(1535,{
})
*/