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

MangaFox::getTitleData()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 20
nc 18
nop 2
dl 0
loc 29
ccs 0
cts 20
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 MangaFox extends Base_Site_Model {
4
	public $titleFormat   = '/^[a-z0-9_]+$/';
5
	public $chapterFormat = '/^(?:v[0-9a-zA-Z]+\/)?c[0-9\.]+$/';
6
	public $customType    = 1;
7
8
	public function getFullTitleURL(string $title_url) : string {
9
		return "http://mangafox.la/manga/{$title_url}/";
10
	}
11
12
	public function getChapterData(string $title_url, string $chapter) : array {
13
		return [
14
			'url'    => "http://mangafox.la/manga/{$title_url}/{$chapter}/1.html",
15
			'number' => $chapter
16
		];
17
	}
18
19
	public function getTitleData(string $title_url, bool $firstGet = FALSE) : ?array {
20
		$titleData = [];
21
22
		$fullURL = $this->getFullTitleURL($title_url);
23
		$content = $this->get_content($fullURL);
24
25
		$data = $this->parseTitleDataDOM(
26
			$content,
27
			$title_url,
28
			"//title",
29
			"//body/div[@id='page']/div[@class='left']/div[@id='chapters']/ul[1]/li[1]",
30
			"div/span[@class='date']",
31
			"div/h3/a"
32
		);
33
		if($data) {
34
			$titleData['title'] = html_entity_decode(explode(' Manga - Read ', $data['nodes_title']->textContent)[0]);
35
36
			$link = preg_replace('/^(.*\/)(?:[0-9]+\.html)?$/', '$1', (string) $data['nodes_chapter']->getAttribute('href'));
37
			$chapterURLSegments = explode('/', $link);
38
			$titleData['latest_chapter'] = $chapterURLSegments[5] . (isset($chapterURLSegments[6]) && !empty($chapterURLSegments[6]) ? "/{$chapterURLSegments[6]}" : "");
39
			$titleData['last_updated'] =  date("Y-m-d H:i:s", strtotime((string) $data['nodes_latest']->nodeValue));
40
41
			if($firstGet) {
42
				$titleData = array_merge($titleData, $this->doCustomFollow($content['body']));
43
			}
44
		}
45
46
		return (!empty($titleData) ? $titleData : NULL);
47
	}
48
49
	//FIXME: This entire thing feels like an awful implementation....BUT IT WORKS FOR NOW.
50
	public function handleCustomFollow(callable $callback, string $data = "", array $extra = []) {
51
		preg_match('/var sid=(?<id>[0-9]+);/', $data, $matches);
52
53
		$formData = [
54
			'action' => 'add',
55
			'sid'    => $matches['id']
56
		];
57
58
		$cookies = [
59
			"mfvb_userid={$this->config->item('mangafox_userid')}",
60
			"mfvb_password={$this->config->item('mangafox_password')}",
61
			"bmsort=last_chapter"
62
		];
63
		$content = $this->get_content('http://mangafox.la/ajax/bookmark.php', implode("; ", $cookies), "", TRUE, TRUE, $formData);
64
65
		$callback($content, $matches['id'], function($body) {
66
			return $body == 'true';
67
		});
68
	}
69
	public function doCustomUpdate() {
70
		$titleDataList = [];
71
72
		$cookies = [
73
			"mfvb_uf={$this->config->item('mangafox_userid')}",
74
			"mfvb_pf={$this->config->item('mangafox_password')}",
75
			"bmsort=last_chapter",
76
			"bmorder=za"
77
		];
78
		$content = $this->get_content('http://mangafox.la/bookmark/?status=currentreading&sort=last_chapter&order=za', implode("; ", $cookies), "", TRUE);
79
80
		if(!is_array($content)) {
81
			log_message('error', "{$this->site} /bookmark | Failed to grab URL (See above curl error)");
82
		} else {
83
			$headers     = $content['headers'];
84
			$status_code = $content['status_code'];
85
			$data        = $content['body'];
86
87
			if(!($status_code >= 200 && $status_code < 300)) {
88
				log_message('error', "{$this->site} /bookmark | Bad Status Code ({$status_code})");
89
			} else if(empty($data)) {
90
				log_message('error', "{$this->site} /bookmark | Data is empty? (Status code: {$status_code})");
91
			} else {
92
				$data = preg_replace('/^[\s\S]+<ul id="bmlist">/', '<ul id="bmlist">', $data);
93
				$data = preg_replace('/<!-- end of bookmark -->[\s\S]+$/', '<!-- end of bookmark -->', $data);
94
95
				$dom = new DOMDocument();
96
				libxml_use_internal_errors(TRUE);
97
				$dom->loadHTML($data);
98
				libxml_use_internal_errors(FALSE);
99
100
				print $data;
101
				$xpath      = new DOMXPath($dom);
102
				$nodes_rows = $xpath->query("//ul[@id='bmlist']/li/div[@class='series_grp' and h2[@class='title']/span[@class='updatedch'] and dl]");
103
				if($nodes_rows->length > 0) {
104
					foreach($nodes_rows as $row) {
105
						$titleData = [];
106
107
						$nodes_title   = $xpath->query("h2[@class='title']/a[contains(@class, 'title')]", $row);
108
						$nodes_chapter = $xpath->query("dl/dt[1]/a[@class='chapter']", $row);
109
						$nodes_latest  = $xpath->query("dl/dt[1]/em/span[@class='timing']", $row);
110
111
						if($nodes_title->length === 1 && $nodes_chapter->length === 1 && $nodes_latest->length === 1) {
112
							$title = $nodes_title->item(0);
113
114
							$titleData['title'] = trim($title->textContent);
115
116
117
							$link = preg_replace('/^(.*\/)(?:[0-9]+\.html)?$/', '$1', (string) $nodes_chapter->item(0)->getAttribute('href'));
118
							$chapterURLSegments = explode('/', $link);
119
							$titleData['latest_chapter'] = $chapterURLSegments[5] . (isset($chapterURLSegments[6]) && !empty($chapterURLSegments[6]) ? "/{$chapterURLSegments[6]}" : "");
120
121
							$titleData['last_updated'] =  date("Y-m-d H:i:s", strtotime((string) $nodes_latest->item(0)->nodeValue));
122
123
							$title_url = explode('/', $title->getAttribute('href'))[4];
124
							$titleDataList[$title_url] = $titleData;
125
						} else {
126
							log_message('error', "{$this->site}/Custom | Invalid amount of nodes (TITLE: {$nodes_title->length} | CHAPTER: {$nodes_chapter->length}) | LATEST: {$nodes_latest->length})");
127
						}
128
					}
129
				} else {
130
					log_message('error', "{$this->site} | Following list is empty?");
131
				}
132
			}
133
		}
134
		return $titleDataList;
135
	}
136
	public function doCustomCheck(string $oldChapterString, string $newChapterString) {
137
		$oldChapterSegments = explode('/', $oldChapterString);
138
		$newChapterSegments = explode('/', $newChapterString);
139
140
		$status = $this->doCustomCheckCompare($oldChapterSegments, $newChapterSegments);
141
142
		return $status;
143
	}
144
}
145