-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
235 lines (204 loc) · 6.41 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
var TWILIO_SID, TWILIO_APP_ID, MONGO_DATABASE, YOUTUBE_API;
var google = require('googleapis');
var customsearch = google.customsearch('v1');
var fs = require('fs');
var obj = JSON.parse(fs.readFileSync('./creds.json', 'utf8'));
var path = require('path');
TWILIO_SID = obj.TWILIO_SID;
TWILIO_APP_ID = obj.TWILIO_APP_ID;
MONGO_DATABASE = obj.MONGO_DATABASE;
YOUTUBE_API = obj.YOUTUBE_API;
var express = require('express');
var client = require('twilio')(TWILIO_SID, TWILIO_APP_ID);
var MongoClient = require("mongodb").MongoClient;
var request = require('request');
var app = express();
var db;
//-r -D
var DEPLOY_TEST = "7";
var PORT = 3000;
var API_KEY;
var NUMBER = "+17323336592";
app.use(express.static('public'));
app.use('/static', express.static(__dirname + '/public'));
console.log("Running deploy test " + DEPLOY_TEST);
//Open the connection to the database
MongoClient.connect(MONGO_DATABASE, function (err, database) {
if (err) {
console.log("There was a problem trying to connect to the database " +
"the application has bene terminated");
throw err;
} else {
db = database;
console.log("successfully connected to the database");
//Simulate should crash the server, does not specify res value
//console.log("simulating a test text");
//simulate();
//console.log(db.collection('users').findOne({"phoneNumber": "+18482294098"}));
//getSong("Im like hey whats up", function(){});
}
});
app.use(express.static('public'));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
//Allows client to fetch for the request
app.get('/fetch', function(req, res){
//fetches all songs for the given url
var number = req.query.phoneNum;
var s;
db.collection('users').find({"phoneNumber": "+1" + number}).forEach(function(u) {
res.send(u.songs);
return;
});
return;
});
//Adds song to mongodb server
//Takes in phone number and song / search term
app.get('/addSong', function(req, res){
var number = req.query.From;
var text = req.query.Body;
var songName = parseBody(text.toLowerCase());
var lyrics = parseLyric(text.toLowerCase());
//Invalid syntax
if(songName == null){
if(lyrics == null){
sendMessage(number, "The syntax of your request was invalid");
res.send("Reponse Finished: With Error by user");
return;
}
getSong(lyrics, function(reply){
if(reply == -99){
sendMessage(number, "We weren't able to identify your song ;_;");
return;
}
if(typeof reply !== 'undefined' && reply){
sendMessage(number, "The song should be " + reply.replace(" - A-Z Lyrics", ""));
var s = reply.replace(" - A-Z Lyrics", "").split("-")[1].replace(" ", "");
if(s == null){
console.log("null caught");
return;
}
getSongUrl(s,
function(response){
var videoID = JSON.parse(response).items[0].id.videoId;
addToDatabase(number, videoID);
return;
});
}
});
}
getSongUrl(songName, function(response){
if(JSON.parse(response).items.length > 0){
var videoID = JSON.parse(response).items[0].id.videoId;
sendMessage(number, "The song has been added to your playlist");
addToDatabase(number, videoID);
res.send("Reponse Finished: With Success");
}
return;
});
});
function getSong(lyric, callback){
customsearch.cse.list({ cx: '013924013317681380050:mhj2yarvy-q', q: lyric, auth: "AIzaSyAhsmn1-SsD12eowR4dYzY4V4TPsJsP_TI" }, function(err, resp) {
if (err) {
console.log('An error occured', err);
return;
}
// Got the response from custom search
console.log('Result: ' + resp.searchInformation.formattedTotalResults);
console.log(resp.items);
if(typeof resp.items !== 'undefined' && resp.items){
if (resp.items && resp.items.length > 0) {
callback(resp.items[0].title);
}
}
else{
callback(-99);
}
});
}
function parseLyric(body){
if(body.indexOf("lyric ") == -1){
return null;
}
return body.replace("lyric ", "");
}
//Send a message through twilio
function sendMessage(number, message){
client.sendMessage({
to:number, // Any number Twilio can deliver to
from: NUMBER, // A number you bought from Twilio and can use for outbound communication
body: message // body of the SMS message
}, function(err, responseData) { //this function is executed when a response is received from Twilio
if (err) {
console.log(err);
console.log(responseData.from);
console.log(responseData.body);
}
});
}
function parseBody(body){
if(body.indexOf("add ") == -1){
return null;
}
return body.replace("add ", "");
}
function getSongUrl(searchTerm, callback){
//Search for song using youtube api
request("https://www.googleapis.com/youtube/v3/search?part=id&q=" + searchTerm + "&type=video&key=" + YOUTUBE_API, function (error, response, body) {
if (!error && response.statusCode == 200) {
callback(body);
}
})
}
function simulate(){
var number = "+18482294098";
var text = "add darude sandstorm";
var songName = parseBody(text);
console.log(songName);
//Invalid syntax
if(songName == null){
sendMessage(number, "The syntax of your request was invalid");
return;
}
console.log("adding " + songName);
getSongUrl(songName, function(response){
if(JSON.parse(response).items.length > 0){
var videoID = JSON.parse(response).items[0].id.videoId;
addToDatabase(number, videoID);
res.send("Reponse Finished: With Success");
}
});
}
function addToDatabase(number, URL){
//Detect if song is already in the playlist
//if(db.collection('users').find({"phonenumber": {$eq: number}}, {$elemMatch: {"songs": URL}}).first() !== null){
//return;
//}
console.log(URL);
if(URL === null || URL === "null"){
console.log("Null caught");
return;
}
if(URL === "A7JVDxN-Eck"){
console.log("no null");
return "";
}
if(db.collection('users').find({ "phoneNumber": { $eq : number } }).count() < 0){
db.collection('users').insertOne({
"phoneNumber": number,
"songs": [URL]
});
}
//Add the url with teh number to the song database
db.collection('users').update({"phoneNumber": { $eq: number }},
{ $push: { "songs": URL } },
{
upsert: true,
multi: false,
});
}
app.listen(PORT);
console.log("The server is now listening on port " + PORT);