2015-09-04 18:59:18 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
|
|
use Illuminate\Support\Facades\File;
|
2015-09-05 15:39:48 +02:00
|
|
|
use Entities\Album;
|
2015-09-04 18:59:18 +02:00
|
|
|
use Entities\Image;
|
|
|
|
use Entities\User;
|
2015-09-05 15:39:48 +02:00
|
|
|
use Entities\ShowSong;
|
2015-09-06 17:33:12 +02:00
|
|
|
use Entities\Track;
|
2015-09-05 15:39:48 +02:00
|
|
|
use Entities\TrackType;
|
2015-09-07 13:50:35 +02:00
|
|
|
use Commands\UploadTrackCommand;
|
2015-09-04 18:59:18 +02:00
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
|
|
|
use Carbon\Carbon;
|
|
|
|
|
|
|
|
require_once(app_path() . '/library/getid3/getid3/getid3.php');
|
|
|
|
|
|
|
|
|
|
|
|
class ImportMLPMA extends Command {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command name.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-09-07 13:50:35 +02:00
|
|
|
protected $name = 'mlpma:import';
|
2015-09-04 18:59:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description = 'Imports the MLP Music Archive';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* File extensions to ignore when importing the archive.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $ignoredExtensions = ['db', 'jpg', 'png'];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new command instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function fire()
|
|
|
|
{
|
|
|
|
$mlpmaPath = Config::get('app.files_directory').'mlpma';
|
|
|
|
$tmpPath = Config::get('app.files_directory').'tmp';
|
|
|
|
|
|
|
|
if (!File::exists($tmpPath)) {
|
|
|
|
File::makeDirectory($tmpPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->comment('Enumerating MLP Music Archive source files...');
|
|
|
|
$files = File::allFiles($mlpmaPath);
|
|
|
|
$this->info(sizeof($files).' files found!');
|
|
|
|
|
|
|
|
$this->comment('Enumerating artists...');
|
|
|
|
$artists = File::directories($mlpmaPath);
|
|
|
|
$this->info(sizeof($artists).' artists found!');
|
|
|
|
|
|
|
|
$this->comment('Importing tracks...'.PHP_EOL);
|
|
|
|
|
2015-09-05 15:39:48 +02:00
|
|
|
$totalFiles = sizeof($files);
|
|
|
|
$currentFile = 0;
|
|
|
|
|
2015-09-04 18:59:18 +02:00
|
|
|
foreach($files as $file) {
|
2015-09-05 15:39:48 +02:00
|
|
|
$currentFile++;
|
|
|
|
$this->comment('['.$currentFile.'/'.$totalFiles.'] Importing track ['. $file->getFilename() .']...');
|
2015-09-04 18:59:18 +02:00
|
|
|
|
|
|
|
if (in_array($file->getExtension(), $this->ignoredExtensions)) {
|
|
|
|
$this->comment('This is not an audio file! Skipping...'.PHP_EOL);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-07 13:50:35 +02:00
|
|
|
// Has this track already been imported?
|
|
|
|
$importedTrack = DB::table('mlpma_tracks')
|
|
|
|
->where('filename', '=', $file->getFilename())
|
|
|
|
->first();
|
|
|
|
|
|
|
|
if ($importedTrack) {
|
|
|
|
$this->comment('This track has already been imported! Skipping...' . PHP_EOL);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-04 18:59:18 +02:00
|
|
|
//==========================================================================================================
|
|
|
|
// Extract the original tags.
|
|
|
|
//==========================================================================================================
|
|
|
|
$getId3 = new getID3;
|
|
|
|
|
2015-09-07 13:50:35 +02:00
|
|
|
// all tags read by getID3, including the cover art
|
|
|
|
$allTags = $getId3->analyze($file->getPathname());
|
|
|
|
|
|
|
|
// tags specific to a file format (ID3 or Atom), pre-normalization but with cover art removed
|
|
|
|
$rawTags = [];
|
|
|
|
|
|
|
|
// normalized tags used by Pony.fm
|
2015-09-04 18:59:18 +02:00
|
|
|
$parsedTags = [];
|
2015-09-07 13:50:35 +02:00
|
|
|
|
2015-09-04 18:59:18 +02:00
|
|
|
if ($file->getExtension() === 'mp3') {
|
2015-09-07 13:50:35 +02:00
|
|
|
list($parsedTags, $rawTags) = $this->getId3Tags($allTags);
|
2015-09-04 18:59:18 +02:00
|
|
|
|
|
|
|
} else if ($file->getExtension() === 'm4a') {
|
2015-09-07 13:50:35 +02:00
|
|
|
list($parsedTags, $rawTags) = $this->getAtomTags($allTags);
|
2015-09-04 18:59:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//==========================================================================================================
|
|
|
|
// Determine the release date.
|
|
|
|
//==========================================================================================================
|
|
|
|
$modifiedDate = Carbon::createFromTimeStampUTC(File::lastModified($file->getPathname()));
|
|
|
|
$taggedYear = $parsedTags['year'];
|
|
|
|
|
|
|
|
$this->info('Modification year: '.$modifiedDate->year);
|
|
|
|
$this->info('Tagged year: '.$taggedYear);
|
|
|
|
|
|
|
|
if ($taggedYear !== null && $modifiedDate->year === $taggedYear) {
|
2015-09-06 17:33:12 +02:00
|
|
|
$releasedAt = $modifiedDate;
|
2015-09-04 18:59:18 +02:00
|
|
|
|
|
|
|
} else if ($taggedYear !== null && $modifiedDate->year !== $taggedYear) {
|
2015-09-05 15:39:48 +02:00
|
|
|
$this->error('Release years don\'t match! Using the tagged year...');
|
2015-09-06 17:33:12 +02:00
|
|
|
$releasedAt = Carbon::create($taggedYear);
|
2015-09-04 18:59:18 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// $taggedYear is null
|
|
|
|
$this->error('This track isn\'t tagged with its release year! Using the track\'s last modified date...');
|
2015-09-06 17:33:12 +02:00
|
|
|
$releasedAt = $modifiedDate;
|
2015-09-04 18:59:18 +02:00
|
|
|
}
|
|
|
|
|
2015-09-07 13:50:35 +02:00
|
|
|
// This is later used by the classification/publishing script to determine the publication date.
|
|
|
|
$parsedTags['released_at'] = $releasedAt;
|
|
|
|
|
2015-09-04 18:59:18 +02:00
|
|
|
//==========================================================================================================
|
|
|
|
// Does this track have vocals?
|
|
|
|
//==========================================================================================================
|
2015-09-06 17:33:12 +02:00
|
|
|
$isVocal = $parsedTags['lyrics'] !== null;
|
2015-09-04 18:59:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
//==========================================================================================================
|
|
|
|
// Determine which artist account this file belongs to using the containing directory.
|
|
|
|
//==========================================================================================================
|
|
|
|
$this->info('Path to file: '.$file->getRelativePath());
|
|
|
|
$path_components = explode(DIRECTORY_SEPARATOR, $file->getRelativePath());
|
|
|
|
$artist_name = $path_components[0];
|
|
|
|
$album_name = array_key_exists(1, $path_components) ? $path_components[1] : null;
|
|
|
|
|
|
|
|
$this->info('Artist: '.$artist_name);
|
|
|
|
$this->info('Album: '.$album_name);
|
|
|
|
|
|
|
|
$artist = User::where('display_name', '=', $artist_name)->first();
|
|
|
|
|
|
|
|
if (!$artist) {
|
|
|
|
$artist = new User;
|
|
|
|
$artist->display_name = $artist_name;
|
|
|
|
$artist->email = null;
|
|
|
|
$artist->is_archived = true;
|
|
|
|
|
|
|
|
$artist->slug = Str::slug($artist_name);
|
|
|
|
|
|
|
|
$slugExists = User::where('slug', '=', $artist->slug)->first();
|
|
|
|
if ($slugExists) {
|
|
|
|
$this->error('Horsefeathers! The slug '.$artist->slug.' is already taken!');
|
|
|
|
$artist->slug = $artist->slug.'-'.Str::random(4);
|
|
|
|
}
|
|
|
|
|
|
|
|
$artist = $artist->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================================================
|
|
|
|
// Extract the cover art, if any exists.
|
|
|
|
//==========================================================================================================
|
2015-09-06 17:33:12 +02:00
|
|
|
$coverId = null;
|
2015-09-07 13:50:35 +02:00
|
|
|
if (array_key_exists('comments', $allTags) && array_key_exists('picture', $allTags['comments'])) {
|
|
|
|
$image = $allTags['comments']['picture'][0];
|
2015-09-04 18:59:18 +02:00
|
|
|
|
|
|
|
if ($image['image_mime'] === 'image/png') {
|
|
|
|
$extension = 'png';
|
|
|
|
|
|
|
|
} else if ($image['image_mime'] === 'image/jpeg') {
|
|
|
|
$extension = 'jpg';
|
|
|
|
|
|
|
|
} else if ($image['image_mime'] === 'image/gif') {
|
|
|
|
$extension = 'gif';
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$this->error('Unknown cover art format!');
|
|
|
|
}
|
|
|
|
|
|
|
|
// write temporary image file
|
|
|
|
$imageFilename = $file->getFilename() . ".cover.$extension";
|
|
|
|
$imageFilePath = "$tmpPath/".$imageFilename;
|
|
|
|
File::put($imageFilePath, $image['data']);
|
|
|
|
|
|
|
|
|
|
|
|
$imageFile = new UploadedFile($imageFilePath, $imageFilename, $image['image_mime']);
|
|
|
|
|
2015-09-05 15:39:48 +02:00
|
|
|
$cover = Image::upload($imageFile, $artist);
|
2015-09-06 17:33:12 +02:00
|
|
|
$coverId = $cover->id;
|
2015-09-04 18:59:18 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
$this->error('No cover art found!');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//==========================================================================================================
|
|
|
|
// Is this part of an album?
|
|
|
|
//==========================================================================================================
|
2015-09-06 17:33:12 +02:00
|
|
|
// Find the album if it exists and create it if it doesn't.
|
|
|
|
$albumId = null;
|
|
|
|
$albumName = $parsedTags['album'];
|
2015-09-04 18:59:18 +02:00
|
|
|
|
2015-09-06 17:33:12 +02:00
|
|
|
if ($albumName !== null) {
|
2015-09-05 15:39:48 +02:00
|
|
|
$album = Album::where('user_id', '=', $artist->id)
|
2015-09-06 17:33:12 +02:00
|
|
|
->where('title', '=', $albumName)
|
2015-09-05 15:39:48 +02:00
|
|
|
->first();
|
2015-09-04 18:59:18 +02:00
|
|
|
|
2015-09-05 15:39:48 +02:00
|
|
|
if (!$album) {
|
|
|
|
$album = new Album;
|
|
|
|
|
2015-09-06 17:33:12 +02:00
|
|
|
$album->title = $albumName;
|
2015-09-05 15:39:48 +02:00
|
|
|
$album->user_id = $artist->id;
|
2015-09-06 17:33:12 +02:00
|
|
|
$album->cover_id = $coverId;
|
2015-09-05 15:39:48 +02:00
|
|
|
|
|
|
|
$album->save();
|
|
|
|
}
|
2015-09-06 17:33:12 +02:00
|
|
|
|
|
|
|
$albumId = $album->id;
|
2015-09-05 15:39:48 +02:00
|
|
|
}
|
2015-09-04 18:59:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
//==========================================================================================================
|
|
|
|
// Save this track.
|
|
|
|
//==========================================================================================================
|
2015-09-06 17:33:12 +02:00
|
|
|
$title = $parsedTags['title'];
|
2015-09-07 13:50:35 +02:00
|
|
|
//
|
|
|
|
// $track = Track::where('user_id', '=', $artist->id)
|
|
|
|
// ->where('title', '=', $title)
|
|
|
|
// ->first();
|
2015-09-06 17:33:12 +02:00
|
|
|
|
2015-09-07 13:50:35 +02:00
|
|
|
// "upload" the track to Pony.fm
|
|
|
|
Auth::loginUsingId($artist->id);
|
|
|
|
|
|
|
|
$trackFile = new UploadedFile($file->getPathname(), $file->getFilename(), $allTags['mime_type']);
|
|
|
|
Input::instance()->files->add(['track' => $trackFile]);
|
2015-09-04 18:59:18 +02:00
|
|
|
|
2015-09-07 13:50:35 +02:00
|
|
|
$upload = new UploadTrackCommand(true);
|
|
|
|
$result = $upload->execute();
|
|
|
|
|
|
|
|
// var_dump(null);
|
|
|
|
|
|
|
|
if ($result->didFail()) {
|
|
|
|
$this->error(json_encode($result->getValidator()->messages()->getMessages(), JSON_PRETTY_PRINT));
|
|
|
|
} else {
|
|
|
|
DB::table('mlpma_tracks')
|
|
|
|
->insert([
|
|
|
|
'track_id' => $result['id'],
|
|
|
|
'path' => $file->getRelativePath(),
|
|
|
|
'filename' => $file->getFilename(),
|
|
|
|
'extension' => $file->getExtension(),
|
|
|
|
'imported_at' => Carbon::now(),
|
|
|
|
'parsed_tags' => json_encode($parsedTags),
|
|
|
|
'raw_tags' => json_encode($rawTags),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$track = Track::find($result['id']);
|
|
|
|
var_dump($track);
|
2015-09-05 15:39:48 +02:00
|
|
|
|
2015-09-06 17:33:12 +02:00
|
|
|
$track->title = $parsedTags['title'];
|
|
|
|
$track->cover_id = $coverId;
|
|
|
|
$track->album_id = $albumId;
|
2015-09-07 13:50:35 +02:00
|
|
|
$track->track_number = $parsedTags['track_number'];
|
2015-09-06 17:33:12 +02:00
|
|
|
$track->released_at = $releasedAt;
|
|
|
|
$track->is_vocal = $isVocal;
|
|
|
|
$track->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
echo PHP_EOL.PHP_EOL;
|
2015-09-04 18:59:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the console command arguments.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getArguments()
|
|
|
|
{
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the console command options.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getOptions()
|
|
|
|
{
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $rawTags
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getId3Tags($rawTags) {
|
|
|
|
$tags = $rawTags['tags']['id3v2'];
|
2015-09-07 13:50:35 +02:00
|
|
|
$comment = null;
|
|
|
|
|
|
|
|
if (isset($tags['comment'])) {
|
|
|
|
// The "comment" tag comes in with a badly encoded string index
|
|
|
|
// so its array key has to be used implicitly.
|
|
|
|
$key = array_keys($tags['comment'])[0];
|
|
|
|
|
|
|
|
// The comment may have a null byte at the end. trim() removes it.
|
|
|
|
$comment = trim($tags['comment'][$key]);
|
|
|
|
|
|
|
|
// Replace the malformed comment with the "fixed" one.
|
|
|
|
unset($tags['comment'][$key]);
|
|
|
|
$tags['comment'][0] = $comment;
|
|
|
|
}
|
2015-09-04 18:59:18 +02:00
|
|
|
|
|
|
|
return [
|
2015-09-07 13:50:35 +02:00
|
|
|
[
|
|
|
|
'title' => $tags['title'][0],
|
|
|
|
'artist' => $tags['artist'][0],
|
|
|
|
'band' => isset($tags['band']) ? $tags['band'][0] : null,
|
|
|
|
'genre' => isset($tags['genre']) ? $tags['genre'][0] : null,
|
|
|
|
'track_number' => isset($tags['track_number']) ? $tags['track_number'][0] : null,
|
|
|
|
'album' => isset($tags['album']) ? $tags['album'][0] : null,
|
|
|
|
'year' => isset($tags['year']) ? (int) $tags['year'][0] : null,
|
|
|
|
'comments' => $comment,
|
|
|
|
'lyrics' => isset($tags['unsynchronised_lyric']) ? $tags['unsynchronised_lyric'][0] : null,
|
|
|
|
],
|
|
|
|
$tags];
|
2015-09-04 18:59:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $rawTags
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getAtomTags($rawTags) {
|
2015-09-06 17:33:12 +02:00
|
|
|
$tags = $rawTags['tags']['quicktime'];
|
|
|
|
|
|
|
|
$trackNumber = null;
|
|
|
|
if (isset($tags['track_number'])) {
|
|
|
|
$trackNumberComponents = explode('/', $tags['track_number'][0]);
|
|
|
|
$trackNumber = $trackNumberComponents[0];
|
|
|
|
}
|
2015-09-04 18:59:18 +02:00
|
|
|
|
|
|
|
return [
|
2015-09-07 13:50:35 +02:00
|
|
|
[
|
|
|
|
'title' => $tags['title'][0],
|
|
|
|
'artist' => $tags['artist'][0],
|
|
|
|
'band' => isset($tags['band']) ? $tags['band'][0] : null,
|
|
|
|
'album_artist' => isset($tags['album_artist']) ? $tags['album_artist'][0] : null,
|
|
|
|
'genre' => isset($tags['genre']) ? $tags['genre'][0] : null,
|
|
|
|
'track_number' => $trackNumber,
|
|
|
|
'album' => isset($tags['album']) ? $tags['album'][0] : null,
|
|
|
|
'year' => isset($tags['year']) ? (int) $tags['year'][0] : null,
|
|
|
|
'comments' => isset($tags['comments']) ? $tags['comments'][0] : null,
|
|
|
|
'lyrics' => isset($tags['lyrics']) ? $tags['lyrics'][0] : null,
|
|
|
|
],
|
|
|
|
$tags];
|
2015-09-04 18:59:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|