SpotifyDownloader
This commit is contained in:
parent
c782812464
commit
7c3a3d1fa7
3 changed files with 31 additions and 2 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -132,3 +132,4 @@ dist
|
||||||
|
|
||||||
cache
|
cache
|
||||||
/cache.json
|
/cache.json
|
||||||
|
/config.json
|
5
defaults/config.json
Normal file
5
defaults/config.json
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"Spotify": {
|
||||||
|
"client_secret": ""
|
||||||
|
}
|
||||||
|
}
|
25
index.js
25
index.js
|
@ -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];
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue