Skip to content

Commit

Permalink
Add media discovery commands 🤔 Probably wont keep this. Just trying t…
Browse files Browse the repository at this point in the history
…o fix my library.
  • Loading branch information
austinkregel committed May 5, 2024
1 parent 1e55a68 commit 0e792e9
Show file tree
Hide file tree
Showing 2 changed files with 276 additions and 0 deletions.
203 changes: 203 additions & 0 deletions app/Console/Commands/LibraryMetaDataScan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
<?php

declare(strict_types=1);

namespace App\Console\Commands;

use App\Jobs\BuildMetaDataFile;
use Closure;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;

class LibraryMetaDataScan extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:library-meta-data-scan';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';

/**
* Execute the console command.
*/
public function handle()
{
// ...$filesystem->directories('/media/Downloads'),
$supportedTypes = [
// Audio
'mp3',
'flac',
'm3u',
'm4a',
'ogg',
// ebooks
'pdf',
'epub',
//video
'mp4',
'mkv',
'm4v',
'm2ts',
'part',
'mkv.part',
// sub titles
'ts',
'srt',
// metadata
'cue',
'xml',
// playlist
'm3u8',
// db
'json',
'sqlite',
'sqlite3',
'ini',

// checksums
'sfv',
'checksum',

// audiotracks
'mka',
];
$blacklistedTypes = [
'gif',
'png',
'jpg',
'jpeg',
'nfo',
'gif',
'mobileconfig',
'xci',
'html',
'log',
'svg',
'txt',
'url',
'exe',
'db',
'metathumb',
'iso',
'lnk',
'md',
'About_album',
'pls',
'accurip',
'mid',
'auCDtect',
'opus',
'hosts',
'last_time',
'sbk',
'yml',
'db-shm',
'db-wal',
'zip',
'yaml',
'pem',
'txz',
'1',
'mbp',
'env',
'config',
'key',
'js',
'INFO',
'css',
'psd',
'pspimage',
'sqlite3-shm',
'sqlite3-wal',
'gz',
'phar',
'LICENSE',
'lock',
'dist',
'web',
'php',
'composer',
'console',
'sh',
'jsonlint',
'php-cs-fixer',
'php-parse',
'phpstan',
'phpunit',
'satis',
'validate-json',
'var-dump-server',
'Dockerfile',
'conf',
'ico',
'map',
'eot',
'ttf',
'woff',
'woff2',
'twig',
'meta',
'PORTING_INFO',
'compile',
'bat',
'stub',
'scss',
'rst',
'diff',
'phpt',
'template',
'y',
'asc',
'neon',
'xsd',
'tpl',
'base64',
'xlf',
'CHANGELOG',
'secret',
'public',
'planet',
'port',
'peer',
];

$this->recursivelyFindFiles('/media/Shows', $supportedTypes, $blacklistedTypes, function (\SplFileInfo $file, $err, $out) {
});
}

protected function recursivelyFindFiles(string $directory, array &$supportedTypes, array &$blacklistedTypes, Closure $callback)
{
$this->warn(' Found: '.$directory);
$files = (new Filesystem)->files($directory);

// Build a list of supported file types.
// Interactively when
/** @var \SplFileInfo $file */
foreach ($files as $file) {
$name = $file->getBasename();
$split = explode('.', $name);
$extension = end($split);
$path = $file->getPathname();

if (in_array($extension, $blacklistedTypes)) {
continue;
}

dispatch(new BuildMetaDataFile($path));
}

$directories = (new Filesystem)->directories($directory);

foreach ($directories as $directory) {
$this->recursivelyFindFiles($directory, $supportedTypes, $blacklistedTypes, $callback);
}
}
}
73 changes: 73 additions & 0 deletions app/Jobs/BuildMetaDataFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Symfony\Component\Process\Process;

class BuildMetaDataFile implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

/**
* Create a new job instance.
*/
public function __construct(
protected string $path
) {
//
}

/**
* Execute the job.
*/
public function handle(): void
{
$process = new Process([
'/usr/bin/mediainfo',
'--Output=JSON',
'--Full',
$this->path,
]);
$process->run(function ($e, $o) {
$file = new \SplFileInfo($this->path);
// Will only be executed on supported types.
$name = $file->getBasename();
$split = explode('.', $name);
$extension = end($split);
$path = $file->getPathname();

$data = json_decode($o, true);

$data['name'] = $name;
$data['path'] = $path;
// bytes
$data['size'] = $file->getSize();
$data['type'] = $file->getType();

if (! file_exists(storage_path('meta/'.$extension))) {
(new Filesystem)
->makeDirectory(storage_path('meta/'.$extension), 0755, true);
}

$metadataNewPath = storage_path('meta/'.$extension.'/'.md5($path).'.json');
$metadataPath = storage_path('meta/'.md5($path).'.json');

if (file_exists($metadataPath)) {
// info('Danger, file conflict in md5 hash', $data);
(new Filesystem)->move($metadataPath, $metadataNewPath);

return;
}

file_put_contents($metadataNewPath, json_encode($data, JSON_PRETTY_PRINT));
});
}
}

0 comments on commit 0e792e9

Please sign in to comment.