generated from melaasar/template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
137 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const mongoose = require("mongoose"); | ||
|
||
// WE DONT USE THIS YET (GOTTA FIGURE OUT DB CONFIG) | ||
const songSchema = new mongoose.Schema({ | ||
title: { type: String, required: true }, | ||
artist: { type: String, required: true }, | ||
album: { type: String }, | ||
duration: { type: Number }, | ||
preview_url: { type: String }, | ||
}); | ||
|
||
module.exports = mongoose.model("Song", songSchema); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
const express = require("express"); | ||
const router = express.Router(); | ||
const axios = require("axios"); | ||
const { getAccessToken } = require("../utility/tokenManager"); | ||
|
||
// Get spotify access token with client credentials flow | ||
// see https://developer.spotify.com/documentation/web-api/tutorials/client-credentials-flow | ||
router.get('/token', async (req, res) => { | ||
try { | ||
const accessToken = await getAccessToken() | ||
res.json({ | ||
success: true, | ||
access_token: accessToken | ||
}) | ||
} catch (error) { | ||
res.status(500).json({ | ||
success: false, | ||
message: "Failed to retrieve access token", | ||
details: error.message | ||
}) | ||
} | ||
}) | ||
|
||
// Get track from track ID | ||
router.get("/track/:id", async (req, res) => { | ||
const trackId = req.params.id; | ||
|
||
try { | ||
const accessToken = await getAccessToken(); | ||
const response = await axios.get( | ||
`https://api.spotify.com/v1/tracks/${trackId}`, | ||
{ | ||
headers: { | ||
Authorization: `Bearer ${accessToken}`, | ||
}, | ||
} | ||
); | ||
res.json(response.data); | ||
} catch (error) { | ||
res.status(500).json({ error: "Failed to fetch track data" }); | ||
} | ||
}); | ||
|
||
// Get recommendation from genre | ||
router.get("/recommendations", async (req, res) => { | ||
const { genres, limit = 10 } = req.query; // Default to "pop" if genres is not provided | ||
const accessToken = await getAccessToken(); | ||
|
||
try { | ||
const response = await axios.get( | ||
`https://api.spotify.com/v1/recommendations?seed_genres=${genres}&limit=${limit}`, | ||
{ | ||
headers: { | ||
Authorization: `Bearer ${accessToken}`, | ||
}, | ||
} | ||
); | ||
res.json(response.data); | ||
|
||
} catch (error) { | ||
console.error("Error fetching recommendations:", error); | ||
res.status(500).json({ error: "Failed to fetch recommendations" }); | ||
} | ||
}); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters