-
Notifications
You must be signed in to change notification settings - Fork 0
/
sabicripper.php
executable file
·366 lines (331 loc) · 11.6 KB
/
sabicripper.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?php
/**
* Script By Ervin Sabic
* Published under GPL
* Feel free to do whatever you please with this script.
* Website: www.gearsite.net
* Desc: PHP CLI Script that is used to pull data from a website. Useful for when you need to move hundreds of images and want to organize them.
*/
class Ripper {
public $galleryLinks = [];
public $sourceLinks = [];
public $dom;
public $debug;
public $data;
public $dir;
public function __construct($args, $count, $debug = false){
$this->dom = new DomDocument();
$this->source = $args[1];
$this->dir = $args[2];
$this->count = $count;
$this->timeout = 30;
}
public function init(){
echo
"
_____ _ _ ______ _ \n
/ ___| | | (_) | ___ (_) \n
\ `--. __ _| |__ _ ___ | |_/ /_ _ __ _ __ ___ _ __ \n
`--. \/ _` | '_ \| |/ __| | /| | '_ \| '_ \ / _ \ '__|\n
/\__/ / (_| | |_) | | (__ | |\ \| | |_) | |_) | __/ | \n
\____/ \__,_|_.__/|_|\___| \_| \_|_| .__/| .__/ \___|_| \n
| | | | \n
|_| |_| \n
\n ";
if($this->count > 3 || $this->count < 3){
if($this->count > 3) $this->throwError("Too Many Arguments", false);
if($this->count < 3) $this->throwError("Too Few Arguments", false);
if($this->debug){
echo "Displaying Passed Parameters: \n";
var_dump($argv);
}
echo "CLI Usage: \nphp ".$argv[0]." http://www.example.com /MyDirectoryThatIWantImagesIn \n";
die();
}else {
echo "Creating Directory. Standby.\n";
$this->generateDirectory($this->dir);
echo "Source: ".$this->source ."\n";
echo "Directory: ".$this->dir ."\n";
echo "CWD: ". getcwd() ."\n";
}
}
/**
* --------------------
*
* SCRAPING METHODS
*
* --------------------
*/
/**
* @param string $source
* Returns HTML using PhantomJS
*/
public function scrapeHtml($source) {
echo "Attempting to connect to ".$source."...\n";
file_put_contents("includes/page.json", json_encode($source));
$raw = shell_exec("phantomjs includes/scrape.js 2>&1");
if($raw == "failed" || $raw == ""){
$this->throwError("Error Getting Page ".$source."! \n Check to see if it is a valid URL. Include the http:// or https:// if you didn't.");
}else {
echo "Page found!\n";
}
return $raw;
}
/**
* @param string $html
* Returns an array of src links pulled from IMG tags using the DOM
*/
public function scrapeImageSources($html){
$ret = [];
$this->dom->loadHtml($html);
echo "Attempting to rip image source data from given html. Standby... \n";
foreach($this->dom->getElementsByTagName("img") as $found){
$src = $found->getAttribute('src');
echo "Found: ".$src."\n";
array_push($ret, $src);
}
return $ret;
}
/**
* @param array $locations
* Downloads images at the specified locations into the directory specified in the constructor.
*/
public function scrapeImages($locations){
echo "Attempting to download images from given source data. Standby... \n";
foreach($locations as $location){
echo "Scraping: ".$location;
$fname = basename($location);
//$this->downloadFile($location, $this->formatDirectory($this->dir).$fname);
$this->downloadFile($location);
//shell_exec("mv ".$fname." ".$this->getDirectory());
}
}
/**
* @param string $path
* Checks to see if a file exists and is readable then if it is, downloads it.
*/
public function downloadFile($path){
echo "Grabbing File:" .$path."\n";
shell_exec("wget -P".$this->getDirectory()." ".$path);
//echo "Attempting to place ".basename($path)." in ".$this->getDirectory();
}
/**
* @param array $data
* Dumps current data into a .json file
*/
public function dumpJSON($data){
echo "Dumping JSON... Standby... \n";
$data = json_encode($data);
file_put_contents("data.json", $data);
echo "JSON Successfully Saved!";
}
/**
* --------------------
*
* INPUT RELATED METHODS
*
* --------------------
*/
/**
* @param string $prompt
* Asks for user input via CLI
*/
public function requestInput(string $prompt, string $inputType, $options){
$input = readline($prompt);
if($input == "Exit"){
die();
}
elseif($input == "Help"){
echo "Nobody can help you here! Muahahahahahah";
}
else {
return $this->checkInput($input, $inputType, $options);
}
}
/**
* @param string $input
* @param string $type
* Checks the users input based on type.
*/
public function checkInput(string $input, string $type, array $options = [])
{
if($type == "Y/N"){
if($input == "Y"){
return True;
}elseif($input == "N"){
return False;
}
else {
return $this->throwError("Invalid Input! Must be Y or N.");
}
}
elseif($type == "Open"){
if($options == []) $this->throwError("Bad input options exception! Terminating...");
if (in_array($input, $options)){
return true;
}
else {
$this->throwError($input." is not a valid option!", false);
echo "Valid Options are: \n";
foreach($options as $item){
echo $item."\n";
}
die();
}
}
else {
return $this->throwError("Bad input type exception. Terminating");
}
}
/**
* --------------------
*
* FILTERING RELATED METHODS
*
* --------------------
*/
/**
* @param string $input
* @param array $subject
* Applies the necessary filter depending on the input.
*/
public function selectFilter($input, $subject){
$acceptedFilters = [
"Unique"=>$this->uniqueFilter($subject),
"Contain"=>$this->containFilter($subject),
"Exclude"=>$this->excludeFilter($subject),
"None"=>$this->returnSubject($subject),
];
if(array_key_exists($input,$acceptedFilters)){
$this->$acceptedFilters[$input]();
}else{
$input($subject);
}
}
/**
* @param array $subject
* Filters data depending on if value contains a string. Removes those that don't.
*/
public function containFilter($subject){
$condition = readline("What is the data supposed to contain?");
foreach($subject as $key=>$value){
if(strpos($value, $condition) !== false){
continue;
}else {
echo $value. " has been filtered out. Does not contain the string ".$condition."\n";
unset($subject[$key]);
}
}
return $subject;
}
/**
* @param array $subject
* Filters data depending on if value contains a string. Removes those that do.
*/
public function excludeFilter($subject){
$condition = readline("What string should be excluded from the data?");
foreach($subject as $key=>$value){
if(strpos($subject, $condition) === false){
continue;
}else {
unset($subject[$key]);
}
}
return $subject;
}
/**
* @param array $subject
* Filters data to make each entry unique.
*/
public function uniqueFilter($subject){
$subject = array_unique($subject);
return $subject;
}
/**
* @param array $subject
* @param string $url
* Filters data that does not start with $url
*/
public function externalLinkFilter($subject, $url){
foreach($subject as $key=>$value){
if(strpos($value, $url) === 0){
continue;
}else {
echo "Removing: ".$value." does not start with ".$url."\n";
unset($subject[$key]);
}
}
return $subject;
}
/**
* --------------------
*
* UTILITY METHODS
*
* --------------------
*/
/**
* View current data
* @param boolean $request
*/
public function viewData($request = false){
if($request == false){
return var_dump($this->data);
}else {
$option = $this->requestInput("Would you like to view the current data?", "Y/N");
if($option){
return var_dump($this->data);
}
}
}
/**
* --------------------
*
* CORE METHODS
*
* --------------------
*/
/**
* @param string $error
* @param boolean $kill
* Function used to return exceptions and kill the program by default.
*/
public function throwError(string $error, $kill = true){
echo "ERROR!: \n";
echo "\033[0;31m".$error." \033[0m \n";
if($kill){
die();
}
}
/**
* @param string $dir
* Used to format directories to be consistent with varying distros.
*/
public function formatDirectory(string $dir){
return rtrim($dir,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
}
/**
* Returns scrape source
*/
public function getSource(){
return $this->source;
}
/**
* Returns directory for downloads
*/
public function getDirectory(){
return $this->formatDirectory($this->dir);
}
/**
* @param string $directory
* Checks to see if directory exists and if it doesn't. Creates it.
*/
public function generateDirectory($directory){
if(file_exists($directory)){
echo "Directory Exists! Continuing...\n";
}else {
mkdir("./".$directory);
}
}
}
?>