Skip to content

Commit

Permalink
Better error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
coopw1 committed Jan 16, 2024
1 parent ec4c4e9 commit 18dcd56
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 140 deletions.
48 changes: 31 additions & 17 deletions src/commands/general/util/getAlbumCover.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,37 @@ const axios = require("axios").default;
*/
module.exports = async (MBID) => {
// Get release MBID from Recording MBID
try {
const BASE_URL = "http://musicbrainz.org/ws/2/recording/";
const PARAMS = {
headers: {
"User-Agent": "DiscordBrainzBot/1.0.0 (coopwd@skiff.com)",
},
params: {
query: `rid:${MBID}`,
},
};
// Make request to MusicBrainz
const response = await axios.get(BASE_URL, PARAMS);

const releaseMBID = await response.data.recordings[0]?.releases[0].id;
return `https://coverartarchive.org/release/${releaseMBID}/front`;
} catch (error) {
console.log("getAlbumCover Error: " + error);
const BASE_URL = "http://musicbrainz.org/ws/2/recording/";
const PARAMS = {
headers: {
"User-Agent": "DiscordBrainzBot/1.0.0 (coopwd@skiff.com)",
},
params: {
query: `rid:${MBID}`,
},
};
// Make request to MusicBrainz
const response = await axios.get(BASE_URL, PARAMS).catch(function (error) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.log(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log("Error", error.message);
}
console.log(error.config);
return "error";
}
});

const releaseMBID = await response.data.recordings[0]?.releases[0].id;
return `https://coverartarchive.org/release/${releaseMBID}/front`;
};
43 changes: 29 additions & 14 deletions src/commands/general/util/getCurrentlyPlaying.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,37 @@ const axios = require("axios").default;
* @return {promise<string>} - A promise that resolves to the currently playing track.
*/
module.exports = async (listenBrainzToken, brainzUsername) => {
try {
const BASE_URL = `https://api.listenbrainz.org/1/user/${brainzUsername}/playing-now`;
const AUTH_HEADER = {
Authorization: `Token ${listenBrainzToken}`,
"User-Agent": "DiscordBrainzBot/1.0.0 (coopwd@skiff.com)",
};
const BASE_URL = `https://api.listenbrainz.org/1/user/${brainzUsername}/playing-now`;
const AUTH_HEADER = {
Authorization: `Token ${listenBrainzToken}`,
"User-Agent": "DiscordBrainzBot/1.0.0 (coopwd@skiff.com)",
};

// Make request to ListenBrainz
const response = await axios.get(BASE_URL, {
// Make request to ListenBrainz
const response = await axios
.get(BASE_URL, {
headers: AUTH_HEADER,
})
.catch(function (error) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.log(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log("Error", error.message);
}
console.log(error.config);
return "error";
});

const currentlyPlaying = response.data.payload;
return currentlyPlaying;
} catch (error) {
console.log("getCurrentlyPlaying Error: " + error);
return "error";
}
const currentlyPlaying = response.data.payload;
return currentlyPlaying;
};
65 changes: 39 additions & 26 deletions src/commands/general/util/getMBID.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,45 @@ module.exports = async (
trackName,
MBIDType = "recordings"
) => {
try {
const BASE_URL = `http://musicbrainz.org/ws/2/${MBIDType.slice(0, -1)}/`;
let PARAMS = {
params: {
query: ``,
},
headers: {
"User-Agent": "DiscordBrainzBot/1.0.0 (coopwd@skiff.com)",
},
};
if (artistName) {
PARAMS.params.query = PARAMS.params.query + `artist:${artistName}`;
}
if (releaseName) {
PARAMS.params.query = PARAMS.params.query + `release:${releaseName}`;
}
if (trackName) {
PARAMS.params.query = PARAMS.params.query + `recording:${trackName}`;
const BASE_URL = `http://musicbrainz.org/ws/2/${MBIDType.slice(0, -1)}/`;
let PARAMS = {
params: {
query: ``,
},
headers: {
"User-Agent": "DiscordBrainzBot/1.0.0 (coopwd@skiff.com)",
},
};
if (artistName) {
PARAMS.params.query = PARAMS.params.query + `artist:${artistName}`;
}
if (releaseName) {
PARAMS.params.query = PARAMS.params.query + `release:${releaseName}`;
}
if (trackName) {
PARAMS.params.query = PARAMS.params.query + `recording:${trackName}`;
}
// Make request to MusicBrainz
const response = await axios.get(BASE_URL, PARAMS).catch(function (error) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.log(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log("Error", error.message);
}
// Make request to MusicBrainz
const response = await axios.get(BASE_URL, PARAMS);

const MBID = response.data[MBIDType][0].id;
return MBID;
} catch (error) {
console.log("getMBID Error: " + error);
console.log(error.config);
return "error";
}
});

const MBID = response.data[MBIDType][0].id;
return MBID;
};
43 changes: 29 additions & 14 deletions src/commands/general/util/getRecentlyPlayed.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,37 @@ const axios = require("axios").default;
* @return {promise<Object>} An object of recently played tracks.
*/
module.exports = async (listenBrainzToken, brainzUsername) => {
try {
const BASE_URL = `https://api.listenbrainz.org/1/user/${brainzUsername}/listens`;
const AUTH_HEADER = {
Authorization: `Token ${listenBrainzToken}`,
"User-Agent": "DiscordBrainzBot/1.0.0 (coopwd@skiff.com)",
};
const BASE_URL = `https://api.listenbrainz.org/1/user/${brainzUsername}/listens`;
const AUTH_HEADER = {
Authorization: `Token ${listenBrainzToken}`,
"User-Agent": "DiscordBrainzBot/1.0.0 (coopwd@skiff.com)",
};

// Make request to ListenBrainz
const response = await axios.get(BASE_URL, {
// Make request to ListenBrainz
const response = await axios
.get(BASE_URL, {
headers: AUTH_HEADER,
})
.catch(function (error) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.log(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log("Error", error.message);
}
console.log(error.config);
return "error";
});

const recentlyPlayed = response.data.payload;
return recentlyPlayed;
} catch (error) {
console.log("getRecentlyPlayed Error: " + error);
return "error";
}
const recentlyPlayed = response.data.payload;
return recentlyPlayed;
};
1 change: 1 addition & 0 deletions src/commands/general/util/getSongInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ module.exports = async (
console.log("Error", error.message);
}
console.log(error.config);
return "error";
});

