SpotifyDownloader

This commit is contained in:
bingus_violet 2024-06-08 19:14:16 -05:00
parent c782812464
commit 7c3a3d1fa7
3 changed files with 31 additions and 2 deletions

3
.gitignore vendored
View file

@ -131,4 +131,5 @@ dist
.pnp.* .pnp.*
cache cache
/cache.json /cache.json
/config.json

5
defaults/config.json Normal file
View file

@ -0,0 +1,5 @@
{
"Spotify": {
"client_secret": ""
}
}

View file

@ -6,12 +6,14 @@ WebSocket = require('ws')
const PORT = process.env.PORT || 8080 const PORT = process.env.PORT || 8080
const cachePath = path.join(__dirname, "cache"), const cachePath = path.join(__dirname, "cache"),
cacheFile = path.join(__dirname, "cache.json") cacheFile = path.join(__dirname, "cache.json"),
configFile = path.join(__dirname, "config.json")
if (!fs.existsSync(cachePath)) { fs.mkdirSync(cachePath) } if (!fs.existsSync(cachePath)) { fs.mkdirSync(cachePath) }
if (!fs.existsSync(cacheFile) || !JSON.parse(fs.readFileSync(cacheFile)).songs) { if (!fs.existsSync(cacheFile) || !JSON.parse(fs.readFileSync(cacheFile)).songs) {
fs.writeFileSync(cacheFile, fs.readFileSync(path.join(__dirname, "defaults/cache.json"))) fs.writeFileSync(cacheFile, fs.readFileSync(path.join(__dirname, "defaults/cache.json")))
} }
if (!fs.existsSync(configFile)) { fs.writeFileSync(configFile, fs.readFileSync(path.join(__dirname, "defaults/config.json")))}
var cacheDirs = ["songs", "imgs"] var cacheDirs = ["songs", "imgs"]
for (var i = 0; i < cacheDirs.length; i++) { for (var i = 0; i < cacheDirs.length; i++) {
@ -20,6 +22,14 @@ for (var i = 0; i < cacheDirs.length; i++) {
} }
} }
var conf = JSON.parse(fs.readFileSync(path.join(__dirname, "config.json")))
const Spotify = require("spotifydl-core").default
const spotifydl = new Spotify({
clientId: "5420a0e8fae24101ac6ad57278bde694",
clientSecret: conf.Spotify.client_secret
})
const songCache = path.join(cachePath, "songs"), const songCache = path.join(cachePath, "songs"),
imgCache = path.join(cachePath, "imgs") imgCache = path.join(cachePath, "imgs")
@ -27,6 +37,8 @@ var app = express()
const imgWaitMax = 2 const imgWaitMax = 2
app.use("/songs", express.static(songCache))
app.get("/cached/*", (req, res) => { app.get("/cached/*", (req, res) => {
var imgURL = req.originalUrl var imgURL = req.originalUrl
imgURL = imgURL.substring(imgURL.indexOf("/", 2) + 1) imgURL = imgURL.substring(imgURL.indexOf("/", 2) + 1)
@ -130,6 +142,17 @@ function socketeer() {
} else if (data.op == 0) { } else if (data.op == 0) {
var lanyardData = data.d var lanyardData = data.d
var spotify = lanyardData.spotify
if (conf.Spotify.client_secret && spotify) {
var trackURL = "https://open.spotify.com/track/" + spotify.track_id
var songPath = path.join(cachePath, "/songs/", spotify.track_id + ".mp3")
if (!fs.existsSync(songPath)) {
await spotifydl.downloadTrack(trackURL, songPath)
}
}
for (let index = 0; index < lanyardData.activities.length; index++) { for (let index = 0; index < lanyardData.activities.length; index++) {
const activity = lanyardData.activities[index]; const activity = lanyardData.activities[index];