PHP MusicBrainz get the first release date

444 Views Asked by At

i am trying to get the first release date of a song using Musicbrainz. To get this i am using the mikealmond musicBrainz library.

The problem i have is that when i try to execute exactly the same code as in this example (https://github.com/mikealmond/MusicBrainz/blob/master/examples/first-recording-search.php) i always get an authentication error.

Client error response [status code] 401 [reason phrase] Unauthorized [url] http://musicbrainz.org/ws/2/artist/0383dadf-2a4e-4d10-a46a-e9e041da8eb3?inc=releases+recordings+release-groups+user-ratings&fmt=json

Therefore i tried to add my username and password to the request:

$brainz = new MusicBrainz(new GuzzleHttpAdapter(new Client()),'myusername','mypassword'); 
$brainz->setUserAgent('myapplicationname', '0.2', 'http://localhost:443/');

If i call the url in the error message manually and enter my username and password i get the array i expect.

I just had a discovery: If I removed -"+ user - ratings"- it does not require authentication.

Therefore i commented the lines with "user - ratings" in my project

Now i think it works, but the performance of the query is very bad and often i get Error 503 // The MusicBrainz web server is currently busy. Please try again later. // It takes a few seconds just for one song. Does someone know if this is normal or if i still have some kind of mistake?

My code....

//Create new MusicBrainz object
    $brainz = new MusicBrainz(new GuzzleHttpAdapter(new Client()), 'username', 'password');
    $brainz->setUserAgent('applicationname', '0.2', 'http://localhost:443/');

    // set defaults
    $artistId = null;
    $songId = null;
    $lastScore = null;
    $firstRecording = array(
        'releaseDate' => new DateTime()
    );
    // Set the search arguments to pass into the RecordingFilter
    $args = array(
        "recording" => 'we will rock you',
        "artist" => 'Queen',
    );
    try {
        // Find all the recordings that match the search and loop through them
        $recordings = $brainz->search(new RecordingFilter($args));

$recorings i can print and in the loop i can print each $recording, but the error comes when i extract the informations


 /** @var $recording \MusicBrainz\Recording */
            foreach ($recordings as $recording) {
                // if the recording has a lower score than the previous recording, stop the loop.
                // This is because scores less than 100 usually don't match the search well
                if (null != $lastScore && $recording->getScore() < $lastScore) {
                    break;
                }
                $lastScore = $recording->getScore();
                $releaseDates = $recording->getReleaseDates();
                $oldestReleaseKey = key($releaseDates);
                if (strtoupper($recording->getArtist()->getName()) == strtoupper($args['artist'])
                    && $releaseDates[$oldestReleaseKey] < $firstRecording['releaseDate']
                ) {
                    $firstRecording = array(
                        'releaseDate' => $recording->releases[$oldestReleaseKey]->getReleaseDate()
                    );
                }
            }
            pr($firstRecording);
        } catch (Exception $e) {
            pr($e->getMessage());
        }
1

There are 1 best solutions below

0
On
 $brainz = new MusicBrainz(new GuzzleHttpAdapter(new Client()), 'username', 'password');

You must set your MusicBrainz account credentials. Replace 'username' with your account username, and 'password' with the password used to login to MusicBrainz.org