2015-12-13 04:42:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2015-12-20 12:17:35 +00:00
|
|
|
use Lastfm;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property string path
|
|
|
|
*/
|
|
|
|
class Song extends Model
|
|
|
|
{
|
|
|
|
protected $guarded = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attributes to be hidden from JSON outputs.
|
|
|
|
* Here we specify to hide lyrics as well to save some bandwidth (actually, lots of it).
|
|
|
|
* Lyrics can then be queried on demand.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $hidden = ['lyrics', 'created_at', 'updated_at', 'path', 'mtime'];
|
|
|
|
|
2015-12-20 18:09:34 +00:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $casts = [
|
2015-12-21 02:18:00 +00:00
|
|
|
'length' => 'float',
|
2015-12-20 18:09:34 +00:00
|
|
|
];
|
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
public function album()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Album::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function playlists()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(Playlist::class);
|
|
|
|
}
|
|
|
|
|
2015-12-20 12:17:35 +00:00
|
|
|
/**
|
|
|
|
* Scrobble the song using Last.fm service.
|
|
|
|
*
|
|
|
|
* @param string $timestamp The UNIX timestamp in which the song started playing.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2015-12-20 12:30:28 +00:00
|
|
|
public function scrobble($timestamp)
|
2015-12-20 12:17:35 +00:00
|
|
|
{
|
|
|
|
// Don't scrobble the unknown guys. No one knows them.
|
2015-12-21 13:49:00 +00:00
|
|
|
if ($this->album->artist->isUnknown()) {
|
2015-12-20 12:17:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the current user hasn't connected to Last.fm, don't do shit.
|
2015-12-21 13:49:00 +00:00
|
|
|
if (!$sessionKey = auth()->user()->getLastfmSessionKey()) {
|
2015-12-20 12:17:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Lastfm::scrobble(
|
2015-12-20 12:30:28 +00:00
|
|
|
$this->album->artist->name,
|
|
|
|
$this->title,
|
2015-12-20 12:17:35 +00:00
|
|
|
$timestamp,
|
|
|
|
$this->album->name === Album::UNKNOWN_NAME ? '' : $this->album->name,
|
|
|
|
$sessionKey
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
/**
|
|
|
|
* Sometimes the tags extracted from getID3 are HTML entity encoded.
|
|
|
|
* This makes sure they are always sane.
|
|
|
|
*
|
|
|
|
* @param $value
|
|
|
|
*/
|
|
|
|
public function setTitleAttribute($value)
|
|
|
|
{
|
|
|
|
$this->attributes['title'] = html_entity_decode($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Some songs don't have a title.
|
|
|
|
* Fall back to the file name (without extension) for such.
|
|
|
|
*
|
|
|
|
* @param $value
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getTitleAttribute($value)
|
|
|
|
{
|
|
|
|
return $value ?: pathinfo($this->path, PATHINFO_FILENAME);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepare the lyrics for displaying.
|
|
|
|
*
|
|
|
|
* @param $value
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getLyricsAttribute($value)
|
|
|
|
{
|
|
|
|
return nl2br($value);
|
|
|
|
}
|
|
|
|
}
|