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:24:19 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
8955f5dc5f04134c1638bc2b61bb3a972bd83483
8955f5dc
1 parent
0c8cc58f
Add 'api.js', Modify 'index.js'
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
207 additions
and
3 deletions
routes/api.js
routes/index.js
routes/api.js
0 → 100644
View file @
8955f5d
const
SpotifyWebApi
=
require
(
'spotify-web-api-node'
);
// spotify api
//const { search } = require('.');
var
spotifyApi
=
new
SpotifyWebApi
({
clientId
:
"faf7865d62b5488da2f6bca05e63a075"
,
//your id
clientSecret
:
"b888f07e8986499aa70950b0235d81eb"
,
//your secret
redirectUri
:
'http://localhost:3000/callback'
//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
;
//followers.total
//images[0]
//name
//popularity
//id
// Get albums by a certain artist
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
;
// function search(){
// SearchUrl = `https://open.spotify.com/artist/4kyWODlwZxF4tiAe4LblhX`
// request(SearchUrl, (error, response, body) => {
// var data = body;
// console.log(body)
// // for (var j = 0; j < 10; j++) {
// // var team_id = data["info"]["participants"][j]["teamId"]
// // if (p_id == summoner_puuid) {
// // my_info["kills"] += match["info"]["participants"][j]["kills"]
// // win = match["info"]["participants"][j]["win"];
// // }
// // }
// })
// }
// module.exports.search = search;
// var item = data.body.artists.items.find((item,idx)=>{
// item.popularity == "0";
// });
\ No newline at end of file
routes/index.js
View file @
8955f5d
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