mirror of
https://github.com/koel/koel
synced 2024-11-10 06:34:14 +00:00
Refactor Lastfm and iTunes services
This commit is contained in:
parent
0ad670ffff
commit
b227ece517
5 changed files with 77 additions and 46 deletions
|
@ -3,6 +3,8 @@
|
|||
namespace App\Services;
|
||||
|
||||
use Exception;
|
||||
use GuzzleHttp\Client;
|
||||
use Illuminate\Contracts\Cache\Repository as Cache;
|
||||
use Log;
|
||||
|
||||
class LastfmService extends ApiClient implements ApiConsumerInterface
|
||||
|
@ -20,6 +22,16 @@ class LastfmService extends ApiClient implements ApiConsumerInterface
|
|||
* @var string
|
||||
*/
|
||||
protected $keyParam = 'api_key';
|
||||
/**
|
||||
* @var Cache
|
||||
*/
|
||||
private $cache;
|
||||
|
||||
public function __construct(Client $client, Cache $cache)
|
||||
{
|
||||
parent::__construct($client);
|
||||
$this->cache = $cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if our application is using Last.fm.
|
||||
|
@ -53,23 +65,22 @@ class LastfmService extends ApiClient implements ApiConsumerInterface
|
|||
$name = urlencode($name);
|
||||
|
||||
try {
|
||||
$cacheKey = md5("lastfm_artist_$name");
|
||||
return $this->cache->remember(md5("lastfm_artist_$name"), 24 * 60 * 7, function () use ($name): ?array {
|
||||
$response = $this->get("?method=artist.getInfo&autocorrect=1&artist=$name");
|
||||
|
||||
if ($response = cache($cacheKey)) {
|
||||
$response = simplexml_load_string($response);
|
||||
} else {
|
||||
if ($response = $this->get("?method=artist.getInfo&autocorrect=1&artist=$name")) {
|
||||
cache([$cacheKey => $response->asXML()], 24 * 60 * 7);
|
||||
if (!$response) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
$response = json_decode(json_encode($response), true);
|
||||
$response = simplexml_load_string($response->asXML());
|
||||
$response = json_decode(json_encode($response), true);
|
||||
|
||||
if (!$response || !$artist = array_get($response, 'artist')) {
|
||||
return null;
|
||||
}
|
||||
if (!$response || !$artist = array_get($response, 'artist')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->buildArtistInformation($artist);
|
||||
return $this->buildArtistInformation($artist);
|
||||
});
|
||||
} catch (Exception $e) {
|
||||
Log::error($e);
|
||||
|
||||
|
@ -90,8 +101,8 @@ class LastfmService extends ApiClient implements ApiConsumerInterface
|
|||
'url' => array_get($artistData, 'url'),
|
||||
'image' => count($artistData['image']) > 3 ? $artistData['image'][3] : $artistData['image'][0],
|
||||
'bio' => [
|
||||
'summary' => $this->formatText(array_get($artistData, 'bio.summary')),
|
||||
'full' => $this->formatText(array_get($artistData, 'bio.content')),
|
||||
'summary' => $this->formatText(array_get($artistData, 'bio.summary', '')),
|
||||
'full' => $this->formatText(array_get($artistData, 'bio.content', '')),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
@ -113,21 +124,22 @@ class LastfmService extends ApiClient implements ApiConsumerInterface
|
|||
try {
|
||||
$cacheKey = md5("lastfm_album_{$albumName}_{$artistName}");
|
||||
|
||||
if ($response = cache($cacheKey)) {
|
||||
$response = simplexml_load_string($response);
|
||||
} else {
|
||||
if ($response = $this->get("?method=album.getInfo&autocorrect=1&album=$albumName&artist=$artistName")) {
|
||||
cache([$cacheKey => $response->asXML()], 24 * 60 * 7);
|
||||
return $this->cache->remember($cacheKey, 24 * 60 * 7, function () use ($albumName, $artistName): ?array {
|
||||
$response = $this->get("?method=album.getInfo&autocorrect=1&album=$albumName&artist=$artistName");
|
||||
|
||||
if (!$response) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
$response = json_decode(json_encode($response), true);
|
||||
$response = simplexml_load_string($response->asXML());
|
||||
$response = json_decode(json_encode($response), true);
|
||||
|
||||
if (!$response || !$album = array_get($response, 'album')) {
|
||||
return null;
|
||||
}
|
||||
if (!$response || !$album = array_get($response, 'album')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->buildAlbumInformation($album);
|
||||
return $this->buildAlbumInformation($album);
|
||||
});
|
||||
} catch (Exception $e) {
|
||||
Log::error($e);
|
||||
|
||||
|
@ -148,8 +160,8 @@ class LastfmService extends ApiClient implements ApiConsumerInterface
|
|||
'url' => array_get($albumData, 'url'),
|
||||
'image' => count($albumData['image']) > 3 ? $albumData['image'][3] : $albumData['image'][0],
|
||||
'wiki' => [
|
||||
'summary' => $this->formatText(array_get($albumData, 'wiki.summary')),
|
||||
'full' => $this->formatText(array_get($albumData, 'wiki.content')),
|
||||
'summary' => $this->formatText(array_get($albumData, 'wiki.summary', '')),
|
||||
'full' => $this->formatText(array_get($albumData, 'wiki.content', '')),
|
||||
],
|
||||
'tracks' => array_map(function ($track) {
|
||||
return [
|
||||
|
|
|
@ -2,12 +2,21 @@
|
|||
|
||||
namespace App\Services;
|
||||
|
||||
use Cache;
|
||||
use Exception;
|
||||
use GuzzleHttp\Client;
|
||||
use Illuminate\Contracts\Cache\Repository as Cache;
|
||||
use Log;
|
||||
|
||||
class iTunesService extends ApiClient implements ApiConsumerInterface
|
||||
{
|
||||
private $cache;
|
||||
|
||||
public function __construct(Client $client, Cache $cache)
|
||||
{
|
||||
parent::__construct($client);
|
||||
$this->cache = $cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether to use iTunes services.
|
||||
*/
|
||||
|
@ -28,8 +37,8 @@ class iTunesService extends ApiClient implements ApiConsumerInterface
|
|||
public function getTrackUrl(string $term, string $album = '', string $artist = ''): ?string
|
||||
{
|
||||
try {
|
||||
return Cache::remember(md5("itunes_track_url_{$term}{$album}{$artist}"), 24 * 60 * 7,
|
||||
function () use ($term, $album, $artist) {
|
||||
return $this->cache->remember(md5("itunes_track_url_{$term}{$album}{$artist}"), 24 * 60 * 7,
|
||||
function () use ($term, $album, $artist): string {
|
||||
$params = [
|
||||
'term' => $term.($album ? " $album" : '').($artist ? " $artist" : ''),
|
||||
'media' => 'music',
|
||||
|
|
|
@ -6,7 +6,8 @@ use App\Models\User;
|
|||
use App\Services\LastfmService;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use Mockery as m;
|
||||
use Illuminate\Contracts\Cache\Repository as Cache;
|
||||
use Mockery;
|
||||
use Tymon\JWTAuth\JWTAuth;
|
||||
|
||||
class LastfmTest extends TestCase
|
||||
|
@ -14,11 +15,11 @@ class LastfmTest extends TestCase
|
|||
public function testGetSessionKey()
|
||||
{
|
||||
/** @var Client $client */
|
||||
$client = m::mock(Client::class, [
|
||||
$client = Mockery::mock(Client::class, [
|
||||
'get' => new Response(200, [], file_get_contents(__DIR__.'../../blobs/lastfm/session-key.xml')),
|
||||
]);
|
||||
|
||||
self::assertEquals('foo', (new LastfmService($client))->getSessionKey('bar'));
|
||||
self::assertEquals('foo', (new LastfmService($client, app(Cache::class)))->getSessionKey('bar'));
|
||||
}
|
||||
|
||||
public function testSetSessionKey()
|
||||
|
|
|
@ -8,11 +8,17 @@ use App\Services\LastfmService;
|
|||
use Exception;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use Mockery as m;
|
||||
use Illuminate\Contracts\Cache\Repository as Cache;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
class LastfmServiceTest extends TestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
|
@ -22,14 +28,12 @@ class LastfmServiceTest extends TestCase
|
|||
$artist = factory(Artist::class)->make(['name' => 'foo']);
|
||||
|
||||
/** @var Client $client */
|
||||
$client = m::mock(Client::class, [
|
||||
$client = Mockery::mock(Client::class, [
|
||||
'get' => new Response(200, [], file_get_contents(__DIR__.'../../../blobs/lastfm/artist.xml')),
|
||||
]);
|
||||
|
||||
$api = new LastfmService($client);
|
||||
$info = $api->getArtistInformation($artist->name
|
||||
|
||||
);
|
||||
$api = new LastfmService($client, app(Cache::class));
|
||||
$info = $api->getArtistInformation($artist->name);
|
||||
|
||||
$this->assertEquals([
|
||||
'url' => 'http://www.last.fm/music/Kamelot',
|
||||
|
@ -50,11 +54,11 @@ class LastfmServiceTest extends TestCase
|
|||
$artist = factory(Artist::class)->make();
|
||||
|
||||
/** @var Client $client */
|
||||
$client = m::mock(Client::class, [
|
||||
$client = Mockery::mock(Client::class, [
|
||||
'get' => new Response(400, [], file_get_contents(__DIR__.'../../../blobs/lastfm/artist-notfound.xml')),
|
||||
]);
|
||||
|
||||
$api = new LastfmService($client);
|
||||
$api = new LastfmService($client, app(Cache::class));
|
||||
|
||||
$this->assertNull($api->getArtistInformation($artist->name));
|
||||
}
|
||||
|
@ -71,11 +75,11 @@ class LastfmServiceTest extends TestCase
|
|||
]);
|
||||
|
||||
/** @var Client $client */
|
||||
$client = m::mock(Client::class, [
|
||||
$client = Mockery::mock(Client::class, [
|
||||
'get' => new Response(200, [], file_get_contents(__DIR__.'../../../blobs/lastfm/album.xml')),
|
||||
]);
|
||||
|
||||
$api = new LastfmService($client);
|
||||
$api = new LastfmService($client, app(Cache::class));
|
||||
$info = $api->getAlbumInformation($album->name, $album->artist->name);
|
||||
|
||||
// Then I get the album's info
|
||||
|
@ -109,11 +113,11 @@ class LastfmServiceTest extends TestCase
|
|||
$album = factory(Album::class)->create();
|
||||
|
||||
/** @var Client $client */
|
||||
$client = m::mock(Client::class, [
|
||||
$client = Mockery::mock(Client::class, [
|
||||
'get' => new Response(400, [], file_get_contents(__DIR__.'../../../blobs/lastfm/album-notfound.xml')),
|
||||
]);
|
||||
|
||||
$api = new LastfmService($client);
|
||||
$api = new LastfmService($client, app(Cache::class));
|
||||
|
||||
$this->assertNull($api->getAlbumInformation($album->name, $album->artist->name));
|
||||
}
|
||||
|
|
|
@ -6,7 +6,9 @@ use App\Services\iTunesService;
|
|||
use Exception;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use Illuminate\Contracts\Cache\Repository as Cache;
|
||||
use Mockery;
|
||||
use Mockery\MockInterface;
|
||||
use Tests\TestCase;
|
||||
|
||||
class iTunesServiceTest extends TestCase
|
||||
|
@ -23,7 +25,10 @@ class iTunesServiceTest extends TestCase
|
|||
'get' => new Response(200, [], file_get_contents(__DIR__.'../../../blobs/itunes/track.json')),
|
||||
]);
|
||||
|
||||
$url = (new iTunesService($client))->getTrackUrl($term);
|
||||
/** @var Cache|MockInterface $cache */
|
||||
$cache = app(Cache::class);
|
||||
|
||||
$url = (new iTunesService($client, $cache))->getTrackUrl($term);
|
||||
|
||||
self::assertEquals(
|
||||
'https://itunes.apple.com/us/album/i-remember-you/id265611220?i=265611396&uo=4&at=1000lsGu',
|
||||
|
|
Loading…
Reference in a new issue