-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.php
56 lines (45 loc) · 1.73 KB
/
main.php
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
<?php
if (php_sapi_name() != 'cli') {
printf("Should be run from CLI.\n");
exit(0);
}
if (is_dir(__DIR__ . '/vendor')) {
// for development
require_once __DIR__ . '/vendor/autoload.php';
} else {
// for global installation in ~/.composer
require_once __DIR__ . '/../../autoload.php';
}
array_shift($argv); // take out the first param (script name)
$updater = new \AlixBa\Addic7edSubtitles\Jobs\ShowsUpdater();
$finder = new \AlixBa\Addic7edSubtitles\Jobs\SubtitlesFinder();
$io = new \AlixBa\Addic7edSubtitles\Helpers\IO;
$options = array_filter($argv, function ($arg) { return substr($arg, 0, 2) === '--'; });
$files = array_diff($argv, $options);
$exceptions = [];
$download = !in_array("--no-download", $options);
$update = in_array("--update", $options);
if ($update) {
$updater->updateShows();
}
while (!is_null($filename = array_shift($files))) {
printf("Running addic7ed-php for [%s].\n", $filename);
$extensions = ['mp4', 'mkv', 'avi'];
$file = pathinfo($filename);
$basename = in_array($file['extension'], $extensions) ? $file['filename'] : $basename = $file['basename'];
try {
$srt = $finder->findSubtitle($basename, $download);
if (!is_null($srt)) {
$io->saveSubtitle($file['dirname'], $basename, $srt);
}
} catch (\Exception $e) {
if (!in_array($filename, $exceptions)) {
printf("Error while running addic7ed-php for [%s]. Will retry.\n", $basename);
$exceptions[] = $filename;
array_push($files, $filename);
} else {
printf("Error while running addic7ed-php for [%s].\n", $basename);
}
}
printf("\n");
}