Skip to content

Commit

Permalink
fix: New Releases custom app for Spotify 1.1.81+ (#1563)
Browse files Browse the repository at this point in the history
* Fix `New Releases` custom app for Spotify 1.1.81+

- Based on proposed fix for `Shuffle+` (#1559)
- Fixes #1539 #1530 

Notes:
- Can probably be written nicer - this is my scuffed attempt to fix it
- May or may not actually show all new releases for all followed artists - have over 665 of them but I don't think I'm getting all of them (see below)
- May or may not return `error 500` (added `.catch()` block keeps it from breaking whole custom app)

* Minimize `internal server error: 500`...

...for big libraries of followed artists.

Changes:
- Change `URL` to query only discographies
- Limit amount of queried albums to 5

Notes:
- This does **NOT** fixes erroring fully - it only maxes out amount of data you can query before getting rate limited
- The more options you select (ex. albums + EPs + podcasts), the less data you may receive
- To max number of albums received, I recommend to select only `Albums` (since `Singles and EPs` will probably get displayed anyway...)

* Add notifications when error occurred

Notifications added:
- Error code (`500`, `429` etc.)
- Amount of followed artists to fetch releases from
- Amount of followed artists failed to fetch releases from

I guess we have to get along with getting `500-ed` - one time it fetches everything instantly, second time it drops 60 artists...

* "Prettify" file to pass `Prettier` test

* Fix filtering, counter...

- Fixing filtering as no matter was what set in config, it always displayed everything as "Album"
- Fixing "Missing releases from..." counter - should properly reset now

What broke again:
- "Appears on" releases cannot be retrieved with that API endpoint - this filter is just there and doesn't do anything - this prevents from showing everything as "Appears on" etc.

Notes:
- There seem to be an API endpoint for retrieving "Related content" stuff - problem is that would query everything TWICE... which breaks everything even more (and we don't wanna do that)
- If someone knows how to query everything using separate endpoint without doing it 4 times, let me know...

* Forgor `( )`... Oops... 💀

I forgor 💀

* Include requested changes

Changes:
- Properly encode URI including variables
- Make `limit` variable customizable via settings (set default to 5)
- Make error messages as "dev console only"

Notes:
- Errors displayed in console may be a little spammy - if we get error early, there may be lots of lines displaying it + counter...
   * I'm not too sure how to tackle this - just remove them altogether? Or is there a function that could "suppress" them?
   * Switching from normal `log` to `debug` may help a little as they will be only visible if user has set their console log level to include `Verbose`
- Making `limit` customizable may lead to even more errors but fuck it I guess - it's better to have a choice than not, right?
   * It can be manually input via custom app settings (same place where other options are) - there is no list etc. - it's just normal input field
- Set `offset` value as const `0` and not making it customizable (cause why would you want to start searching from ex. 3rd album instead of beginning, right?)
- Leaving `Fetching releases from...` notification cause it looks cool - it's fun to know how many followed artists you have 😆
  • Loading branch information
th3an7 authored Apr 6, 2022
1 parent a82586b commit 0a89573
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 12 deletions.
7 changes: 7 additions & 0 deletions CustomApps/new-releases/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,13 @@ function openConfig() {
},
when: () => true,
},
{
desc: "Track limit",
key: "limit",
defaultValue: CONFIG.limit,
type: ConfigInput,
when: () => true,
},
{
desc: "Date locale",
key: "locale",
Expand Down
52 changes: 40 additions & 12 deletions CustomApps/new-releases/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const CONFIG = {
range: localStorage.getItem("new-releases:range") || "30",
locale: localStorage.getItem("new-releases:locale") || navigator.language,
relative: getConfig("new-releases:relative", false),
limit: localStorage.getItem("new-releases:limit") || 5,
};

let gridList = [];
Expand Down Expand Up @@ -224,19 +225,34 @@ async function getArtistList() {
const body = await CosmosAsync.get("sp://core-collection/unstable/@/list/artists/all?responseFormat=protobufJson", {
policy: { list: { link: true, name: true } },
});
count(true);
return body.item;
}

async function getArtistEverything(artist) {
const uid = artist.link.replace("spotify:artist:", "");
const body = await CosmosAsync.get(`hm://artist/v3/${uid}/desktop/entity?format=json`);
const releases = body?.releases;
const uid = artist.link;
const offset = 0;
const limit = CONFIG.limit;
const body = await CosmosAsync.get(
`https://api-partner.spotify.com/pathfinder/v1/query?operationName=queryArtistDiscographyAll&variables=${encodeURIComponent(

This comment has been minimized.

Copy link
@Exhigh

Exhigh Apr 17, 2022

Contributor

Since, we got to know that wg:// has been replaced instead hm:// (thanks to @ririxidev), can't we technically revert back this commit, but keep changes like showing releases being fetched from 'x' artists, since this method is prone to being rate limited. Did test the change to wg:// and can confirm that it's working. @th3an7 @afonsojramos

Same could be said for shuffle+ as well, but i didn't look into shuffle+ working after reverting back to the previous state with the protocol being changed to wg://.

This comment has been minimized.

Copy link
@rxri

rxri Apr 17, 2022

Member

Will do later.
edit: nvm has been merged.

`{\"uri\":\"${uid}\",\"offset\":${offset},\"limit\":${limit}}`
)}&extensions=%7B%22persistedQuery%22%3A%7B%22version%22%3A1%2C%22sha256Hash%22%3A%22e108cfbb0b850e577260638713504712091e98dd98ef768d7724c1c444de4cab%22%7D%7D`
);
const releases = body?.data?.artist.discography?.all?.items.map((item) => item.releases.items[0]);
const items = [];
const types = [
[CONFIG.album, releases?.albums?.releases, Spicetify.Locale.get("album")],
[CONFIG["appears-on"], releases?.appears_on?.releases, Spicetify.Locale.get("artist.appears-on")],
[CONFIG.compilations, releases?.compilations?.releases, Spicetify.Locale.get("compilation")],
[CONFIG["single-ep"], releases?.singles?.releases, Spicetify.Locale.get("single") + "/" + Spicetify.Locale.get("ep")],
[CONFIG.album, releases.filter((releases) => releases.type === "ALBUM"), Spicetify.Locale.get("album")],
[
CONFIG["appears-on"],
releases.filter((releases) => releases.type !== ("ALBUM" || "EP" || "COMPILATION" || "SINGLE")),
Spicetify.Locale.get("artist.appears-on"),
],
[CONFIG.compilations, releases.filter((releases) => releases.type === "COMPILATION"), Spicetify.Locale.get("compilation")],
[
CONFIG["single-ep"],
releases.filter((releases) => releases.type === ("SINGLE" || "EP")),
Spicetify.Locale.get("single") + "/" + Spicetify.Locale.get("ep"),
],
];
for (const type of types) {
if (type[0] && type[1]) {
Expand Down Expand Up @@ -264,7 +280,7 @@ async function getPodcastRelease(uri) {
}

function metaFromTrack(artist, track) {
const time = new Date(track.year, track.month - 1, track.day);
const time = new Date(track.date.isoString);
if (today - time.getTime() < limitInMs) {
return {
uri: track.uri,
Expand All @@ -273,21 +289,33 @@ function metaFromTrack(artist, track) {
name: artist.name,
uri: artist.link,
},
imageURL: track.cover.uri,
imageURL: track.coverArt.sources[2].url,
time,
trackCount: track.track_count,
trackCount: track.tracks.totalCount,
};
}
return null;
}

var count = (function () {
var counter = 0;
return function (reset = false) {
return reset ? (counter = 0) : counter++;
};
})();

async function fetchTracks() {
let artistList = await getArtistList();
Spicetify.showNotification(`Fetching releases from ${artistList.length} artists`);

const requests = artistList.map(async (obj) => {
const artist = obj.artistMetadata;

return await getArtistEverything(artist);
return await getArtistEverything(artist).catch((err) => {
console.debug("Could not fetch all releases - error code: " + err.status);
if ((err.status = 500)) {
console.debug(`Missing releases from ${count()} artists`);
}
});
});

return await Promise.all(requests);
Expand Down

0 comments on commit 0a89573

Please sign in to comment.