/
bot.php
277 lines (248 loc) · 8.18 KB
/
bot.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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
// Images Processor Bot
// version 0.4 last update Jan-19-2016
// not less PHP 5.2.1 (file_put_contents(), sys_get_temp_dir())
// script's modes
$clear_mode = "clear";
$shedule_mode = "shedule";
$download_mode = "download";
$resize_mode = "resize";
$list_mode = "list";
// for queues
$db_name = "bot_queues.sqlite";
$queue_download = "download";
$queue_failed = "failed";
$queue_resize = "resize";
$queue_done = "done";
// new size for images
$new_width = $new_height = 640;
// messages
$use_messages = PHP_EOL . 'Use: bot (' .
$clear_mode . '|' .
$shedule_mode . '|' .
$download_mode . '|' .
$resize_mode . '|' .
$list_mode . ')' . PHP_EOL;
$use_shedule_message = PHP_EOL . 'Use: bot shedule file_with_links.txt' . PHP_EOL;
$error_open_file = 'Error: failed to open file ';
$error_file_not_exists = 'File not exists';
$urls_processed = ' url(s) processed';
$dl_queue_empty = PHP_EOL . "Download queue is empty" . PHP_EOL;
$files_download = ' file(s) downloaded to ';
$rs_queue_empty = PHP_EOL . "Resize queue is empty" . PHP_EOL;
$files_resized = ' file(s) resized. Use: bot list';
// class for URLs
class urlClass {
public $url;
public $status;
public function __construct($url) {
$this->url = $url;
}
public function getStatus() {
$Headers = @get_headers($this->url);
$this->status = $Headers[0];
}
public function isURLexists() {
$this->getStatus();
return (strpos($this->status, '200') ? true : false);
}
}
// class for images
class imgClass {
private $image;
private $width;
private $height;
private $image_type;
public function load($filename) {
$image_info = getimagesize($filename);
list($this->width, $this->height, $this->image_type) = getimagesize($filename);
switch ($this->image_type) {
case IMAGETYPE_JPEG:
$this->image = imagecreatefromjpeg($filename);
return true;
case IMAGETYPE_PNG:
$this->image = imagecreatefrompng($filename);
return true;
case IMAGETYPE_GIF:
$this->image = imagecreatefromgif($filename);
return true;
default:
return false;
}
}
public function save($filename) {
switch ($this->image_type) {
case IMAGETYPE_JPEG:
imagejpeg($this->image, $filename);
break;
case IMAGETYPE_PNG:
imagepng($this->image,$filename);
break;
case IMAGETYPE_GIF:
imagegif($this->image,$filename);
}
}
public function getWidth() {
return $this->width;
}
public function getHeight() {
return $this->height;
}
public function resize($width, $height) {
$new_width = $width;
$new_height = $height;
if (($width / $height) != ($this->getWidth() / $this->getHeight())) {
if ($this->getWidth() > $this->getHeight()) {
$new_height = $width / $this->getWidth() * $this->getHeight();
} else {
$new_width = $height / $this->getHeight() * $this->getWidth();
}
}
$new_img = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($new_img, 255, 255, 255);
imagefill($new_img, 0, 0, $white);
imagecopyresized($new_img, $this->image, 0, 0, 0, 0, $new_width, $new_height, $this->getWidth(), $this->getHeight());
$this->image = $new_img;
}
}
// class for queues
class queClass {
private $db;
private $queue_name;
private $field_name;
public function __construct($db, $name, $field, $type) {
$this->db = $db;
if ((is_array($field))&&(is_array($type))) {
for($idx = 0; $idx < count($field); $idx++) {
$type[$idx] = $field[$idx] . ' ' . $type[$idx];
}
$field_type = implode(',', $type);
$field = implode(',', $field);
} else {
$field_type = $field . ' ' . $type;
}
$this->queue_name = $name;
$this->field_name = $field;
$db->exec('CREATE TABLE IF NOT EXISTS ' . $this->queue_name . ' (' . $field_type . ')');
}
public function add($value) {
if (is_array($value)) {
foreach ($value as &$v) {
$v = '\'' . $v . '\'';
}
$value = implode(',', $value);
} else {
$value = '\'' . $value . '\'';
}
$this->db->exec('INSERT INTO ' . $this->queue_name . ' (' . $this->field_name . ') VALUES (' . $value . ')');
}
public function select() {
return $this->db->query('SELECT * FROM ' . $this->queue_name)->fetchAll();
}
public function show() {
$Results = $this->select();
if ($Results) {
echo PHP_EOL . DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR . $this->queue_name . PHP_EOL;
}
foreach ($Results as $row) {
$field_names = explode(',', $this->field_name);
foreach($field_names as $name) {
echo $row[$name] . ' ';
}
echo PHP_EOL;
}
}
public function clear() {
$this->db->exec('DELETE FROM ' . $this->queue_name);
}
}
// main procedure begin
if ($argc == 1) {
die($use_messages);
}
$db = new PDO('sqlite:' . dirname(__FILE__) . DIRECTORY_SEPARATOR . $db_name);
$queueDownload = new queClass($db, $queue_download, 'url', 'TEXT NOT NULL');
$queueFailed = new queClass($db, $queue_failed, array('url', 'status'), array('TEXT NOT NULL', 'VARCHAR(25) NOT NULL'));
$queueResize = new queClass($db, $queue_resize, 'path', 'TEXT NOT NULL');
$queueDone = new queClass($db, $queue_done, 'path', 'TEXT NOT NULL');
switch ($argv[1]) {
// clear mode
case $clear_mode:
$queueDownload->clear();
$queueFailed->clear();
$queueResize->clear();
$queueDone->clear();
break;
// shedule mode
case $shedule_mode:
if ($argc < 3) {
die($use_shedule_message);
}
if (!file_exists($argv[2])) {
die(PHP_EOL . $error_open_file . $argv[2] . PHP_EOL);
}
$Urls = file($argv[2], FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach($Urls as $url) {
$img_url = new urlClass($url);
if ($img_url->isURLexists()) {
$queueDownload->add($img_url->url);
} else {
$queueFailed->add(array($img_url->url, $img_url->status));
}
}
echo PHP_EOL . count($Urls). $urls_processed . PHP_EOL;
break;
// download mode
case $download_mode:
$counter = 0;
$Results = $queueDownload->select();
if (!$Results) {
die ($dl_queue_empty);
}
foreach($Results as $url) {
$img_url = new urlClass($url['url']);
if ($img_url->isURLexists()) {
$image = new imgClass();
$image->load($img_url->url);
$image->save(sys_get_temp_dir() . DIRECTORY_SEPARATOR . basename($img_url->url));
$queueResize->add(sys_get_temp_dir() . DIRECTORY_SEPARATOR . basename($img_url->url));
$counter++;
} else {
$queueFailed->add(array($img_url->url, $img_url->status));
}
}
echo PHP_EOL . $counter. $files_download . sys_get_temp_dir() . PHP_EOL;
$queueDownload->clear();
break;
// resize mode
case $resize_mode:
$counter = 0;
$Results = $queueResize->select();
if (!$Results) {
die ($rs_queue_empty);
}
foreach($Results as $img) {
if (file_exists($img['path'])) {
$image = new imgClass();
$image->load($img['path']);
$image->resize($new_width, $new_height);
$image->save(basename($img['path']));
$queueDone->add(basename($img['path']));
$counter++;
} else {
$queueFailed->add(array($img['path'], $error_file_not_exists));
}
}
echo PHP_EOL . $counter . $files_resized . PHP_EOL;
$queueResize->clear();
break;
// list mode
case $list_mode:
$queueDownload->show();
$queueFailed->show();
$queueResize->show();
$queueDone->show();
break;
default:
die($use_messages);
}