-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
108 lines (95 loc) · 2.81 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
require('dotenv').config()
const axios = require('axios')
const fs = require('fs')
const path = require('path')
const utf8 = require('utf8')
const unzipper = require('unzipper')
var parseHeader = require('parse-http-header')
const {
BASE_URL,
SESSION,
SECURE_SESSION,
MUSIC_DIR,
PLEX_URL,
PLEX_TOKEN,
PLEX_LIBRARY_ID
} = process.env
const account = axios.create({
baseURL: BASE_URL,
headers: {
Cookie: `session=${SESSION}; secureSession=${SECURE_SESSION}`
},
withCredentials: true
})
const cacheFile = path.resolve((MUSIC_DIR || 'downloads'), '7d-dl.cache.json')
let cache
try {
cache = fs.readFileSync(cacheFile)
cache = JSON.parse(cache)
} catch (e) {
cache = { downloaded: [] }
fs.writeFileSync(cacheFile, JSON.stringify(cache))
}
if (!fs.existsSync('./downloads')) {
fs.mkdirSync('./downloads')
}
const download = id => account.get(`/download/release/${id}`, {
responseType: 'stream'
}).then(response => {
let { filename } = parseHeader(response.headers['content-disposition'])
filename = filename.replace(/\"/g, '')
filename = utf8.decode(filename)
console.log(`Downloading "${filename}"`)
const file = path.resolve(__dirname, 'downloads', `${filename}.part`)
response.data.pipe(fs.createWriteStream(file))
response.data.on('end', () => {
const zipFile = file.replace('.zip.part', '.zip')
fs.renameSync(file, zipFile)
console.log('Download complete!')
markComplete(id)
copy(zipFile)
})
})
const markComplete = id => {
cache.downloaded.push(id)
fs.writeFileSync('cache.json', JSON.stringify(cache))
}
const copy = file => {
const musicPath = MUSIC_DIR || path.resolve(__dirname, 'downloads')
console.log(`Extracting ${file} to ${musicPath}`)
const extract = fs.createReadStream(file)
.pipe(unzipper.Extract({ path: musicPath }))
extract.on('close', () => {
console.log('Done extracting, deleting ZIP')
fs.unlinkSync(file)
plexScan()
})
}
const plexScan = () => {
if (PLEX_URL) {
const plexUpdate = `${PLEX_URL}/library/sections/${PLEX_LIBRARY_ID}/refresh?X-Plex-Token=${PLEX_TOKEN}`
console.log({ plexUpdate })
axios.get(plexUpdate).then(() => {
console.log('Triggered Plex scan')
}).catch(err => {
console.log('Error triggering Plex library refresh. Is your X-Plex-Token valid?')
})
}
}
account.get('/yourmusic', {
Accept: 'application/json, text/javascript, */*; q=0.01',
'X-Requested-With': 'XMLHttpRequest'
}).then(response => {
const releases = response.data.ownedReleaseIds
releases.forEach(release => {
if (cache.downloaded.includes(release)) {
// console.log(`Already downloaded ${release}`)
return
}
download(release).catch(err => {
console.log('Error trying to download track', err)
})
})
}).catch(err => {
console.log('Error tring to access account', err)
})