forked from Convertio/convertio-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.php
More file actions
91 lines (71 loc) · 2.21 KB
/
cli.php
File metadata and controls
91 lines (71 loc) · 2.21 KB
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
<?php
if (PHP_SAPI != 'cli') {
die('This is command line utility only');
}
require_once "autoload.php";
use Convertio\Convertio;
use ConvertioCLI\CLIOptions;
use ConvertioCLI\FilesOutput;
use ConvertioCLI\Info;
$CLIOptions = new CLIOptions($argv);
if ($CLIOptions->isShowHelp()) {
Info::printHelp();
}
if ($CLIOptions->isShowVersion()) {
Info::printVersion();
}
$APIKey = $CLIOptions->getAPIKey();
$OutFormat = $CLIOptions->getOutputFormat();
$OutDir = $CLIOptions->getOutputDir();
$Files = $CLIOptions->getInputFiles();
if (empty($APIKey)) {
Info::printError("No API Key provided");
}
if (empty($OutFormat)) {
Info::printError("No output format option set");
}
if (empty($Files)) {
Info::printError("No readable input files");
}
$FO = new FilesOutput($Files);
$FO->updateConsole();
$APIs = array();
foreach ($Files as $I => $FN) {
$FO->updateFileStep($FN, 'start');
try {
$APIs[$FN] = new Convertio($APIKey);
$APIs[$FN]->start($FN, $OutFormat);
$FO->updateFileStep($FN, 'convert');
} catch (\Exception $e) {
$FO->fileError($FN, $e->getMessage());
unset($Files[$I]);
if (isset($APIs[$FN])) {
unset($APIs[$FN]);
}
}
}
while (count($APIs) > 0) {
foreach ($APIs as $FN => $API) {
try {
$API->status();
$FO->updateFileStep($FN, $API->step, $API->step_percent);
if ($API->step == 'finish') {
$FO->updateFileStatus($FN, 'Done! [' . $API->result_size . ' Bytes]');
$OutFN = $OutDir . "/" . basename($FN) . "." . strtolower($OutFormat);
$API->download($OutFN);
$API->delete();
$FO->updateFileStatus($FN, 'Done! => ' . $OutFN . ' [' . $API->result_size . ' Bytes]');
unset($APIs[$FN]);
}
} catch (\Exception $e) {
if (!empty($API->result_public_url)) {
$FO->fileError($FN, $e->getMessage() . " [Try download it manually: " . $API->result_public_url . "]");
} else {
$FO->fileError($FN, $e->getMessage());
}
unset($APIs[$FN]);
}
}
usleep(100000);
}
Info::printLine("\nAll Done!\n");