deemix-js/deemix/index.js

123 lines
3.8 KiB
JavaScript
Raw Normal View History

2021-04-01 11:38:59 +00:00
const got = require('got')
const {
generateTrackItem,
generateAlbumItem,
generatePlaylistItem,
generateArtistItem,
generateArtistDiscographyItem,
generateArtistTopItem
} = require('./itemgen.js')
const {
2021-06-05 16:20:22 +00:00
LinkNotSupported,
LinkNotRecognized
} = require('./errors.js')
2021-04-01 11:38:59 +00:00
async function parseLink(link){
2021-04-21 17:00:51 +00:00
if (link.includes('deezer.page.link')){
2021-09-30 17:00:36 +00:00
link = await got.get(link, {https: {rejectUnauthorized: false}}) // Resolve URL shortner
2021-04-08 15:39:33 +00:00
link = link.url
}
2021-04-01 11:38:59 +00:00
// Remove extra stuff
2021-04-21 17:00:51 +00:00
if (link.includes('?')) link = link.slice(0, link.indexOf('?'))
if (link.includes('&')) link = link.slice(0, link.indexOf('&'))
if (link.endsWith('/')) link = link.slice(0, -1) // Remove last slash if present
2021-04-01 11:38:59 +00:00
2021-04-21 17:00:51 +00:00
let link_type, link_id
2021-04-01 11:38:59 +00:00
2021-04-21 17:00:51 +00:00
if (!link.includes('deezer')) return [link, link_type, link_id] // return if not a deezer link
2021-04-01 11:38:59 +00:00
if (link.search(/\/track\/(.+)/g) != -1){
2021-04-21 17:00:51 +00:00
link_type = 'track'
link_id = /\/track\/(.+)/g.exec(link)[1]
2021-04-01 11:38:59 +00:00
}else if (link.search(/\/playlist\/(\d+)/g) != -1){
2021-04-21 17:00:51 +00:00
link_type = 'playlist'
link_id = /\/playlist\/(\d+)/g.exec(link)[1]
2021-04-01 11:38:59 +00:00
}else if (link.search(/\/album\/(.+)/g) != -1){
2021-04-21 17:00:51 +00:00
link_type = 'album'
link_id = /\/album\/(.+)/g.exec(link)[1]
2021-04-01 11:38:59 +00:00
}else if (link.search(/\/artist\/(\d+)\/top_track/g) != -1){
2021-04-21 17:00:51 +00:00
link_type = 'artist_top'
link_id = /\/artist\/(\d+)\/top_track/g.exec(link)[1]
2021-04-01 11:38:59 +00:00
}else if (link.search(/\/artist\/(\d+)\/discography/g) != -1){
2021-04-21 17:00:51 +00:00
link_type = 'artist_discography'
link_id = /\/artist\/(\d+)\/discography/g.exec(link)[1]
2021-04-01 11:38:59 +00:00
}else if (link.search(/\/artist\/(\d+)/g) != -1){
2021-04-21 17:00:51 +00:00
link_type = 'artist'
link_id = /\/artist\/(\d+)/g.exec(link)[1]
2021-04-01 11:38:59 +00:00
}
2021-04-21 17:00:51 +00:00
return [link, link_type, link_id]
2021-04-01 11:38:59 +00:00
}
2021-05-13 16:11:56 +00:00
async function generateDownloadObject(dz, link, bitrate, plugins={}, listener){
2021-04-21 17:00:51 +00:00
let link_type, link_id
[link, link_type, link_id] = await parseLink(link)
2021-04-01 11:38:59 +00:00
if (link_type == null || link_id == null){
const pluginNames = Object.keys(plugins)
let currentPlugin
let item = null
for (let i = 0; i < pluginNames.length; i++){
currentPlugin = plugins[pluginNames[i]]
item = await currentPlugin.generateDownloadObject(dz, link, bitrate, listener)
if (item) break
}
2021-06-05 16:20:22 +00:00
if (item) return item
throw new LinkNotRecognized(link)
}
2021-04-01 11:38:59 +00:00
2021-04-21 17:00:51 +00:00
switch (link_type) {
2021-04-01 11:38:59 +00:00
case 'track':
2021-04-21 17:00:51 +00:00
return generateTrackItem(dz, link_id, bitrate)
2021-04-01 11:38:59 +00:00
case 'album':
2021-04-21 17:00:51 +00:00
return generateAlbumItem(dz, link_id, bitrate)
2021-04-01 11:38:59 +00:00
case 'playlist':
2021-04-21 17:00:51 +00:00
return generatePlaylistItem(dz, link_id, bitrate)
2021-04-01 11:38:59 +00:00
case 'artist':
2021-05-13 16:11:56 +00:00
return generateArtistItem(dz, link_id, bitrate, listener)
2021-04-01 11:38:59 +00:00
case 'artist_discography':
2021-05-13 16:11:56 +00:00
return generateArtistDiscographyItem(dz, link_id, bitrate, listener)
2021-04-01 11:38:59 +00:00
case 'artist_top':
2021-04-21 17:00:51 +00:00
return generateArtistTopItem(dz, link_id, bitrate)
2021-06-05 16:20:22 +00:00
default:
throw new LinkNotSupported(link)
2021-04-01 11:38:59 +00:00
}
}
module.exports = {
parseLink,
generateDownloadObject,
types: {
2021-04-27 17:25:14 +00:00
...require('./types/index.js'),
2021-04-01 11:38:59 +00:00
...require('./types/Album.js'),
...require('./types/Artist.js'),
...require('./types/Date.js'),
...require('./types/Lyrics.js'),
...require('./types/Picture.js'),
...require('./types/Playlist.js'),
...require('./types/Track.js'),
2021-04-27 17:25:14 +00:00
downloadObjects: require('./types/DownloadObjects.js')
},
2021-04-27 17:25:14 +00:00
itemgen: {
generateTrackItem,
generateAlbumItem,
generatePlaylistItem,
generateArtistItem,
generateArtistDiscographyItem,
generateArtistTopItem
},
settings: require('./settings.js'),
downloader: require('./downloader.js'),
decryption: require('./decryption.js'),
tagger: require('./tagger.js'),
utils: {
...require('./utils/index.js'),
localpaths: require('./utils/localpaths.js'),
2021-05-16 19:40:14 +00:00
pathtemplates: require('./utils/pathtemplates.js'),
deezer: require('./utils/deezer.js')
},
plugins: {
spotify: require('./plugins/spotify.js')
2021-04-27 17:25:14 +00:00
}
2021-04-01 11:38:59 +00:00
}