Code Duplication    Length = 64-66 lines in 2 locations

application/models/Site_Model.php 2 locations

@@ 856-921 (lines=66) @@
853
	}
854
}
855
856
class SeaOtterScans extends Site_Model {
857
	public function getFullTitleURL(string $title_url) : string {
858
		return "http://reader.seaotterscans.com/series/{$title_url}";
859
	}
860
861
	public function isValidTitleURL(string $title_url) : bool {
862
		$success = (bool) preg_match('/^[a-z0-9_]+/', $title_url);
863
		if(!$success) log_message('error', "Invalid Title URL (SeaOtterScans): {$title_url}");
864
		return $success;
865
	}
866
	public function isValidChapter(string $chapter) : bool {
867
		$success = (bool) preg_match('/^en\/[0-9]+(?:\/[0-9]+(?:\/[0-9]+(?:\/[0-9]+)?)?)?$/', $chapter);
868
		if(!$success) log_message('error', 'Invalid Chapter (SeaOtterScans): '.$chapter);
869
		return $success;
870
	}
871
872
	public function getChapterData(string $title_url, string $chapter) : array {
873
		//LANG/VOLUME/CHAPTER/CHAPTER_EXTRA(/page/)
874
		$chapter_parts = explode('/', $chapter);
875
		return [
876
			'url'    => "https://reader.seaotterscans.com/read/{$title_url}/{$chapter}",
877
			'number' => ($chapter_parts[1] !== '0' ? "v{$chapter_parts[1]}/" : '') . "c{$chapter_parts[2]}" . (isset($chapter_parts[3]) ? ".{$chapter_parts[3]}" : '')/*)*/
878
		];
879
	}
880
881
	public function getTitleData(string $title_url) {
882
		$titleData = [];
883
884
		$fullURL = $this->getFullTitleURL($title_url);
885
		$data = $this->get_content($fullURL);
886
		if(strpos($data, '404 Page Not Found') === FALSE) {
887
			//FIXME: For whatever reason, we can't grab the entire div without simplexml shouting at us
888
			$data = preg_replace('/^[\S\s]*(<article[\S\s]*)<\/article>[\S\s]*$/', '$1', $data);
889
890
			$dom = new DOMDocument();
891
			libxml_use_internal_errors(true);
892
			$dom->loadHTML($data);
893
			libxml_use_internal_errors(false);
894
895
			$xpath = new DOMXPath($dom);
896
897
			$nodes_title = $xpath->query("//div[@class='large comic']/h1[@class='title']");
898
899
			//SOO sometimes uses volume groups which are above recent chapters (if they haven't been grouped yet), so make sure we check for both.
900
			$nodes_row   = $xpath->query("//div[@class='list']/div[@class='group']/div[@class='title' and text() = 'Chapters']/following-sibling::div[@class='element'][1]");
901
			if($nodes_row->length !== 1) {
902
				$nodes_row   = $xpath->query("//div[@class='list']/div[@class='group'][1]/div[@class='element'][1]");
903
			}
904
			if($nodes_title->length === 1 && $nodes_row->length === 1) {
905
				$titleData['title'] = trim($nodes_title[0]->textContent);
906
907
908
				$nodes_latest  = $xpath->query("div[@class='meta_r']", $nodes_row[0]);
909
				$nodes_chapter = $xpath->query("div[@class='title']/a", $nodes_row[0]);
910
911
				$link = (string) $nodes_chapter[0]->getAttribute('href');
912
				$titleData['latest_chapter'] = preg_replace('/.*\/read\/.*?\/(.*?)\/$/', '$1', $link);
913
				$titleData['last_updated'] =  date("Y-m-d H:i:s", strtotime((string) str_replace('.', '', explode(',', $nodes_latest[0]->textContent)[1])));
914
			}
915
		} else {
916
			//TODO: Throw ERRORS;
917
		}
918
919
		return (!empty($titleData) ? $titleData : NULL);
920
	}
921
}
922
923
class HelveticaScans extends Site_Model {
924
	public function getFullTitleURL(string $title_url) : string {
@@ 923-986 (lines=64) @@
920
	}
921
}
922
923
class HelveticaScans extends Site_Model {
924
	public function getFullTitleURL(string $title_url) : string {
925
		return "http://helveticascans.com/reader/series/{$title_url}";
926
	}
927
928
	public function isValidTitleURL(string $title_url) : bool {
929
		$success = (bool) preg_match('/^[a-z0-9_]+/', $title_url);
930
		if(!$success) log_message('error', "Invalid Title URL (HelveticaScans): {$title_url}");
931
		return $success;
932
	}
933
	public function isValidChapter(string $chapter) : bool {
934
		$success = (bool) preg_match('/^en\/[0-9]+(?:\/[0-9]+(?:\/[0-9]+(?:\/[0-9]+)?)?)?$/', $chapter);
935
		if(!$success) log_message('error', 'Invalid Chapter (HelveticaScans): '.$chapter);
936
		return $success;
937
	}
938
939
	public function getChapterData(string $title_url, string $chapter) : array {
940
		//LANG/VOLUME/CHAPTER/CHAPTER_EXTRA(/page/)
941
		$chapter_parts = explode('/', $chapter);
942
		return [
943
			'url'    => "http://helveticascans.com/reader/read/{$title_url}/{$chapter}",
944
			'number' => ($chapter_parts[1] !== '0' ? "v{$chapter_parts[1]}/" : '') . "c{$chapter_parts[2]}" . (isset($chapter_parts[3]) ? ".{$chapter_parts[3]}" : '')/*)*/
945
		];
946
	}
947
948
	public function getTitleData(string $title_url) {
949
		$titleData = [];
950
951
		$fullURL = $this->getFullTitleURL($title_url);
952
		$data = $this->get_content($fullURL);
953
		if(strpos($data, '404 Page Not Found') === FALSE) {
954
			//FIXME: For whatever reason, we can't grab the entire div without simplexml shouting at us
955
			$data = preg_replace('/^[\S\s]*(<article[\S\s]*)<\/article>[\S\s]*$/', '$1', $data);
956
957
			$dom = new DOMDocument();
958
			libxml_use_internal_errors(true);
959
			$dom->loadHTML($data);
960
			libxml_use_internal_errors(false);
961
962
			$xpath = new DOMXPath($dom);
963
964
			$nodes_title = $xpath->query("//div[@class='large comic']/h1[@class='title']");
965
			$nodes_row   = $xpath->query("//div[@class='list']/div[@class='group']/div[@class='title' and text() = 'Chapters']/following-sibling::div[@class='element'][1]");
966
			if($nodes_row->length !== 1) {
967
				$nodes_row   = $xpath->query("//div[@class='list']/div[@class='group'][1]/div[@class='element'][1]");
968
			}
969
			if($nodes_title->length === 1 && $nodes_row->length === 1) {
970
				$titleData['title'] = trim($nodes_title[0]->textContent);
971
972
973
				$nodes_latest  = $xpath->query("div[@class='meta_r']", $nodes_row[0]);
974
				$nodes_chapter = $xpath->query("div[@class='title']/a", $nodes_row[0]);
975
976
				$link = (string) $nodes_chapter[0]->getAttribute('href');
977
				$titleData['latest_chapter'] = preg_replace('/.*\/read\/.*?\/(.*?)\/$/', '$1', $link);
978
				$titleData['last_updated'] =  date("Y-m-d H:i:s", strtotime((string) str_replace('.', '', explode(',', $nodes_latest[0]->textContent)[1])));
979
			}
980
		} else {
981
			//TODO: Throw ERRORS;
982
		}
983
984
		return (!empty($titleData) ? $titleData : NULL);
985
	}
986
}
987