Code Duplication    Length = 57-58 lines in 2 locations

application/models/Site_Model.php 2 locations

@@ 407-464 (lines=58) @@
404
	}
405
}
406
407
class MangaPanda extends Site_Model {
408
	public function getFullTitleURL(string $title_url) : string {
409
		return "http://www.mangapanda.com/{$title_url}";
410
	}
411
412
	public function getChapterData(string $title_url, string $chapter) : array {
413
		return [
414
			'url'    => "http://www.mangapanda.com/{$title_url}/{$chapter}/",
415
			'number' => 'c'.$chapter
416
		];
417
	}
418
419
	public function isValidTitleURL(string $title_url) : bool {
420
		$success = (bool) preg_match('/^[a-z0-9-]+$/', $title_url);
421
		if(!$success) log_message('error', "Invalid Title URL (MangaPanda): {$title_url}");
422
		return $success;
423
	}
424
	public function isValidChapter(string $chapter) : bool {
425
		$success = (bool) preg_match('/^[0-9]+$/', $chapter);
426
		if(!$success) log_message('error', 'Invalid Chapter (MangaPanda): '.$chapter);
427
		return $success;
428
	}
429
430
	public function getTitleData(string $title_url) {
431
		$titleData = [];
432
433
		$fullURL = "http://www.mangapanda.com/{$title_url}";
434
435
		$data = $this->get_content($fullURL);
436
		if($data !== 'Can\'t find the manga series.') {
437
			//$data = preg_replace('/^[\S\s]*(<body id="body">[\S\s]*<\/body>)[\S\s]*$/', '$1', $data);
438
439
			$dom = new DOMDocument();
440
			libxml_use_internal_errors(true);
441
			$dom->loadHTML($data);
442
			libxml_use_internal_errors(false);
443
444
			$xpath = new DOMXPath($dom);
445
446
			$nodes_title = $xpath->query("//h2[@class='aname']");
447
			$nodes_row   = $xpath->query("(//table[@id='listing']/tr)[last()]");
448
			if($nodes_title->length === 1 && $nodes_row->length === 1) {
449
				//This seems to be be the only viable way to grab the title...
450
				$titleData['title'] = $nodes_title[0]->nodeValue;
451
452
				$nodes_latest  = $xpath->query("td[2]", $nodes_row[0]);
453
				$nodes_chapter = $xpath->query("td[1]/a", $nodes_row[0]);
454
455
				$titleData['latest_chapter'] = preg_replace('/^.*\/([0-9]+)$/', '$1', (string) $nodes_chapter[0]->getAttribute('href'));
456
				$titleData['last_updated'] =  date("Y-m-d H:i:s", strtotime((string) $nodes_latest[0]->nodeValue));
457
			}
458
		} else {
459
			//TODO: Throw ERRORS;
460
		}
461
462
		return (!empty($titleData) ? $titleData : NULL);
463
	}
464
}
465
466
class MangaStream extends Site_Model {
467
	public function getFullTitleURL(string $title_url) : string {
@@ 466-522 (lines=57) @@
463
	}
464
}
465
466
class MangaStream extends Site_Model {
467
	public function getFullTitleURL(string $title_url) : string {
468
		return "https://mangastream.com/manga/{$title_url}/";
469
	}
470
471
	public function isValidTitleURL(string $title_url) : bool {
472
		$success = (bool) preg_match('/^[a-z0-9_]+$/', $title_url);
473
		if(!$success) log_message('error', "Invalid Title URL (MangaStream): {$title_url}");
474
		return $success;
475
	}
476
	public function isValidChapter(string $chapter) : bool {
477
		$success = (bool) preg_match('/^(.*?)\/[0-9]+$/', $chapter);
478
		if(!$success) log_message('error', 'Invalid Chapter (MangaStream): '.$chapter);
479
		return $success;
480
	}
481
482
	public function getChapterData(string $title_url, string $chapter) : array {
483
		return [
484
			'url'    => "https://mangastream.com/r/{$title_url}/{$chapter}",
485
			'number' => 'c'.explode('/', $chapter)[0]
486
		];
487
	}
488
489
	public function getTitleData(string $title_url) {
490
		$titleData = [];
491
492
		$fullURL = $this->getFullTitleURL($title_url);
493
494
		$data = $this->get_content($fullURL);
495
		if($data !== 'Can\'t find the manga series.') {
496
			//$data = preg_replace('/^[\S\s]*(<body id="body">[\S\s]*<\/body>)[\S\s]*$/', '$1', $data);
497
498
			$dom = new DOMDocument();
499
			libxml_use_internal_errors(true);
500
			$dom->loadHTML($data);
501
			libxml_use_internal_errors(false);
502
503
			$xpath = new DOMXPath($dom);
504
505
			$nodes_title = $xpath->query("//div[contains(@class, 'content')]/div[1]/h1");
506
			$nodes_row   = $xpath->query("//div[contains(@class, 'content')]/div[1]/table/tr[2]"); //Missing tbody here..
507
			if($nodes_title->length === 1 && $nodes_row->length === 1) {
508
				$titleData['title'] = $nodes_title[0]->nodeValue;
509
510
				$nodes_latest  = $xpath->query("td[2]", $nodes_row[0]);
511
				$nodes_chapter = $xpath->query("td[1]/a", $nodes_row[0]);
512
513
				$titleData['latest_chapter'] = preg_replace('/^.*\/(.*?\/[0-9]+)\/[0-9]+$/', '$1', (string) $nodes_chapter[0]->getAttribute('href'));
514
				$titleData['last_updated'] =  date("Y-m-d H:i:s", strtotime((string) $nodes_latest[0]->nodeValue));
515
			}
516
		} else {
517
			//TODO: Throw ERRORS;
518
		}
519
520
		return (!empty($titleData) ? $titleData : NULL);
521
	}
522
}
523
524
class WebToons extends Site_Model {
525
	/* Webtoons.com has a very weird and pointless URL format.