-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
142 lines (119 loc) · 3.61 KB
/
app.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
'use strict';
// Load environment variables from the .env file
const fs = require ('fs');
const os = require('os');
var express = require('express');
var app = express();
app.use(express.static(__dirname + '/public'));
app.use(express.static(__dirname + '/uploads'));
app.set("view engine","ejs");
var multer = require('multer')
const path = require('path');
var dataObj;
var client_id = process.env.SPOTIFY_CLIENT_ID;
var client_secret = process.env.SPOTIFY_CLIENT_SECRET;
/**********************************
*
* SPOTIFY AUTHORIZATION
*
********************************/
var SpotifyWebApi = require('spotify-web-api-node');
var spotifyApi = new SpotifyWebApi({
clientId: client_id,
clientSecret: client_secret,
});
spotifyApi.clientCredentialsGrant().then(
function(data) {
// Save the access token so that it's used in future calls
spotifyApi.setAccessToken(data.body['access_token']);
},
function(err) {
console.log('Something went wrong when retrieving an access token', err);
}
);
/**********************************
*
* GOOGLE VISION
*
********************************/
const vision = require('@google-cloud/vision');
const client = new vision.ImageAnnotatorClient();
async function getEntities(fileName) {
// Performs label detection on the local file
const [result] = await client.labelDetection(os.tmpdir()+ '/' + fileName);
const labels = result.labelAnnotations;
// For debugging
//labels.forEach(label => console.log(label.description));
return labels[0].description;
}
/*********************************
*
* Uploading files
*
*********************************/
const multer_storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, os.tmpdir())
},
filename: function (req, file, cb) {
cb(null, file.originalname)
}
})
const upload = multer({
fileFilter: function (req, file, cb) {
var filetypes = /jpeg|jpg|JPEG|JPG|png|PNG/;
var mimetype = filetypes.test(file.mimetype);
var extname = filetypes.test(path.extname(file.originalname).toLowerCase());
if (mimetype && extname) {
return cb(null, true);
}
cb("Error: File upload only supports the following filetypes - " + filetypes);
},
storage: multer_storage
});
/******************************
*
* ROUTES
*
******************************/
app.get('/', function(req, res) {
res.status(200);
return res.render('index');
});
app.post('/uploads', upload.single('file'), function (req, res, next) {
res.status(200);
var obj = {
word: null,
image: null,
id: null
};
// Get entity using Gcloud vision API
getEntities(req.file.filename)
.then(function(entity){
// Search playlists using image entity
obj.word = entity;
spotifyApi.searchPlaylists(entity, { limit : 5, offset : 1 })
.then(function(data) {
obj.image = data.body.playlists.items[0].images[0].url;
obj.id = data.body.playlists.items[0].id;
dataObj = obj;
})
.then(function(){
console.log('this should only get called when we have obj data');
res.redirect('/player');
})
.catch(function(error){
console.log('Something went wrong. Error:');
console.log(error);
});
})
.catch(function(error){
console.log('error getting entities');
console.log(error);
});
});
app.get('/player', function(req, res) {
res.status(200);
res.render('player', {word: dataObj.word, uri: dataObj.id, image: dataObj.image});
});
app.listen(process.env.PORT || 8080);