const songData = response.data;
Expand Down
79 changes: 46 additions & 33 deletions src/commands/general/util/getTopStatistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,41 +21,54 @@ module.exports = async (
MBID,
count = 100
) => {
try {
const AUTH_HEADER = {
Authorization: `Token ${listenBrainzToken}`,
"User-Agent": "DiscordBrainzBot/1.0.0 (coopwd@skiff.com)",
const AUTH_HEADER = {
Authorization: `Token ${listenBrainzToken}`,
"User-Agent": "DiscordBrainzBot/1.0.0 (coopwd@skiff.com)",
};

let BASE_URL;
let PARAMS;
if (getListeners) {
BASE_URL = `http://api.listenbrainz.org/1/stats/${searchType.slice(
0,
-1
)}/${MBID}/listeners`;
PARAMS = {
headers: AUTH_HEADER,
};
}
if (!getListeners) {
BASE_URL = `http://api.listenbrainz.org/1/stats/user/${brainzUsername}/${searchType}`;
PARAMS = {
params: {
range: timePeriod,
count: count,
},
headers: AUTH_HEADER,
};
}

let BASE_URL;
let PARAMS;
if (getListeners) {
BASE_URL = `http://api.listenbrainz.org/1/stats/${searchType.slice(
0,
-1
)}/${MBID}/listeners`;
PARAMS = {
headers: AUTH_HEADER,
};
}
if (!getListeners) {
BASE_URL = `http://api.listenbrainz.org/1/stats/user/${brainzUsername}/${searchType}`;
PARAMS = {
params: {
range: timePeriod,
count: count,
},
headers: AUTH_HEADER,
};
// Make request to ListenBrainz
const response = await axios.get(BASE_URL, PARAMS).catch(function (error) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.log(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log("Error", error.message);
}

// Make request to ListenBrainz
const response = await axios.get(BASE_URL, PARAMS);

const topStatistics = response.data.payload;
return topStatistics;
} catch (error) {
console.log("getTopArtists Error: " + error);
console.log(error.config);
return "error";
}
});

const topStatistics = response.data.payload;
return topStatistics;
};
42 changes: 29 additions & 13 deletions src/commands/general/util/getTotalScrobbles.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,37 @@ const axios = require("axios").default;
* @return {promise<number>} The total number of scrobbles for the user.
*/
module.exports = async (listenBrainzToken, brainzUsername) => {
try {
const BASE_URL = `https://api.listenbrainz.org/1/user/${brainzUsername}/listen-count`;
const AUTH_HEADER = {
Authorization: `Token ${listenBrainzToken}`,
"User-Agent": "DiscordBrainzBot/1.0.0 (coopwd@skiff.com)",
};
const BASE_URL = `https://api.listenbrainz.org/1/user/${brainzUsername}/listen-count`;
const AUTH_HEADER = {
Authorization: `Token ${listenBrainzToken}`,
"User-Agent": "DiscordBrainzBot/1.0.0 (coopwd@skiff.com)",
};

// Make request to ListenBrainz
const response = await axios.get(BASE_URL, {
// Make request to ListenBrainz
const response = await axios
.get(BASE_URL, {
headers: AUTH_HEADER,
})
.catch(function (error) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.log(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log("Error", error.message);
}
console.log(error.config);
return "error";
});

const totalScrobbles = await response.data.payload.count;
return totalScrobbles;
} catch (error) {
console.log("getTotalScrobbles Error: " + error);
}
const totalScrobbles = await response.data.payload.count;
return totalScrobbles;
};
Loading

0 comments on commit 18dcd56

Please sign in to comment.