|
|
@ -3,6 +3,12 @@ const axios = require('axios'); |
|
|
const cheerio = require('cheerio'); |
|
|
const cheerio = require('cheerio'); |
|
|
const logger = require('./winston'); |
|
|
const logger = require('./winston'); |
|
|
|
|
|
|
|
|
|
|
|
function asyncWait(delay){ |
|
|
|
|
|
return new Promise((resolve)=>{ |
|
|
|
|
|
setTimeout(resolve, delay); |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
class Importer extends EventEmitter { |
|
|
class Importer extends EventEmitter { |
|
|
constructor(deezer) { |
|
|
constructor(deezer) { |
|
|
super(); |
|
|
super(); |
|
|
@ -41,7 +47,7 @@ class Importer extends EventEmitter { |
|
|
this.tracks = []; |
|
|
this.tracks = []; |
|
|
try { |
|
|
try { |
|
|
//Fetch
|
|
|
//Fetch
|
|
|
let spotifyData = await Spotify.getEmbedData(uri); |
|
|
|
|
|
|
|
|
let spotifyData = await Spotify.getPlaylist(uri); |
|
|
if (!spotifyData.tracks.items) throw Error("No items!"); |
|
|
if (!spotifyData.tracks.items) throw Error("No items!"); |
|
|
|
|
|
|
|
|
for (let track of spotifyData.tracks.items) { |
|
|
for (let track of spotifyData.tracks.items) { |
|
|
@ -52,15 +58,33 @@ class Importer extends EventEmitter { |
|
|
(track.track.album.images.length > 0) ? track.track.album.images[0].url : null |
|
|
(track.track.album.images.length > 0) ? track.track.album.images[0].url : null |
|
|
); |
|
|
); |
|
|
//Match
|
|
|
//Match
|
|
|
try { |
|
|
|
|
|
|
|
|
let errorCount = 0; |
|
|
|
|
|
while(true){ |
|
|
let deezerData = await this.deezer.callPublicApi('track', 'isrc:' + track.track.external_ids.isrc); |
|
|
let deezerData = await this.deezer.callPublicApi('track', 'isrc:' + track.track.external_ids.isrc); |
|
|
if (deezerData.id.toString()) { |
|
|
|
|
|
//Found track
|
|
|
|
|
|
out.id = deezerData.id.toString(); |
|
|
|
|
|
out.ok = true; |
|
|
|
|
|
|
|
|
if(deezerData.error){ |
|
|
|
|
|
if(deezerData.error.message == 'Quota limit exceeded'){ |
|
|
|
|
|
await asyncWait(5000); |
|
|
|
|
|
errorCount++; |
|
|
|
|
|
if(errorCount >= 5){ |
|
|
|
|
|
logger.error(`Error importing: Spotify: ${track.track.id} ${deezerData.error.type}: ${deezerData.error.message}`); |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
}else{ |
|
|
|
|
|
logger.error(`Error importing: Spotify: ${track.track.id} ${deezerData.error.type}: ${deezerData.error.message}`); |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
}else{ |
|
|
|
|
|
try { |
|
|
|
|
|
if (deezerData.id.toString()) { |
|
|
|
|
|
//Found track
|
|
|
|
|
|
out.id = deezerData.id.toString(); |
|
|
|
|
|
out.ok = true; |
|
|
|
|
|
} |
|
|
|
|
|
} catch (e) { |
|
|
|
|
|
logger.error(`Error importing: Spotify: ${track.track.id} ${e}`); |
|
|
|
|
|
} |
|
|
|
|
|
break; |
|
|
} |
|
|
} |
|
|
} catch (e) { |
|
|
|
|
|
logger.error(`Error importing: Spotify: ${track.track.id} ${e}`); |
|
|
|
|
|
} |
|
|
} |
|
|
//Send back
|
|
|
//Send back
|
|
|
this.emit('imported', out); |
|
|
this.emit('imported', out); |
|
|
@ -96,6 +120,35 @@ class ImporterTrack { |
|
|
class Spotify { |
|
|
class Spotify { |
|
|
constructor() {} |
|
|
constructor() {} |
|
|
|
|
|
|
|
|
|
|
|
static async getPlaylist(uri){ |
|
|
|
|
|
const playlistID = uri.split(':')[2]; |
|
|
|
|
|
|
|
|
|
|
|
const tokenRequest = await axios.get('https://open.spotify.com/get_access_token?reason=transport', {responseType: 'json'}); |
|
|
|
|
|
const token = tokenRequest.data.accessToken; |
|
|
|
|
|
|
|
|
|
|
|
const playlistDataRequest = await axios.get(`https://api.spotify.com/v1/playlists/${playlistID}`, { |
|
|
|
|
|
responseType: 'json', |
|
|
|
|
|
headers: { |
|
|
|
|
|
'authorization': 'Bearer ' + token |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
const playlistData = playlistDataRequest.data; |
|
|
|
|
|
|
|
|
|
|
|
let next = playlistData.tracks.next |
|
|
|
|
|
while(next){ |
|
|
|
|
|
const nextItemsRequest = await axios.get(next, { |
|
|
|
|
|
responseType: 'json', |
|
|
|
|
|
headers: { |
|
|
|
|
|
'authorization': 'Bearer ' + token |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
next = nextItemsRequest.data.next; |
|
|
|
|
|
playlistData.tracks.items.push(...nextItemsRequest.data.items); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return playlistData; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
//Fetch JSON data from embeded spotify
|
|
|
//Fetch JSON data from embeded spotify
|
|
|
static async getEmbedData(uri) { |
|
|
static async getEmbedData(uri) { |
|
|
//Fetch
|
|
|
//Fetch
|
|
|
|