deemix-js/deemix/index.js

85 lines
2.6 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')
async function parseLink(link){
2021-04-21 17:00:51 +00:00
if (link.includes('deezer.page.link')){
2021-04-08 15:39:33 +00:00
link = await got.get(link) // Resolve URL shortner
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
}
async function generateDownloadObject(dz, link, bitrate){
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
2021-04-21 17:00:51 +00:00
if (link_type == null || link_id == null) return null
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-04-21 17:00:51 +00:00
return generateArtistItem(dz, link_id, bitrate)
2021-04-01 11:38:59 +00:00
case 'artist_discography':
2021-04-21 17:00:51 +00:00
return generateArtistDiscographyItem(dz, link_id, bitrate)
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-04-01 11:38:59 +00:00
}
return null
}
module.exports = {
parseLink,
generateDownloadObject,
types: {
...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'),
},
downloader: require('./downloader.js')
2021-04-01 11:38:59 +00:00
}