mirror of
https://github.com/koel/koel
synced 2024-11-24 21:23:06 +00:00
cf27ed713d
Koel can now integrate and use the rich information from Last.fm. Now whenever a song is played, its album and artist information will be queried from Last.fm and cached for later use. What's better, if an album has no cover, Koel will try to update its cover if one is found on Last.fm. In order to use this feature, users only need to provide valid Last.fm API credentials (namely LASTFM_API_KEY and LASTFM_API_SECRET) in .env. A npm and gulp rebuild is also required - just like with every update.
77 lines
1.7 KiB
PHP
77 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Facades\Lastfm;
|
|
use App\Facades\Util;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
/**
|
|
* @property int id The model ID
|
|
* @property string name The artist name
|
|
*/
|
|
class Artist extends Model
|
|
{
|
|
const UNKNOWN_ID = 1;
|
|
const UNKNOWN_NAME = 'Unknown Artist';
|
|
|
|
protected $guarded = ['id'];
|
|
|
|
protected $hidden = ['created_at', 'updated_at'];
|
|
|
|
public function albums()
|
|
{
|
|
return $this->hasMany(Album::class);
|
|
}
|
|
|
|
/**
|
|
* Sometimes the tags extracted from getID3 are HTML entity encoded.
|
|
* This makes sure they are always sane.
|
|
*
|
|
* @param $value
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getNameAttribute($value)
|
|
{
|
|
return html_entity_decode($value ?: self::UNKNOWN_NAME);
|
|
}
|
|
|
|
/**
|
|
* Get an Artist object from their name.
|
|
* If such is not found, a new artist will be created.
|
|
*
|
|
* @param string $name
|
|
*
|
|
* @return Artist
|
|
*/
|
|
public static function get($name)
|
|
{
|
|
// Remove the BOM from UTF-8/16/32, as it will mess up the database constraints.
|
|
if ($encoding = Util::detectUTFEncoding($name)) {
|
|
$name = mb_convert_encoding($name, 'UTF-8', $encoding);
|
|
}
|
|
|
|
$name = trim($name) ?: self::UNKNOWN_NAME;
|
|
|
|
return self::firstOrCreate(compact('name'), compact('name'));
|
|
}
|
|
|
|
/**
|
|
* Get extra information about the artist from Last.fm.
|
|
*
|
|
* @return array|false
|
|
*/
|
|
public function getInfo()
|
|
{
|
|
if ($this->id === self::UNKNOWN_ID) {
|
|
return false;
|
|
}
|
|
|
|
$info = Lastfm::getArtistInfo($this->name);
|
|
|
|
// TODO: Copy the artist's image for our local use.
|
|
|
|
return $info;
|
|
}
|
|
}
|