diff --git a/app/Console/Commands/ImportMLPMA.php b/app/Console/Commands/ImportMLPMA.php index b060f694..42ef517c 100644 --- a/app/Console/Commands/ImportMLPMA.php +++ b/app/Console/Commands/ImportMLPMA.php @@ -2,6 +2,7 @@ namespace App\Console\Commands; +use App\Genre; use App\Image; use App\Track; use App\User; @@ -87,6 +88,11 @@ class ImportMLPMA extends Command File::makeDirectory($tmpPath); } + $UNKNOWN_GENRE = Genre::firstOrCreate([ + 'name' => 'Unknown', + 'slug' => 'unknown' + ]); + $this->comment('Enumerating MLP Music Archive source files...'); $files = File::allFiles($mlpmaPath); $this->info(sizeof($files) . ' files found!'); @@ -189,6 +195,31 @@ class ImportMLPMA extends Command $isVocal = $parsedTags['lyrics'] !== null; + //========================================================================================================== + // Determine the genre + //========================================================================================================== + $genreName = $parsedTags['genre']; + $this->info('Genre: '.$genreName); + + if ($genreName) { + $genre = Genre::where('name', '=', $genreName)->first(); + if ($genre) { + $genreId = $genre->id; + + } else { + $genre = new Genre(); + $genre->name = $genreName; + $genre->slug = Str::slug($genreName); + $genre->save(); + $genreId = $genre->id; + $this->comment('Created a new genre!'); + } + + } else { + $genreId = $UNKNOWN_GENRE->id; // "Unknown" genre ID + } + + //========================================================================================================== // Determine which artist account this file belongs to using the containing directory. //========================================================================================================== @@ -264,7 +295,7 @@ class ImportMLPMA extends Command //========================================================================================================== // Is this part of an album? //========================================================================================================== - // Find the album if it exists and create it if it doesn't. + $albumId = null; $albumName = $parsedTags['album']; @@ -311,6 +342,7 @@ class ImportMLPMA extends Command $track->title = $parsedTags['title']; $track->cover_id = $coverId; $track->album_id = $albumId; + $track->genre_id = $genreId; $track->track_number = $parsedTags['track_number']; $track->released_at = $releasedAt; $track->description = $parsedTags['comments']; diff --git a/app/Genre.php b/app/Genre.php index f9af2769..a09ec786 100644 --- a/app/Genre.php +++ b/app/Genre.php @@ -2,12 +2,14 @@ namespace App; -use Illuminate\Database\Eloquent\Model; use App\Traits\SlugTrait; +use Illuminate\Database\Eloquent\Model; class Genre extends Model { protected $table = 'genres'; + protected $fillable = ['name', 'slug']; + public $timestamps = false; use SlugTrait; } \ No newline at end of file