murdos / musicbrainz-userscripts

Collection of userscripts for MusicBrainz, by various authors
535 stars 89 forks source link

beatport_importer: Adapt to website changes #538

Closed zyoshoka closed 1 year ago

zyoshoka commented 1 year ago

Beatport website has been updated recently, and the userscript no longer work at all. This PR adapts to these changes, but several problems still remain.

zyoshoka commented 1 year ago

The problem I mentioned above, which occurs when the track number is over 100, can be solved by sending a direct API request, as shown in the code below, but it requires a Bearer token. This token is apparently issued separately for each client (unlike what Apple Music does), so you need to get your own token somehow. A quick way is to go to the network tab of your browser and find the request you sent to the API and put the token you find in the header in the script, but this is not a smart way as you will have to renew the script every time the token expires. Is there a better way?

$(document).ready(() => {
    MBImportStyle();

    const release_url = window.location.href.replace('/?.*$/', '').replace(/#.*$/, '');

    const data = JSON.parse(document.getElementById('__NEXT_DATA__').innerHTML);
    const release_data = data.props.pageProps.release;

    const initial_url = `https://api.beatport.com/v4/catalog/releases/${release_data.id}/tracks/?per_page=100`;
    const tracks = [];
    const accessToken = ''; // Type your access token here

    getTracks(initial_url, tracks, accessToken, tracks_data => {
        const isrcs = tracks_data.map(track => track.isrc);
        const mbrelease = retrieveReleaseInfo(release_url, release_data, tracks_data);

        setTimeout(() => insertLink(mbrelease, release_url, isrcs), 1000);
    });
});

function getTracks(url, tracks, accessToken, callback) {
    $.ajax({
        url: url,
        type: 'GET',
        beforeSend: xhr => xhr.setRequestHeader('Authorization', `Bearer ${accessToken}`),
        success: data => {
            data.results.forEach(result => tracks.push(result));

            if (data.next) {
                getTracks(data.next, tracks, callback);
            } else {
                callback(tracks);
            }
        },
    });
}
atj commented 1 year ago

To be honest, I'd just merge this PR as is for now and not worry too much about the 0.01% of releases with > 100 tracks. You could then raise a new PR later if you find a reasonable way to resolve it.

However it's not my call, so let's see what @kellnerd's thoughts are...

kellnerd commented 1 year ago

I agree with @atj, let's bump the version and release it to have a working userscript again.

zyoshoka commented 1 year ago

Thank you @atj and @kellnerd. I will create an issue.