-
Notifications
You must be signed in to change notification settings - Fork 3
/
clusterer
executable file
·93 lines (77 loc) · 3.48 KB
/
clusterer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env php
<?php
declare(strict_types=1);
ini_set('memory_limit', -1);
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\SingleCommandApplication;
use Symfony\Component\Console\Style\SymfonyStyle;
require_once __DIR__.DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'autoload.php';
require_once __DIR__.DIRECTORY_SEPARATOR.'util.php';
(new SingleCommandApplication())
->setVersion('0.1.0')
->addOption('metadata-file', null, InputOption::VALUE_REQUIRED, 'Path to the metadata.json file', '.'.DIRECTORY_SEPARATOR.'metadata.json')
->addOption('downloads-dir', null, InputOption::VALUE_REQUIRED, 'Path where songs are downloaded', '.'.DIRECTORY_SEPARATOR.'downloads')
->addOption('clusters-dir', null, InputOption::VALUE_REQUIRED, 'Path where clusters will be saved', '.'.DIRECTORY_SEPARATOR.'clusters')
->setCode(function (InputInterface $input, OutputInterface $output) {
$io = new SymfonyStyle($input, $output);
$io->title('YouTube Music Upload Cluster Generator');
$downloadsDir = $input->getOption('downloads-dir');
$clustersDir = rtrim($input->getOption('clusters-dir'), DIRECTORY_SEPARATOR);
$data = json_decode(file_get_contents($input->getOption('metadata-file')), true);
$count = \count($data);
if (false === $data || 0 === $count) {
$io->error('Empty or not found metadata-file');
return Command::INVALID;
}
$io->text(sprintf('Will cluster songs from %s to %s:', $downloadsDir, $clustersDir));
$progressBar = new ProgressBar($output);
$progressBar->setFormat('verbose');
$progressBar->start($count);
$sanitize = function (string $input): string {
return str_replace(DIRECTORY_SEPARATOR, '-', $input);
};
foreach ($data as $song) {
try {
$path = getDownloadedSong($input->getOption('downloads-dir'), $song['id']);
$ext = pathinfo($path, flags: \PATHINFO_EXTENSION);
if ($song['album']) {
@mkdir($clustersDir.DIRECTORY_SEPARATOR.$sanitize($song['album']), recursive: true);
copy(
$path,
$clustersDir
.DIRECTORY_SEPARATOR
.$sanitize($song['album'])
.DIRECTORY_SEPARATOR
.$sanitize($song['title'])
.'.'
.$ext
);
} else {
copy(
$path,
$clustersDir
.DIRECTORY_SEPARATOR
.$sanitize($song['artist'])
.' - '
.$sanitize($song['title'])
.'.'
.$ext
);
}
@unlink($path);
$progressBar->advance();
} catch (\Throwable $e) {
// $progressBar->advance();
throw $e;
}
}
$progressBar->finish();
$io->newLine(2);
$io->success('done');
return Command::SUCCESS;
})
->run();