Code Duplication    Length = 65-68 lines in 3 locations

application/models/Site_Model.php 3 locations

@@ 907-974 (lines=68) @@
904
	}
905
}
906
907
class SeaOtterScans extends Site_Model {
908
	public function getFullTitleURL(string $title_url) : string {
909
		return "https://reader.seaotterscans.com/series/{$title_url}";
910
	}
911
912
	public function isValidTitleURL(string $title_url) : bool {
913
		$success = (bool) preg_match('/^[a-z0-9_]+/', $title_url);
914
		if(!$success) log_message('error', "Invalid Title URL (SeaOtterScans): {$title_url}");
915
		return $success;
916
	}
917
	public function isValidChapter(string $chapter) : bool {
918
		$success = (bool) preg_match('/^en\/[0-9]+(?:\/[0-9]+(?:\/[0-9]+(?:\/[0-9]+)?)?)?$/', $chapter);
919
		if(!$success) log_message('error', 'Invalid Chapter (SeaOtterScans): '.$chapter);
920
		return $success;
921
	}
922
923
	public function getChapterData(string $title_url, string $chapter) : array {
924
		//LANG/VOLUME/CHAPTER/CHAPTER_EXTRA(/page/)
925
		$chapter_parts = explode('/', $chapter);
926
		return [
927
			'url'    => "https://reader.seaotterscans.com/read/{$title_url}/{$chapter}/",
928
			'number' => ($chapter_parts[1] !== '0' ? "v{$chapter_parts[1]}/" : '') . "c{$chapter_parts[2]}" . (isset($chapter_parts[3]) ? ".{$chapter_parts[3]}" : '')/*)*/
929
		];
930
	}
931
932
	public function getTitleData(string $title_url) {
933
		$titleData = [];
934
935
		$fullURL = $this->getFullTitleURL($title_url);
936
937
		$content = $this->get_content($fullURL);
938
		$data = $content['body'];
939
		if(strpos($data, '404 Page Not Found') === FALSE) {
940
			//FIXME: For whatever reason, we can't grab the entire div without simplexml shouting at us
941
			$data = preg_replace('/^[\S\s]*(<article[\S\s]*)<\/article>[\S\s]*$/', '$1', $data);
942
943
			$dom = new DOMDocument();
944
			libxml_use_internal_errors(true);
945
			$dom->loadHTML($data);
946
			libxml_use_internal_errors(false);
947
948
			$xpath = new DOMXPath($dom);
949
950
			$nodes_title = $xpath->query("//div[@class='large comic']/h1[@class='title']");
951
952
			//SOO sometimes uses volume groups which are above recent chapters (if they haven't been grouped yet), so make sure we check for both.
953
			$nodes_row   = $xpath->query("//div[@class='list']/div[@class='group']/div[@class='title' and text() = 'Chapters']/following-sibling::div[@class='element'][1]");
954
			if($nodes_row->length !== 1) {
955
				$nodes_row   = $xpath->query("//div[@class='list']/div[@class='group'][1]/div[@class='element'][1]");
956
			}
957
			if($nodes_title->length === 1 && $nodes_row->length === 1) {
958
				$titleData['title'] = trim($nodes_title->item(0)->textContent);
959
960
				$firstRow = $nodes_row->item(0);
961
				$nodes_latest  = $xpath->query("div[@class='meta_r']",  $firstRow);
962
				$nodes_chapter = $xpath->query("div[@class='title']/a", $firstRow);
963
964
				$link = (string) $nodes_chapter->item(0)->getAttribute('href');
965
				$titleData['latest_chapter'] = preg_replace('/.*\/read\/.*?\/(.*?)\/$/', '$1', $link);
966
				$titleData['last_updated'] = date("Y-m-d H:i:s", strtotime(((string) str_replace('.', '', explode(',', $nodes_latest[0]->textContent)[1]))));
967
			}
968
		} else {
969
			//TODO: Throw ERRORS;
970
		}
971
972
		return (!empty($titleData) ? $titleData : NULL);
973
	}
974
}
975
976
class HelveticaScans extends Site_Model {
977
	public function getFullTitleURL(string $title_url) : string {
@@ 976-1041 (lines=66) @@
973
	}
974
}
975
976
class HelveticaScans extends Site_Model {
977
	public function getFullTitleURL(string $title_url) : string {
978
		return "http://helveticascans.com/reader/series/{$title_url}";
979
	}
980
981
	public function isValidTitleURL(string $title_url) : bool {
982
		$success = (bool) preg_match('/^[a-z0-9_]+/', $title_url);
983
		if(!$success) log_message('error', "Invalid Title URL (HelveticaScans): {$title_url}");
984
		return $success;
985
	}
986
	public function isValidChapter(string $chapter) : bool {
987
		$success = (bool) preg_match('/^en\/[0-9]+(?:\/[0-9]+(?:\/[0-9]+(?:\/[0-9]+)?)?)?$/', $chapter);
988
		if(!$success) log_message('error', 'Invalid Chapter (HelveticaScans): '.$chapter);
989
		return $success;
990
	}
991
992
	public function getChapterData(string $title_url, string $chapter) : array {
993
		//LANG/VOLUME/CHAPTER/CHAPTER_EXTRA(/page/)
994
		$chapter_parts = explode('/', $chapter);
995
		return [
996
			'url'    => "http://helveticascans.com/reader/read/{$title_url}/{$chapter}/",
997
			'number' => ($chapter_parts[1] !== '0' ? "v{$chapter_parts[1]}/" : '') . "c{$chapter_parts[2]}" . (isset($chapter_parts[3]) ? ".{$chapter_parts[3]}" : '')/*)*/
998
		];
999
	}
1000
1001
	public function getTitleData(string $title_url) {
1002
		$titleData = [];
1003
1004
		$fullURL = $this->getFullTitleURL($title_url);
1005
1006
		$content = $this->get_content($fullURL);
1007
		$data = $content['body'];
1008
		if(strpos($data, '404 Page Not Found') === FALSE) {
1009
			//FIXME: For whatever reason, we can't grab the entire div without simplexml shouting at us
1010
			$data = preg_replace('/^[\S\s]*(<article[\S\s]*)<\/article>[\S\s]*$/', '$1', $data);
1011
1012
			$dom = new DOMDocument();
1013
			libxml_use_internal_errors(true);
1014
			$dom->loadHTML($data);
1015
			libxml_use_internal_errors(false);
1016
1017
			$xpath = new DOMXPath($dom);
1018
1019
			$nodes_title = $xpath->query("//div[@class='large comic']/h1[@class='title']");
1020
			$nodes_row   = $xpath->query("//div[@class='list']/div[@class='group']/div[@class='title' and text() = 'Chapters']/following-sibling::div[@class='element'][1]");
1021
			if($nodes_row->length !== 1) {
1022
				$nodes_row   = $xpath->query("//div[@class='list']/div[@class='group'][1]/div[@class='element'][1]");
1023
			}
1024
			if($nodes_title->length === 1 && $nodes_row->length === 1) {
1025
				$titleData['title'] = trim($nodes_title->item(0)->textContent);
1026
1027
1028
				$nodes_latest  = $xpath->query("div[@class='meta_r']", $nodes_row[0]);
1029
				$nodes_chapter = $xpath->query("div[@class='title']/a", $nodes_row[0]);
1030
1031
				$link = (string) $nodes_chapter->item(0)->getAttribute('href');
1032
				$titleData['latest_chapter'] = preg_replace('/.*\/read\/.*?\/(.*?)\/$/', '$1', $link);
1033
				$titleData['last_updated'] =  date("Y-m-d H:i:s", strtotime((string) str_replace('.', '', explode(',', $nodes_latest->item(0)->textContent)[1])));
1034
			}
1035
		} else {
1036
			//TODO: Throw ERRORS;
1037
		}
1038
1039
		return (!empty($titleData) ? $titleData : NULL);
1040
	}
1041
}
1042
1043
class SenseScans extends Site_Model {
1044
	public function getFullTitleURL(string $title_url) : string {
@@ 1043-1107 (lines=65) @@
1040
	}
1041
}
1042
1043
class SenseScans extends Site_Model {
1044
	public function getFullTitleURL(string $title_url) : string {
1045
		return "http://reader.sensescans.com/series/{$title_url}";
1046
	}
1047
1048
	public function isValidTitleURL(string $title_url) : bool {
1049
		$success = (bool) preg_match('/^[a-z0-9_]+/', $title_url);
1050
		if(!$success) log_message('error', "Invalid Title URL (SenseScans): {$title_url}");
1051
		return $success;
1052
	}
1053
	public function isValidChapter(string $chapter) : bool {
1054
		$success = (bool) preg_match('/^en\/[0-9]+(?:\/[0-9]+(?:\/[0-9]+(?:\/[0-9]+)?)?)?$/', $chapter);
1055
		if(!$success) log_message('error', 'Invalid Chapter (SenseScans): '.$chapter);
1056
		return $success;
1057
	}
1058
1059
	public function getChapterData(string $title_url, string $chapter) : array {
1060
		//LANG/VOLUME/CHAPTER/CHAPTER_EXTRA(/page/)
1061
		$chapter_parts = explode('/', $chapter);
1062
		return [
1063
			'url'    => "http://reader.sensescans.com/read/{$title_url}/{$chapter}/",
1064
			'number' => ($chapter_parts[1] !== '0' ? "v{$chapter_parts[1]}/" : '') . "c{$chapter_parts[2]}" . (isset($chapter_parts[3]) ? ".{$chapter_parts[3]}" : '')/*)*/
1065
		];
1066
	}
1067
1068
	public function getTitleData(string $title_url) {
1069
		$titleData = [];
1070
1071
		$fullURL = $this->getFullTitleURL($title_url);
1072
1073
		$content = $this->get_content($fullURL);
1074
		$data = $content['body'];
1075
		if(strpos($data, '404 Page Not Found') === FALSE) {
1076
			//FIXME: For whatever reason, we can't grab the entire div without simplexml shouting at us
1077
			$data = preg_replace('/^[\S\s]*(<article[\S\s]*)<\/article>[\S\s]*$/', '$1', $data);
1078
1079
			$dom = new DOMDocument();
1080
			libxml_use_internal_errors(true);
1081
			$dom->loadHTML($data);
1082
			libxml_use_internal_errors(false);
1083
1084
			$xpath = new DOMXPath($dom);
1085
1086
			$nodes_title = $xpath->query("//div[@class='large comic']/h1[@class='title']");
1087
			$nodes_row   = $xpath->query("//div[@class='list']/div[@class='group']/div[@class='title' and text() = 'Chapters']/following-sibling::div[@class='element'][1]");
1088
			if($nodes_row->length !== 1) {
1089
				$nodes_row   = $xpath->query("//div[@class='list']/div[@class='group'][1]/div[@class='element'][1]");
1090
			}
1091
			if($nodes_title->length === 1 && $nodes_row->length === 1) {
1092
				$titleData['title'] = trim($nodes_title->item(0)->textContent);
1093
1094
				$nodes_latest    = $xpath->query("div[@class='meta_r']", $nodes_row[0]);
1095
				$nodes_chapter   = $xpath->query("div[@class='title']/a", $nodes_row[0]);
1096
1097
				$link = (string) $nodes_chapter->item(0)->getAttribute('href');
1098
				$titleData['latest_chapter'] = preg_replace('/.*\/read\/.*?\/(.*?)\/$/', '$1', $link);
1099
				$titleData['last_updated'] =  date("Y-m-d H:i:s", strtotime((string) str_replace('.', '', explode(',', $nodes_latest->item(0)->textContent)[1])));
1100
			}
1101
		} else {
1102
			//TODO: Throw ERRORS;
1103
		}
1104
1105
		return (!empty($titleData) ? $titleData : NULL);
1106
	}
1107
}
1108