I am currently developing a web application that will find artists and their associates titles. To do this, I decided to parse the API MusicBrainz, through this package (which documentation is here).
Suppose I already have the artist mbid (unique identifier for MusicBrainz).
The problem is that if I do a search on David Guetta (mbid = 302bd7b9-d012-4360-897a-93b00c855680
) for example, the script tells me that the number of releases is 196. However, the API provides 100 maximum results per request.
// Instantiate a MusicBrainz object
$brainz = new MusicBrainz(new GuzzleHttpAdapter(new Client()));
$brainz->setUserAgent('ApplicationName', '0.2', 'http://example.com');
$limit = 100;
$offset = 0;
$includes = array('labels', 'recordings');
try {
$details = $brainz->browseRelease('artist', '302bd7b9-d012-4360-897a-93b00c855680', $includes, $limit, $offset);
print_r($details);
} catch (Exception $e) {
print $e->getMessage();
}
The function browseRelease
below allows us to define such parameters as limit
and offset
.
public function browseRelease($entity, $mbid, $includes = array(), $limit = 25, $offset = null, $releaseType = array(), $releaseStatus = array())
{
if (!in_array($entity, array('artist', 'label', 'recording', 'release-group'))) {
throw new Exception('Invalid browse entity for release');
}
return $this->browse(
new Filters\ReleaseFilter(array()),
$entity,
$mbid,
$includes,
$limit,
$offset,
$releaseType,
$releaseStatus
);
}
Now we come to my real question in this post.
If I defined $limit = 100
and $offset = 0
for the first query, I don't understand how to change these values depending on the number of total releases, and thus get the results that are between 100 and 196 (in this example).
The pseudo code answer from Jordumus is correct, but this is how you have to modify your code to make it work.
$limit = 100;
$offset = 0;
$release_count = 0;
$includes = array('labels', 'recordings');
do {
if ($offset != 0) sleep(1);
try {
$details = $brainz->browseRelease('artist', '302bd7b9-d012-4360-897a-93b00c855680', $includes, $limit, $offset);
$release_count = $details['release-count'];
print_r($details);
} catch (Exception $e) {
print $e->getMessage();
}
$offset += $limit;
} while ($offset < $release_count);
Note that it would be enough to set $release_count
the first time, since the value doesn't change, but resetting it also doesn't hurt.
I added a sleep
in there because of the required rate limiting which doesn't seem to be implemented in the PHP library.