-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcli_downloader.php
More file actions
95 lines (81 loc) · 2.07 KB
/
cli_downloader.php
File metadata and controls
95 lines (81 loc) · 2.07 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
92
93
94
95
<?php
require '_autoload.php';
function download_it($img_url, $output_file, $options='') {
$dir = dirname($output_file) . '\\';
//exec("mkdir \"$dir\"");
exec("wget -t 0 --retry-connrefused --no-check-certificate -O \"$output_file\" \"$img_url\" $options");
}
interface ADownloader {
public function display();
public function download();
}
class DConfig {
private static $v = null;
public static function __callStatic($name, $arguments) {
if (self::$v === null) {
self::$v = parse_ini_file(__DIR__.'/downloaders/config.ini');
}
return isset(self::$v[$name]) ? self::$v[$name] : '';
}
}
class MainProgram {
private $downloaders;
private $active;
public function __construct() {
// loop through classes
$scan = scandir('downloaders');
$this->downloaders = array();
foreach ($scan as $entry) {
if ($pos = strpos($entry, '_Downloader.php')) {
$classname = substr($entry, 0, $pos) . '_Downloader';
include 'downloaders/'.$entry;
$this->downloaders[] = new $classname;
}
}
}
private function print_menu() {
$text = '';
$i = 1;
foreach ($this->downloaders as $downloader) {
$text .= "[$i] {$downloader->display()}\n";
$i++;
}
$text .= "[q]/[exit] Close\n\n";
$text .= 'Choose: ';
echo $text;
}
private function interpret_input($input) {
echo "You chose [{$input}]\n\n";
$downloader = $this->search_downloader($input);
if ($input === 'exit' || $input === 'q') {
$this->active = false;
echo "Goodbye\n";
return;
} else if ($downloader) {
echo "{$downloader->display()}\n==========================================\n\n";
$downloader->download();
} else {
echo "UNRECOGNIZED\n";
}
echo "Press Enter...";
fgets(STDIN);
}
private function search_downloader($input) {
$key = $input - 1;
if (isset($this->downloaders[$key])) {
return $this->downloaders[$key];
} else {
return null;
}
}
public function run() {
$this->active = true;
while ($this->active) {
$this->print_menu();
$input = fgets(STDIN);
$this->interpret_input(trim($input));
}
}
}
$p = new MainProgram();
$p->run();