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

1
.gitignore vendored
View file

@ -132,3 +132,4 @@ dist
cache
/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 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(cacheFile) || !JSON.parse(fs.readFileSync(cacheFile)).songs) {
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"]
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"),
imgCache = path.join(cachePath, "imgs")
@ -27,6 +37,8 @@ var app = express()
const imgWaitMax = 2
app.use("/songs", express.static(songCache))
app.get("/cached/*", (req, res) => {
var imgURL = req.originalUrl
imgURL = imgURL.substring(imgURL.indexOf("/", 2) + 1)
@ -130,6 +142,17 @@ function socketeer() {
} else if (data.op == 0) {
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++) {
const activity = lanyardData.activities[index];