Code Duplication    Length = 65-68 lines in 3 locations

application/models/Site_Model.php 3 locations

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