Skip to content

Commit

Permalink
#100: Progress commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Josef Citrine committed Feb 21, 2017
1 parent 3526403 commit 8841bd2
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 15 deletions.
85 changes: 70 additions & 15 deletions app/Console/Commands/ImportPonify.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,6 @@ public function handle()

$getId3 = new getID3;

// Enable file hashing
$getId3->option_md5_data = true;
$getId3->option_md5_data_source = true;

// all tags read by getID3, including the cover art
$allTags = $getId3->analyze($file->getPathname());

Expand All @@ -153,7 +149,11 @@ public function handle()
// normalized tags used by Pony.fm
$parsedTags = [];

list($parsedTags, $rawTags) = $this->parseTags($file);
list($parsedTags, $rawTags) = $this->parseTags($file, $allTags);

$imageFilename = $file->getFilename() . ".tags.txt";
$imageFilePath = "$tmpPath/" . $imageFilename;
File::put($imageFilePath, print_r($allTags, true));

//==========================================================================================================
// Check to see if we have this track already, if so, compare hashes of the two files
Expand Down Expand Up @@ -194,18 +194,46 @@ public function handle()

if ($existingFile === null) {
// Can't find a matching format
// Check to see if we have a better quality file
// Before we do anything, was this from MLPMA?
$mlpmaTrack = DB::table('mlpma_tracks')->where('track_id', '=', $existingTrack->id)->first();

if (!is_null($mlpmaTrack)) {
// This was from the archive
// See if we have a higher quality source file
if (Track::$Formats[$importFormat]['is_lossless']) {
// Source is lossless, is the existing track lossy?
if ($existingFile->isMasterLossy()) {
// Cool! Let's replace it
$this->comment('Replacing (' . $existingTrack->id . ') ' . $existingTrack->title);

$this->replaceTrack($file, $existingTrack, $artist, $allTags['mime_type']);

continue;
}
}
}

continue;

} else {
$this->comment("Found existing file");

// Found a matching format, are they the same?
// Before we check it, see if it came from MLPMA
// We're only replacing tracks with the same format if they're archived
$getId3_source = new getID3;

$getId3_source->option_md5_data = true;
$getId3_source->option_md5_data_source = true;

$sourceWithMd5 = $getId3_source->analyze($file->getPathname());

$getId3_existing = new getID3;
$getId3_existing->option_md5_data = true;
$getId3_existing->option_md5_data_source = true;
$existingFileTags = $getId3->analyze($existingFile->getFile());
$existingFileTags = $getId3_existing->analyze($existingFile->getFile());

$importHash = array_key_exists('md5_data_source', $allTags) ? $allTags['md5_data_source'] : $allTags['md5_data'];
$importHash = array_key_exists('md5_data_source', $sourceWithMd5) ? $sourceWithMd5['md5_data_source'] : $sourceWithMd5['md5_data'];
$targetHash = array_key_exists('md5_data_source', $existingFileTags) ? $existingFileTags['md5_data_source'] : $existingFileTags['md5_data'];

$this->info("Archive hash: " . $importHash);
Expand All @@ -218,8 +246,12 @@ public function handle()
$this->comment("Versions are the same. Skipping...\n");
continue;
} else {
// Audio is different. Replace if it came from MLPMA
// TODO: Replace file
// Audio is different, let's replace it
$this->comment('Replacing (' . $existingTrack->id . ') ' . $existingTrack->title);

$this->replaceTrack($file, $existingTrack, $artist, $allTags['mime_type']);

continue;
}
}
} else {
Expand Down Expand Up @@ -253,9 +285,15 @@ public function handle()

$this->comment('Extracting cover art!');
$coverId = null;
$image = null;

if (array_key_exists('comments', $allTags) && array_key_exists('picture', $allTags['comments'])) {
$image = $allTags['comments']['picture'][0];
} else if (array_key_exists('id3v2', $allTags) && array_key_exists('APIC', $allTags['id3v2'])) {
$image = $allTags['id3v2']['APIC'][0];
}

if ($image !== null) {
if ($image['image_mime'] === 'image/png') {
$extension = 'png';
} else {
Expand Down Expand Up @@ -300,6 +338,7 @@ public function handle()
$this->error(json_encode($result->getValidator()->messages()->getMessages(), JSON_PRETTY_PRINT));
} else {
$track = Track::find($result->getResponse()['id']);
$track->cover_id = $coverId;
$track->license_id = 2;
$track->save();
}
Expand All @@ -324,17 +363,13 @@ protected function getFormat($extension) {
return null;
}

public function parseTags($file)
public function parseTags($file, $allTags)
{
$audioCodec = $file->getExtension();

//==========================================================================================================
// Extract the original tags.
//==========================================================================================================
$getId3 = new getID3;

// all tags read by getID3, including the cover art
$allTags = $getId3->analyze($file->getPathname());

// $rawTags => tags specific to a file format (ID3 or Atom), pre-normalization but with cover art removed
// $parsedTags => normalized tags used by Pony.fm
Expand Down Expand Up @@ -376,6 +411,8 @@ protected function getId3Tags($rawTags)
{
if (array_key_exists('tags', $rawTags) && array_key_exists('id3v2', $rawTags['tags'])) {
$tags = $rawTags['tags']['id3v2'];
} elseif (array_key_exists('id3v2', $rawTags)) {
$tags = $rawTags['tags']['id3v2'];
} elseif (array_key_exists('tags', $rawTags) && array_key_exists('id3v1', $rawTags['tags'])) {
$tags = $rawTags['tags']['id3v1'];
} else {
Expand Down Expand Up @@ -555,4 +592,22 @@ protected function parseDateString(string $dateString)
}
}
}

protected function replaceTrack($toBeUploaded, $targetTrack, $artist, $mime) {
Auth::loginUsingId($artist->id);

$trackFile = new UploadedFile($toBeUploaded->getPathname(), $toBeUploaded->getFilename(), $mime, null, null, true);

$upload = new UploadTrackCommand(true, false, null, false, $targetTrack->getNextVersion(), $targetTrack);
$upload->_file = $trackFile;
$result = $upload->execute();

if ($result->didFail()) {
$this->error(json_encode($result->getValidator()->messages()->getMessages(), JSON_PRETTY_PRINT));
} else {
$track = Track::find($result->getResponse()['id']);
$track->license_id = 2;
$track->save();
}
}
}
48 changes: 48 additions & 0 deletions database/migrations/2017_02_08_233324_create_ponify_tracks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePonifyTracks extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('ponify_tracks', function(Blueprint $table)
{
$table->increments('id');
$table->integer('track_id')->unsigned()->index();
$table->string('path')->index();
$table->string('filename')->index();
$table->string('extension')->index();
$table->dateTime('imported_at');
$table->text('parsed_tags');
$table->text('raw_tags');
});

Schema::table('ponify_tracks', function(Blueprint $table)
{
$table->foreign('track_id')->references('id')->on('tracks')->onUpdate('RESTRICT')->onDelete('RESTRICT');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('ponify_tracks', function(Blueprint $table)
{
$table->dropForeign('ponify_tracks_track_id_foreign');
});

Schema::drop('ponify_tracks');
}
}

0 comments on commit 8841bd2

Please sign in to comment.