Toggle navigation
Toggle navigation
This project
Loading...
Sign in
김유민
/
digging-service
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
blue
2021-12-05 22:44:56 +0900
Browse Files
Options
Browse Files
Download
Plain Diff
Commit
f3b16c78169663b437daf2a676c7e980e5dd062e
f3b16c78
2 parents
b91b0c7e
c38e6701
Merge branch 'feature_routes'
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
181 additions
and
3 deletions
routes/api.js
routes/index.js
routes/api.js
0 → 100644
View file @
f3b16c7
const
SpotifyWebApi
=
require
(
'spotify-web-api-node'
);
// spotify api
//const { search } = require('.');
var
spotifyApi
=
new
SpotifyWebApi
({
clientId
:
"your id"
,
clientSecret
:
"your secret"
,
redirectUri
:
'redirectUri'
});
const
scopes
=
[
'ugc-image-upload'
,
'user-read-playback-state'
,
'user-modify-playback-state'
,
'user-read-currently-playing'
,
'streaming'
,
'app-remote-control'
,
'user-read-email'
,
'user-read-private'
,
'playlist-read-collaborative'
,
'playlist-modify-public'
,
'playlist-read-private'
,
'playlist-modify-private'
,
'user-library-modify'
,
'user-library-read'
,
'user-top-read'
,
'user-read-playback-position'
,
'user-read-recently-played'
,
'user-follow-read'
,
'user-follow-modify'
];
var
access_token
;
function
redirect
(
res
){
res
.
redirect
(
spotifyApi
.
createAuthorizeURL
(
scopes
));
}
module
.
exports
.
redirect
=
redirect
;
function
callback
(
req
,
res
)
{
const
error
=
req
.
query
.
error
;
const
code
=
req
.
query
.
code
;
const
state
=
req
.
query
.
state
;
if
(
error
)
{
console
.
error
(
'Callback Error:'
,
error
);
res
.
send
(
`Callback Error:
${
error
}
`
);
return
;
}
spotifyApi
.
authorizationCodeGrant
(
code
)
.
then
(
data
=>
{
access_token
=
data
.
body
[
'access_token'
];
const
refresh_token
=
data
.
body
[
'refresh_token'
];
const
expires_in
=
data
.
body
[
'expires_in'
];
spotifyApi
.
setAccessToken
(
access_token
);
spotifyApi
.
setRefreshToken
(
refresh_token
);
console
.
log
(
'access_token:'
,
access_token
);
console
.
log
(
'refresh_token:'
,
refresh_token
);
console
.
log
(
`Sucessfully retreived access token. Expires in
${
expires_in
}
s.`
);
setInterval
(
async
()
=>
{
const
data
=
await
spotifyApi
.
refreshAccessToken
();
const
access_token
=
data
.
body
[
'access_token'
];
console
.
log
(
'The access token has been refreshed!'
);
console
.
log
(
'access_token:'
,
access_token
);
spotifyApi
.
setAccessToken
(
access_token
);
},
expires_in
/
2
*
1000
);
})
.
catch
(
error
=>
{
console
.
error
(
'Error getting Tokens:'
,
error
);
res
.
send
(
`Error getting Tokens:
${
error
}
`
);
});
}
module
.
exports
.
callback
=
callback
;
// Search artists whose name, album or artist contains 'hip hop'
function
searchArtists
(
genre
,
callback
){
spotifyApi
.
searchArtists
(
genre
)
.
then
(
function
(
data
)
{
console
.
log
(
'Search artists success'
,
data
.
body
.
artists
.
items
);
callback
(
data
.
body
.
artists
.
items
[
19
].
id
)
},
function
(
err
)
{
console
.
error
(
err
);
});
}
module
.
exports
.
searchArtists
=
searchArtists
;
// Get an artist
function
getArtist
(
artistid
,
callback
){
spotifyApi
.
getArtist
(
artistid
)
.
then
(
function
(
data
)
{
console
.
log
(
'getArtist success'
,
data
.
body
);
callback
(
data
.
body
);
},
function
(
err
)
{
console
.
error
(
err
);
});
}
module
.
exports
.
getArtist
=
getArtist
;
function
getArtistAlbums
(
artistid
,
callback
){
spotifyApi
.
getArtistAlbums
(
artistid
)
.
then
(
function
(
data
)
{
console
.
log
(
'getArtistAlbums success'
,
data
.
body
.
items
[
0
].
id
);
callback
(
data
.
body
.
items
[
0
].
id
);
},
function
(
err
)
{
console
.
error
(
err
);
});
}
module
.
exports
.
getArtistAlbums
=
getArtistAlbums
;
routes/index.js
View file @
f3b16c7
const
express
=
require
(
"express"
);
const
{
nextTick
}
=
require
(
"process"
);
const
router
=
express
.
Router
();
var
api
=
require
(
'./api'
);
router
.
get
(
'/'
,
(
req
,
res
)
=>
res
.
render
(
'index'
));
router
.
get
(
"/login"
,
(
req
,
res
)
=>
res
.
render
(
"login"
,
{
page
:
"login"
}));
router
.
get
(
"/signup"
,
(
req
,
res
)
=>
res
.
render
(
"signup"
,
{
page
:
"signup"
}));
router
.
get
(
'/'
,
(
req
,
res
)
=>
{
api
.
redirect
(
res
);
});
router
.
get
(
'/callback'
,
(
req
,
res
)
=>
{
api
.
callback
(
req
,
res
);
res
.
render
(
'index'
);
});
router
.
get
(
"/select"
,
(
req
,
res
)
=>
{
res
.
render
(
"select"
,
{
page
:
"select"
})
});
router
.
get
(
"/rock"
,
(
req
,
res
)
=>
{
var
genre
=
'rock'
;
api
.
searchArtists
(
genre
,
function
(
artistid
){
api
.
getArtist
(
artistid
,
function
(
artistdata
){
res
.
render
(
"rock"
,
{
artistdata
:
artistdata
});
});
});
});
router
.
get
(
"/electronic"
,
(
req
,
res
)
=>
{
var
genre
=
'electronic'
;
api
.
searchArtists
(
genre
,
function
(
artistid
){
api
.
getArtist
(
artistid
,
function
(
artistdata
){
res
.
render
(
"electronic"
,
{
artistdata
:
artistdata
});
});
});
});
router
.
get
(
"/hiphop"
,
(
req
,
res
)
=>
{
var
genre
=
'hiphop'
;
api
.
searchArtists
(
genre
,
function
(
artistid
){
api
.
getArtist
(
artistid
,
function
(
artistdata
){
res
.
render
(
"hiphop"
,
{
artistdata
:
artistdata
});
});
});
});
router
.
get
(
"/acoustic"
,
(
req
,
res
)
=>
{
var
genre
=
'acoustic'
;
api
.
searchArtists
(
genre
,
function
(
artistid
){
api
.
getArtist
(
artistid
,
function
(
artistdata
){
res
.
render
(
"acoustic"
,
{
artistdata
:
artistdata
});
});
});
});
router
.
get
(
"/jazz"
,
(
req
,
res
)
=>
{
var
genre
=
'jazz'
;
api
.
searchArtists
(
genre
,
function
(
artistid
){
api
.
getArtist
(
artistid
,
function
(
artistdata
){
res
.
render
(
"jazz"
,
{
artistdata
:
artistdata
});
});
});
});
router
.
get
(
"/pop"
,
(
req
,
res
)
=>
{
var
genre
=
'pop'
;
api
.
searchArtists
(
genre
,
function
(
artistid
){
api
.
getArtist
(
artistid
,
function
(
artistdata
){
res
.
render
(
"pop"
,
{
artistdata
:
artistdata
});
});
});
});
module
.
exports
=
router
;
...
...
Please
register
or
login
to post a comment