Code Duplication    Length = 57-58 lines in 2 locations

application/models/Site_Model.php 2 locations

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