Skip to content

Commit

Permalink
Merge pull request #26 from floydian77/pr-floydian77-scrobble-a-batch…
Browse files Browse the repository at this point in the history
…-of-tracks

Update TrackApi.php - Scrobble multiple tracks in a batch
  • Loading branch information
devilcius authored Mar 9, 2018
2 parents b257bb9 + dd7c149 commit 27731e5
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 23 deletions.
63 changes: 42 additions & 21 deletions src/lastfmapi/Api/TrackApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -526,27 +526,48 @@ public function share($methodVars) {
public function scrobble($methodVars) {
// Only allow full authed calls
if ( $this->fullAuth == true ) {
// Check for required variables
if ( !empty($methodVars['artist']) && !empty($methodVars['track']) && !empty($methodVars['timestamp']) ) {
$vars = array(
'method' => 'track.scrobble',
'api_key' => $this->auth->apiKey,
'sk' => $this->auth->sessionKey
);
$vars = array_merge($vars, $methodVars);
$sig = $this->apiSig($this->auth->apiSecret, $vars);
$vars['api_sig'] = $sig;

if ( $call = $this->apiPostCall($vars) ) {
return true;
}
else {
return false;
}
}
else {
throw new InvalidArgumentException('You must include artist, track and timestamp variables in the call for this method');
}
// Check if $methodVars is not an multi dimensional array
if (count($methodVars) == count($methodVars, COUNT_RECURSIVE)) {
// Array is singular, convert to multi dimensional
$methodVars = array($methodVars);
}

// Check for maximum batch size
if (count($methodVars) <= 50) {
// Build scrobble params, and add array notation to key names
$params = array();
foreach($methodVars as $i => $track) {
// Check for required variables
if ( !empty($track['artist']) && !empty($track['track']) && !empty($track['timestamp']) ) {
foreach($track as $key => $value) {
$_key = sprintf("%s[%d]", $key, $i);
$params[$_key] = $value;
}
}
else {
throw new InvalidArgumentException('You must include artist, track and timestamp variables in the call for this method');
}
}

$vars = array(
'method' => 'track.scrobble',
'api_key' => $this->auth->apiKey,
'sk' => $this->auth->sessionKey
);
$vars = array_merge($vars, $params);
$sig = $this->apiSig($this->auth->apiSecret, $vars);
$vars['api_sig'] = $sig;

if ( $call = $this->apiPostCall($vars) ) {
return true;
}
else {
return false;
}
}
else {
throw new InvalidArgumentException('Batch size exceed maximum of 50 tracks');
}
}
else {
throw new NotAuthenticatedException('Method requires full auth. Call auth.getSession using lastfmApiAuth class');
Expand Down
22 changes: 20 additions & 2 deletions tests/Api/AuthenticatedTrackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function testUnloveTrack()
$this->assertTrue($result);
}

public function testScrobble()
public function testScrobbleASingleTrack()
{
$result = $this->trackApi->scrobble(array(
'artist' => self::ARTIST_NAME,
Expand All @@ -55,5 +55,23 @@ public function testScrobble()
);

$this->assertTrue($result);
}
}

public function testScrobbleABatchOfTracks()
{
$result = $this->trackApi->scrobble(array(
array(
'artist' => self::ARTIST_NAME,
'track' => self::TRACK_NAME,
'timestamp' => time() - 60
),
array(
'artist' => self::ARTIST_NAME,
'track' => self::TRACK_NAME,
'timestamp' => time() - 120
)
));

$this->assertTrue($result);
}
}

0 comments on commit 27731e5

Please sign in to comment.