Completed
Push — master ( 222f63...75778e )
by Angus
02:21
created

DynastyScans::getChapterData()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 37
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 21
nc 7
nop 2
dl 0
loc 37
ccs 0
cts 18
cp 0
crap 42
rs 8.439
c 0
b 0
f 0
1
<?php declare(strict_types=1); defined('BASEPATH') OR exit('No direct script access allowed');
2
3
class DynastyScans extends Base_Site_Model {
4
	//FIXME: This has some major issues. SEE: https://github.com/DakuTree/manga-tracker/issues/58
5
6
	public $titleFormat   = '/^[a-z0-9_]+:--:(?:0|1)$/';
7
	public $chapterFormat = '/^[0-9a-z_]+$/';
8
9
	public function getFullTitleURL(string $title_url) : string {
10
		$title_parts = explode(':--:', $title_url);
11
		$url_type = ($title_parts[1] == '0' ? 'series' : 'chapters');
12
13
		return 'https://dynasty-scans.com/'.$url_type.'/'.$title_parts[0];
14
	}
15
16
	public function getChapterData(string $title_url, string $chapter) : array {
17
		$title_parts = explode(':--:', $title_url);
18
		/* Known chapter url formats (# is numbers):
19
		       chapters_#A_#B - Ch#A-#B
20
		       ch_#A          - Ch#A
21
		       ch_#A_#B       - Ch#A.#B
22
		       <NOTHING>      - Oneshot (This is passed as "oneshot")
23
		*/
24
25
		$chapterData = [
26
			'url'    => 'https://dynasty-scans.com/chapters/' . $title_parts[0].'_'.$chapter,
27
			'number' => ''
28
		];
29
30
		if($chapter == 'oneshot') {
31
			$chapterData['number'] = 'oneshot';
32
		} else {
33
			$chapter = preg_replace("/^([a-zA-Z]+)/", '$1_', $chapter);
34
			$chapterSegments = explode('_', $chapter);
35
			switch($chapterSegments[0]) {
36
				case 'ch':
37
					$chapterData['number'] = 'c'.$chapterSegments[1].(isset($chapterSegments[2]) && !empty($chapterSegments[2]) ? '.'.$chapterSegments[2] : '');
38
					break;
39
40
				case 'chapters':
41
					//This is barely ever used, but I have seen it.
42
					$chapterData['number'] = 'c'.$chapterSegments[1].'-'.$chapterSegments[2];
43
					break;
44
45
				default:
46
					//TODO: FALLBACK, ALERT ADMIN?
47
					$chapterData['number'] = $chapter;
48
					break;
49
			}
50
		}
51
		return $chapterData;
52
	}
53
54
	public function getTitleData(string $title_url, bool $firstGet = FALSE) : ?array {
55
		$titleData = [];
56
57
		$fullURL = $this->getFullTitleURL($title_url);
58
		$content = $this->get_content($fullURL);
59
60
		$title_parts = explode(':--:', $title_url);
61
		switch($title_parts[1]) {
62
			case '0':
63
				//Normal series.
64
				$data = $this->parseTitleDataDOM(
65
					$content,
66
					$title_url,
67
					"//h2[@class='tag-title']/b[1]",
68
					"(//dl[@class='chapter-list']/dd[a[contains(@href,'/chapters/')]])[last()]",
69
					"small",
70
					"a[@class='name']"
71
				);
72
				if($data) {
73
					$titleData['title'] = $data['nodes_title']->textContent;
74
					//In cases where the series is a doujin, try and prepend the copyright.
75
					preg_match('/\/doujins\/[^"]+">(.+?)(?=<\/a>)<\/a>/', $content['body'], $matchesD);
76
					if(!empty($matchedD) && substr($matchesD[1], 0, -7) !== 'Original') {
77
						$titleData['title'] = substr($matchesD[1], 0, -7).' - '.$titleData['title'];
78
					}
79
80
					$chapterURLSegments = explode('/', (string) $data['nodes_chapter']->getAttribute('href'));
81
					if (strpos($chapterURLSegments[2], $title_parts[0]) !== false) {
82
						$titleData['latest_chapter'] = substr($chapterURLSegments[2], strlen($title_parts[0]) + 1);
83
					} else {
84
						$titleData['latest_chapter'] = $chapterURLSegments[2];
85
					}
86
87
					$titleData['last_updated'] =  date("Y-m-d H:i:s", strtotime(str_replace("'", '', substr((string) $data['nodes_latest']->textContent, 9))));
88
				}
89
				break;
90
91
			case '1':
92
				//Oneshot.
93
				$data = $content['body'];
94
95
				preg_match('/<b>.*<\/b>/', $data, $matchesT);
96
				preg_match('/\/doujins\/[^"]+">(.+)?(?=<\/a>)<\/a>/', $data, $matchesD);
97
				$titleData['title'] = (!empty($matchesD) ? ($matchesD[1] !== 'Original' ? $matchesD[1].' - ' : '') : '') . substr($matchesT[0], 3, -4);
98
99
				$titleData['latest_chapter'] = 'oneshot'; //This will never change
100
101
				preg_match('/<i class="icon-calendar"><\/i> (.*)<\/span>/', $data, $matches);
102
				$titleData['last_updated']   = date("Y-m-d H:i:s", strtotime($matches[1]));
103
104
				//Oneshots are special, and really shouldn't need to be re-tracked
105
				$titleData['status'] = '2';
106
				break;
107
108
			default:
109
				//something went wrong
110
				break;
111
		}
112
		return (!empty($titleData) ? $titleData : NULL);
113
	}
114
}
115