-
Notifications
You must be signed in to change notification settings - Fork 3
/
playlist-extractor
executable file
·67 lines (50 loc) · 2.32 KB
/
playlist-extractor
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
#!/usr/bin/env php
<?php
declare(strict_types=1);
ini_set('memory_limit', -1);
use Symfony\Component\DomCrawler\Crawler;
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';
(new SingleCommandApplication())
->setVersion('0.1.0')
->addOption('html-file', null, InputOption::VALUE_REQUIRED, 'Path to the HTML file', '.'.DIRECTORY_SEPARATOR.'YouTube Music.html')
->addOption('playlist-file', null, InputOption::VALUE_REQUIRED, 'Path to the playlist.txt file', '.'.DIRECTORY_SEPARATOR.'playlist.txt')
->setCode(function (InputInterface $input, OutputInterface $output) {
$io = new SymfonyStyle($input, $output);
$io->title('YouTube Music Upload Playlist Extractor');
$htmlFile = $input->getOption('html-file');
$playlistFile = $input->getOption('playlist-file');
if (!file_exists($htmlFile)) {
$io->error('html-file not found');
return Command::INVALID;
}
$io->text(sprintf('Will extract %s from %s:', $playlistFile, $htmlFile));
$crawler = (new Crawler(file_get_contents($htmlFile)))->filter('ytmusic-responsive-list-item-renderer');
$progressBar = new ProgressBar($output);
$progressBar->setFormat('verbose');
$progressBar->start($crawler->count());
$data = $crawler->each(function (Crawler $node) use ($progressBar) {
if (!$node || !\count($node->filter('.title a'))) {
$progressBar->advance();
return;
}
$progressBar->advance();
return sprintf(
'%s by %s',
$node->filter('.title a')->text(),
$node->filter('.secondary-flex-columns')->children()->eq(0)->text()
);
});
file_put_contents($playlistFile, implode(PHP_EOL, $data));
$progressBar->finish();
$io->newLine();
$io->success('done');
return Command::SUCCESS;
})
->run();