forked from captainkuro/crawler
/
manga.php
410 lines (353 loc) · 9.66 KB
/
manga.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<?php
$title = 'Manga Reader Crawler';
interface Manga_Crawler {
// bool
public function is_supported($url);
// bool
public function is_single_chapter($url);
// string
public function get_infix($url);
// array
public function get_info($base);
// array
public function get_images($chapter_url, $prefix, $infix);
}
function get_crawler($url) {
// loop through classes
$scan = scandir('mangas');
foreach ($scan as $entry) {
if ($pos = strpos($entry, '.php')) {
$classname = substr($entry, 0, $pos);
include_once 'mangas/'.$entry;
$object = new $classname;
if ($object->is_supported($url)) {
return $object;
}
}
}
throw new Exception('NOT SUPPORTED');
}
function print_pages($pages) {
echo '<div class="col-sm-4">';
echo '<ul>';
foreach ($pages as $name => $src) {
echo "<li><a href='$src'>$name</a></li>";
}
echo '</ul>';
echo '</div>';
}
class Manga_Pattern_Manager {
const FILE = 'manga.pattern.json';
public $patterns = array();
public function __construct() {
$json = json_decode(file_get_contents(self::FILE), true);
if ($json) {
$this->patterns = $json;
}
}
public function all() {
return $this->patterns;
}
public function convert_and_add($patterns, $prefixes) {
$ar_pattern = array_map('trim', explode("\n", $patterns));
$ar_prefix = array_map('trim', explode("\n", $prefixes));
foreach ($ar_pattern as $i => $pattern) {
$this->patterns[] = array(
'pattern' => $pattern,
'prefix' => $ar_prefix[$i],
);
}
$this->save();
}
public function del($id) {
array_splice($this->patterns, $id, 1);
$this->save();
}
public function save() {
file_put_contents(self::FILE, json_encode($this->patterns));
}
public function get_prefix($url) {
foreach ($this->patterns as $p) {
if (strpos($url, $p['pattern']) !== false) {
return $p['prefix'];
}
}
return '';
}
}
$stage_pattern = false;
$stage_1 = false;
$stage_2 = false;
$stage_3 = false;
if (@$_REQUEST['action'] === 'infix') {
// AJAX: retrieve Infix
$url = $_REQUEST['url'];
try {
$crawler = get_crawler($url);
echo $crawler->get_infix($url);
} catch (Exception $e) {
echo 'NOT SUPPORTED';
}
exit;
} else if (@$_REQUEST['action'] === 'prefix') {
// AJAX: retrieve Prefix
$manager = new Manga_Pattern_Manager();
$url = @$_REQUEST['url'];
echo $manager->get_prefix($url);
exit;
} else if (@$_REQUEST['action'] === 'pattern') {
// Page: Pattern management
$stage_pattern = true;
$manager = new Manga_Pattern_Manager();
if (array_key_exists('del_id', $_REQUEST)) {
$id = $_REQUEST['del_id'];
$manager->del($id);
} else if ($_POST) {
$patterns = $_POST['patterns'];
$prefixes = $_POST['prefixes'];
$manager->convert_and_add($patterns, $prefixes);
}
$all_patterns = $manager->all();
} else {
// Page: Manga crawling
$stage_1 = true;
if (array_key_exists('stage1', $_POST)) {
// Display Stage 2
$base = $_POST['base'];
$prefix = $_POST['prefix'];
$singlefix = $_POST['singlefix'];
$crawler = get_crawler($base);
if ($crawler->is_single_chapter($base)) {
$stage_3 = true;
$info = array(
array(
'url' => $base,
'desc' => 'Single chapter',
'infix' => $singlefix,
),
);
} else {
$stage_2 = true;
$info = $crawler->get_info($base);
}
} else if (array_key_exists('stage2', $_POST)) {
// Display Stage 3
$stage_2 = true;
$stage_3 = true;
$base = $_POST['base'];
$prefix = $_POST['prefix'];
$info = @$_POST['info'];
$crawler = get_crawler($base);
// filter check
$info = array_filter($info, function ($v) {
return isset($v['check']);
});
}
}
include '_header.php';
?>
<script type="text/javascript">
var global_check = false;
function click_this() {
global_check = !global_check;
var tags = document.getElementsByTagName("input");
for (i in tags) {
if (tags[i].type == "checkbox") {
tags[i].checked = global_check;
}
}
}
function parse_it() {
var val = document.getElementById("parse_it").value;
val.match(/^(\w+)-/)
var from = parseInt(RegExp.$1);
val.match(/-(\w+)$/);
var to = parseInt(RegExp.$1);
return [from, to];
}
function check_it() {
var list = parse_it();
for (var i=list[0]; i<=list[1]; i++) {
var el = document.getElementById("check-"+i);
el.checked = true;
}
}
function uncheck_it() {
var list = parse_it();
for (var i=list[0]; i<=list[1]; i++) {
var el = document.getElementById("check-"+i);
el.checked = false;
}
}
function check_this_row(el) {
var suspects = el.getElementsByTagName('input');
suspects[0].click();
}
document.addEventListener('DOMContentLoaded', function(){
// When the URL is blurred = grab prefix and infix
var $base = $('input[name="base"]').first();
$base.on('blur', function(e) {
var url = $base.val();
if (!url) return;
// auto prefix
$.post('?action=prefix', {
url: url
}, function (data) {
$('input[name="prefix"]').val(data);
});
// auto infix
$.post('?action=infix', {
url: url
}, function (data) {
$('input[name="singlefix"]').val(data);
});
});
});
</script>
<ul class="nav nav-tabs">
<li><a href="?action=" tabindex="-1">Manga</a></li>
<li><a href="?action=pattern" tabindex="-1">Pattern</a></li>
</ul>
<div class='container'>
<?php if ($stage_1): ?>
<form class='form-horizontal' method='post'>
<fieldset>
<legend>1</legend>
<div class='form-group row'>
<label class='col-sm-3 control-label'>Manga URL</label>
<div class='col-sm-9 controls'>
<input type='text' class='form-control' name='base' value="<?=@$_POST['base'];?>" />
</div>
</div>
<div class='form-group row'>
<label class='col-sm-3 control-label'>Prefix</label>
<div class='col-sm-4 controls'>
<input type='text' class='form-control' name='prefix' value="<?=@$_POST['prefix'];?>" />
</div>
</div>
<div class='form-group row'>
<label class='col-sm-3 control-label'>Infix</label>
<div class='col-sm-4 controls'>
<input type='text' class='form-control' name='singlefix' value="<?=@$_POST['singlefix'];?>" placeholder="only for single chapter" />
</div>
</div>
<div class='col-md-offset-3'>
<button class='btn btn-primary' type='submit' name='stage1'>Submit</button>
</div>
</fieldset>
</form>
<?php endif; ?>
<?php if ($stage_2): ?>
<form class='form-horizontal' method='post'>
<fieldset>
<legend>2 - <?php echo get_class($crawler); ?></legend>
<div class='form-group row'>
<label class='col-sm-3 control-label'>Manga URL</label>
<div class='col-sm-9 controls'>
<input type='text' class='form-control' name='base' value="<?=@$_POST['base'];?>" />
</div>
</div>
<div class='form-group row'>
<label class='col-sm-3 control-label'>Prefix</label>
<div class='col-sm-4 controls'>
<input type='text' class='form-control' name='prefix' value="<?=@$_POST['prefix'];?>" />
</div>
</div>
<div class='form-group row'>
<label class='col-sm-3 control-label'>Choose chapter</label>
<div class='col-sm-8 controls'>
<div class="col-xs-1">
<label class='checkbox'>
<input type='checkbox' name='all' onclick='click_this()' /> All
</label>
</div>
<div class="col-xs-4">
<input id='parse_it' class='form-control' type='text' />
</div>
<div class="col-xs-4">
<button type='button' onclick='check_it()' class='btn btn-default'>Check</button>
<button type='button' onclick='uncheck_it()' class='btn btn-default'>Uncheck</button>
</div>
<br>
<table class='table table-condensed'>
<thead>
<tr>
<th class='col-sm-1'>#</th>
<th class='col-sm-8'>Chapter Name</th>
<th>Infix</th>
</tr>
</thead>
<tbody>
<?php foreach ($info as $i => $v): ?>
<tr onclick='check_this_row(this)'>
<td>
<input type='checkbox' name="info[<?=$i;?>][check]" value="<?=$i;?>" id="check-<?=(int)$v['infix'];?>" onclick='event.stopPropagation()' />
</td>
<td>
<?=$v['desc'];?>
<input type='hidden' name="info[<?=$i;?>][url]" value="<?=$v['url'];?>" />
<input type='hidden' name="info[<?=$i;?>][desc]" value="<?=$v['desc'];?>" />
</td>
<td>
<input type='text' name="info[<?=$i;?>][infix]" value="<?=$v['infix'];?>" onclick='event.stopPropagation()' />
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
<div class='col-md-offset-3'>
<button class='btn btn-primary' type='submit' name='stage2'>Submit</button>
</div>
</fieldset>
</form>
<?php endif; ?>
<?php if ($stage_3): ?>
<fieldset>
<legend>3 - <?php echo get_class($crawler); ?></legend>
</fieldset>
<div class='row-fluid'>
<?php
$info = array_reverse($info, true);
foreach ($info as $v) {
$pages = $crawler->get_images($v['url'], $prefix, $v['infix']);
print_pages($pages);
}
?>
</div>
<?php endif; ?>
<?php if ($stage_pattern): ?>
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Pattern</th>
<th>Prefix</th>
<th>X</th>
</tr>
</thead>
<tbody>
<?php foreach($all_patterns as $i => $row): ?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $row['pattern']; ?></td>
<td><?php echo $row['prefix']; ?></td>
<td><a href="?action=pattern&del_id=<?=$i;?>">Delete</a></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<form method="post" action="?action=pattern">
<div class="col-xs-7">
<textarea class="form-control" name="patterns" placeholder="URLs" rows="10"></textarea>
</div>
<div class="col-xs-4">
<textarea class="form-control" name="prefixes" placeholder="Prefix-es" rows="10"></textarea>
</div>
<button class='btn btn-primary' type='submit' name='add'>Add</button>
</form>
<?php endif; ?>
</div>
<?php
include '_footer.php';