Code Duplication    Length = 57-58 lines in 2 locations

application/models/Site_Model.php 2 locations

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