-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreference.js
582 lines (435 loc) · 19.7 KB
/
reference.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
'use strict';
let fs = require('fs'),
colors = require('colors'),
express = require('express'),
bodyParser = require('body-parser'),
https = require('https'),
http = require('http'),
Twitter = require('twitter'),
request = require('request'),
querystring = require('querystring'),
cookieParser = require('cookie-parser'),
Client = require('node-rest-client').Client,
app = express(),
client = new Client();
// swap dev/production data
var instagramAccessCode = 'FOR-TEMPORARY-USE-ONLY';
var spotifyAccessToken = 'FOR-TEMPORARY-USE-ONLY';
let instagram_client_id = 'FOR-TEMPORARY-USE-ONLY';
let instagram_client_secret = 'FOR-TEMPORARY-USE-ONLY';
let instagram_redirect_uri = 'FOR-TEMPORARY-USE-ONLY';
let instagramApiURL = 'FOR-TEMPORARY-USE-ONLY';
let spotify_redirect_uri = 'FOR-TEMPORARY-USE-ONLY';
var ignitionSwitch = true;
if (ignitionSwitch) {
instagram_client_id = "b23670e220f14f1c89c11f627c9f9953";
instagram_client_secret = "dd78c7ffbadd4a10a49f24675356c4d2";
instagram_redirect_uri = 'https://losethequit.herokuapp.com/views/werkspayce.html';
instagramApiURL = 'http://www.instagram.com/oauth/authorize?client_id=b23670e220f14f1c89c11f627c9f9953&redirect_uri=https://losethequit.herokuapp.com/views/werkspayce.html&response_type=code&scope=basic+public_content+follower_list+comments+relationships+likes';
spotify_redirect_uri = 'https://losethequit.herokuapp.com/views/werkspayce.html/spotify-callback';
} else {
instagram_client_id = 'd0f6230a40954cb2823768aa53910a5e';
instagram_client_secret = 'bfb29d9f5ee94a46a675f771e9013477';
instagram_redirect_uri = 'http://localhost:5000/views/werkspayce.html';
instagramApiURL = 'http://www.instagram.com/oauth/authorize?client_id=d0f6230a40954cb2823768aa53910a5e&redirect_uri=http://localhost:5000/views/werkspayce.html&response_type=code&scope=basic+public_content+follower_list+comments+relationships+likes';
spotify_redirect_uri = 'http://localhost:5000/views/werkspayce.html/spotify-callback';
}
var twitterClient = new Twitter({
consumer_key: 'qiQPf27ifq8tSBHCAqtu6iHLU',
consumer_secret: 'hMNZdyYyVT4SXTGbd60qE5WVe8wfO9no0mlvJNseRreJDumjQP',
access_token_key: '4882645629-rFYbk7Bz2tw4Z9QEAce62S8SY3MJRNS6HTy3tOk',
access_token_secret: 'Q4NhcvlrW81xQREPH4yQvOR9iwlSQqmC4AVOK9QQOiq7h'
});
var spotify_client_id = '099060b613284cc0af0210f5199dcb0c'; // Your client id
var spotify_client_secret = '42c98e7bfcf6426dbf25888204456dce'; // Your secret
var generateRandomString = function (length) {
/************************************************************
* Generates a random string containing numbers and letters
* @param {number} length The length of the string
* @return {string} The generated string
***********************************************************/
var text = '';
var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (var i = 0; i < length; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
};
var stateKey = 'spotify_auth_state';
var scopes = 'user-read-private user-read-email'
/**
* ROUTING
* ROUTING
* ROUTING
* ROUTING
*/
app.set('port', (process.env.PORT || 5000));
app.use(express.static('/static'))
.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.static('static'))
.use(cookieParser());
app.use('/bower_components', express.static(__dirname + '/bower_components'));
app.use('/node_modules', express.static(__dirname + '/node_modules'));
app.get('/', function (req, res) {
console.log('\n');
console.log('******* INCOMING GET REQUEST - Load Template *******'.black.bgWhite);
console.log('\n');
var html = fs.readFileSync('static/views/shell.html');
res.end(html);
});
app.get('/on-ramp', function (req, res) {
console.log('\n');
console.log('******* INCOMING GET REQUEST - Load Template *******'.black.bgWhite);
console.log('\n');
// var html = fs.readFileSync('static/views/on-ramp.html');
var onRamp = fs.readFileSync('http://go.nthrive.com/rs/099-EMG-811/images/nThrive-banner-001.html');
res.end(onRamp);
});
app.listen(app.get('port'), function () {
console.log('\n');
console.log('********************************************'.black.bgWhite);
console.log("The frontend server is running on port 5000!".black.bgWhite);
console.log('********************************************'.black.bgWhite);
console.log('\n');
});
app.get('/spotify', function (req, res) {
console.log('START *** SPOTIFY - INCOMING INPUT GET REQUEST - SPOTIFY *** START'.black.bgCyan);
var spotifyApiUrl = 'https://api.spotify.com/v1/artists/1vCWHaC5f2uS3yhpwWbIA6/albums?album_type=SINGLE&offset=20&limit=20';
client.get(spotifyApiUrl, function (data, response) {
// console.log(data.items);
res.send(data.items);
res.end();
});
console.log('END ***** SPOTIFY - INCOMING INPUT GET REQUEST - SPOTIFY ***** END'.black.bgCyan);
});
app.get('/views/werkspayce.html/spotify-login', function (req, res) {
console.log('START *** spotify-login *** START'.black.bgCyan);
var state = generateRandomString(16);
res.cookie(stateKey, state);
var scope = 'user-read-private user-read-email';
console.log('state: ' + state)
console.log('stateKey: ' + stateKey)
res.redirect('https://accounts.spotify.com/authorize?' +
querystring.stringify({
response_type: 'code',
client_id: spotify_client_id,
scope: scope,
redirect_uri: spotify_redirect_uri,
state: state
}));
console.log('END ***** spotify-login ***** END'.black.bgCyan);
console.log('\n');
});
app.get('/views/werkspayce.html/spotify-callback', function (req, res) {
console.log('START ***** spotify-callback *** spotify-callback ***** START'.black.bgCyan);
// your application requests refresh and access tokens
// after checking the state parameter
var code = req.query.code || null;
var state = req.query.state || null;
var storedState = req.cookies ? req.cookies[stateKey] : null;
if (state === null || state !== storedState) {
res.redirect('/#' +
querystring.stringify({
error: 'state_mismatch'
}));
} else {
res.clearCookie(stateKey);
var authOptions = {
url: 'https://accounts.spotify.com/api/token',
form: {
code: code,
redirect_uri: spotify_redirect_uri,
grant_type: 'authorization_code'
},
headers: {
'Authorization': 'Basic ' + (new Buffer(spotify_client_id + ':' + spotify_client_secret).toString('base64'))
},
json: true
};
request.post(authOptions, function (error, response, body) {
if (!error && response.statusCode === 200) {
var access_token = body.access_token;
var refresh_token = body.refresh_token;
spotifyAccessToken = access_token;
console.log('\n');
console.log('SPOTIFY ACCESS TOKEN:'.white.bgCyan);
// console.log('\n');
console.log(spotifyAccessToken);
console.log('SPOTIFY ACCESS TOKEN:'.white.bgCyan);
console.log('\n');
// var options = {
// // url: 'https://api.spotify.com/v1/me',
// url: 'https://api.spotify.com/v1/search?q=tania%20bowra&type=artist',
// headers: {
// 'Authorization': 'Bearer ' + access_token
// },
// json: true
// };
//
// // use the access token to access the Spotify Web API
// request.get(options, function (error, response, body) {
// console.log(body);
// });
//
// // we can also pass the token to the browser to make requests from there
// // res.redirect('/views/werkspayce.html?' +
res.redirect('/instagram-login?' +
querystring.stringify({
access_token: access_token,
refresh_token: refresh_token
}));
} else {
res.redirect('/views/werkspayce.html?' +
querystring.stringify({
error: 'invalid_token'
}));
}
console.log('END ******* spotify-callback *** spotify-callback ******* END'.black.bgCyan);
});
}
});
app.post('/spotify-input-query', function (req, res) {
console.log('START *** INSIDE spotify-input-query INSIDE ***'.black.bgCyan);
console.log('\n');
console.log('START *** INSIDE spotify-input-query -TEXT - INSIDE ***'.black.bgCyan);
// req.body seems to only work with POST requests
console.log(req.body);
console.log('START *** INSIDE spotify-input-query -TEXT - INSIDE ***'.black.bgCyan);
var options = {
url: 'https://api.spotify.com/v1/search?q=' + req.body.q + '&type=artist',
headers: {
'Authorization': 'Bearer ' + spotifyAccessToken
},
json: true
};
// use the access token to access the Spotify Web API
request.get(options, function (error, response, body) {
if (response.statusCode === 200) {
res.send(body.artists.items);
} else {
console.log(response.body)
}
});
console.log('\n');
console.log('END *** INSIDE spotify-input-query INSIDE ***'.black.bgCyan);
});
app.post('/userTimeLineQuery', function (req, res) {
console.log('\n');
console.log('START *** INCOMING INPUT GET REQUEST - search-tweets-query *** START'.white.bgBlue);
console.log('INPUT QUERY: ' + '\n' + JSON.stringify(req.body));
console.log('INCOMING INPUT GET REQUEST - search-tweets-query'.white.bgBlue);
console.log('\n');
twitterClient.get('statuses/user_timeline', req.body, function (error, tweets, response) {
if (!error) {
res.json(tweets);
console.log('***** tweets *** SENT *** tweets *****'.white.bgBlue);
} else {
res.json(error);
console.log(error);
}
console.log('\n');
console.log('** TWITTER - END - userTimeLineQuery - END - TWITTER **'.white.bgBlue);
});
});
app.post('/searchTweetsQuery', function (req, res) {
console.log('TWITTER - INPUTQUERY - START - TWITTER'.white.bgBlue);
console.log('\n');
console.log('INCOMING INPUT GET REQUEST - search-tweets-query'.white.bgBlue);
console.log(req.body);
console.log(req.body);
console.log(req.body);
console.log(req.body);
console.log('INCOMING INPUT GET REQUEST - search-tweets-query'.white.bgBlue);
console.log('\n');
twitterClient.get('search/tweets', req.body, function (error, tweets, response) {
if (!error) {
res.json(tweets);
console.log('***** tweets *** SENT *** tweets *****'.white.bgBlue);
} else {
res.json(error);
console.log(error);
}
console.log('\n');
console.log('** TWITTER - END - searchTweetsQuery - END - TWITTER **'.white.bgBlue);
});
});
app.get('/instagram-login', function (req, res) {
res.redirect(instagramApiURL);
res.end();
});
app.post('/ig', function (req, res, next) {
console.log('\n');
console.log('START ***** - IG INCOMING POST REQUEST - IG ***** START'.black.bgGreen);
if (req.body.token.indexOf("http") != -1) {
console.log('INSTAGRAM ACCESS_CODE NOT AVAILABLE'.white.bgRed);
// if the access code IS NOT available
// STOP the process of making a call to the
// instagram api
} else {
// if the access code IS available
// START the process of making a call to the
// instagram api
let ACCESS_CODE = req.body.token;
let post_data = {
'client_id': instagram_client_id,
'client_secret': instagram_client_secret,
'grant_type': 'authorization_code',
'redirect_uri': instagram_redirect_uri,
'code': ACCESS_CODE
};
var headers = {
'User-Agent': 'Super Agent/0.0.1',
'Content-Type': 'application/x-www-form-urlencoded'
};
var post_options = {
url: 'https://api.instagram.com/oauth/access_token',
method: 'POST',
headers: headers,
form: post_data
};
request(post_options, function (error, response, body) {
var parsedBody = JSON.parse(body);
// console.log('*******************************************************'.black.bgRed);
// console.log(parsedBody);
// console.log('*******************************************************'.black.bgRed);
if (response.statusCode != 200) {
console.log('ACCESS CODE NOT PRESENT');
} else {
instagramAccessCode = parsedBody.access_token;
console.log('\n');
console.log('instagramAccessCode:'.white.bgGreen);
console.log(instagramAccessCode);
console.log('instagramAccessCode:'.white.bgGreen);
console.log('\n');
var from_the_docs = {
url: 'https://api.instagram.com/v1/tags/nofilter/media/recent?access_token=' + instagramAccessCode,
method: 'GET'
};
var media_search = {
url: 'https://api.instagram.com/v1/media/search?lat=48.858844&lng=2.294351&access_token=' + instagramAccessCode,
method: 'GET'
};
var popular_media_search = {
url: 'https://api.instagram.com/v1/media/popular?access_token=' + instagramAccessCode,
method: 'GET'
};
var from_SO_search = {
url: 'https://api.instagram.com/v1/tags/res/media/recent?client_id=' + instagram_client_id + '&callback=' +
instagram_redirect_uri + '&access_token=' + instagramAccessCode,
method: 'GET'
}
var from_SO_search = {
url: 'https://api.instagram.com/v1/tags/res/media/recent?client_id=' + instagram_client_id + '&callback=' +
instagram_redirect_uri + '&access_token=' + instagramAccessCode,
method: 'GET'
}
var user_search_by_name = {
url: 'https://api.instagram.com/v1/users/search?q=cthagod&access_token=' + instagramAccessCode,
method: 'GET'
}
// THESE WORK - thisIsCamelCaseAsAnExample this
var self_search = {
url: 'https://api.instagram.com/v1/users/self/media/recent/?access_token=' + instagramAccessCode + '&count=300',
method: 'GET'
};
var popular_tag_search = {
url: 'https://api.instagram.com/v1/tags/search?q=red&access_token=' + instagramAccessCode,
method: 'GET'
};
var search_popular_by_tag_name = {
url: 'https://api.instagram.com/v1/tags/nodejs?access_token=' + instagramAccessCode,
method: 'GET'
};
var popular_tag_search_tag_name_recent = {
url: 'https://api.instagram.com/v1/tags/dev/media/recent?access_token=' + instagramAccessCode + '&count=200',
method: 'GET'
};
//&min_id=678453535718114828_919796408
request(self_search, function (error, response, body) {
if (error && response.statusCode != 200) {
console.error(error);
// if (error)
// throw (error);
} else {
var JSONObjectArray = JSON.parse(body);
// turns off back end logging of user JSONObjectArray
// console.log(JSONObjectArray);
res.send(JSONObjectArray);
console.log('***** JSONObjectArray *** SENT *** JSONObjectArray *****'.black.bgGreen);
console.log('\n');
console.log('END ******* - IG INCOMING POST REQUEST - IG ******* END'.black.bgGreen);
}
});
}
});
}
});
app.post('/instaInputQuery', function (req, res, next) {
console.log('\n');
console.log('START - *** - IG - InputQuery - IG - *** - START'.black.bgGreen);
console.log(req.body);
let inputQueryFromHTML = req.body.query;
var from_the_docs = {
url: 'https://api.instagram.com/v1/tags/nofilter/media/recent?access_token=' + instagramAccessCode,
method: 'GET'
};
var media_search = {
url: 'https://api.instagram.com/v1/media/search?lat=48.858844&lng=2.294351&access_token=' + instagramAccessCode,
method: 'GET'
};
var popular_media_search = {
url: 'https://api.instagram.com/v1/media/popular?access_token=' + instagramAccessCode,
method: 'GET'
};
var from_SO_search = {
url: 'https://api.instagram.com/v1/tags/res/media/recent?client_id=' + instagram_client_id + '&callback=' +
instagram_redirect_uri + '&access_token=' + instagramAccessCode,
method: 'GET'
}
var from_SO_search = {
url: 'https://api.instagram.com/v1/tags/res/media/recent?client_id=' + instagram_client_id + '&callback=' +
instagram_redirect_uri + '&access_token=' + instagramAccessCode,
method: 'GET'
}
var user_search_by_name = {
url: 'https://api.instagram.com/v1/users/search?q=cthagod&access_token=' + instagramAccessCode,
method: 'GET'
}
// THESE WORK
var self_search = {
url: 'https://api.instagram.com/v1/users/self/media/recent/?access_token=' + instagramAccessCode + '&count=300',
method: 'GET'
};
var popular_tag_search = {
url: 'https://api.instagram.com/v1/tags/search?q=red&access_token=' + instagramAccessCode,
method: 'GET'
};
var search_popular_by_tag_name = {
url: 'https://api.instagram.com/v1/tags/' + req.body.query + '?access_token=' + instagramAccessCode,
method: 'GET'
};
var popular_tag_search_tag_name_recent = {
url: 'https://api.instagram.com/v1/tags/' + req.body.query + '/media/recent?access_token=' + instagramAccessCode + '&count=200',
method: 'GET'
};
request(popular_tag_search_tag_name_recent, function (error, response, body) {
if (error || response.statusCode != 200) {
error = error || response;
console.error(error);
// if (error)
// throw (error);
} else {
var JSONobjArray = JSON.parse(body);
// turns off back end logging of user JSONObjectArray
// console.log(JSONObjectArray);
res.send(JSONobjArray);
console.log('***** JSONObjectArray *** SENT *** JSONObjectArray *****'.black.bgGreen);
console.log('\n');
console.log('END - *** - IG - InputQuery - IG - *** - END'.black.bgGreen);
}
});
});