Completed
Push — master ( 0f9479...65c76e )
by Angus
02:49
created

LHTranslation::_fixTitle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 13
ccs 0
cts 5
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php declare(strict_types=1); defined('BASEPATH') OR exit('No direct script access allowed');
2
3
class LHTranslation extends Base_Site_Model {
4
	public $titleFormat   = '/^[a-zA-Z0-9_\-.]+$/';
5
	public $chapterFormat = '/^[0-9\.]+$/';
6
7
	public function getFullTitleURL(string $title_url) : string {
8
		$title_url = str_replace('.','', $title_url);
9
		return "http://lhtranslation.com/{$title_url}";
10
	}
11
12
	public function getChapterData(string $title_url, string $chapter) : array {
13
		$title_url = str_replace('-chapter', '', $title_url);
14
		return [
15
			'url'    => "http://read.lhtranslation.com/read-{$title_url}-chapter-{$chapter}.html",
16
			'number' => "c{$chapter}"
17
		];
18
	}
19
20
	public function getTitleData(string $title_url, bool $firstGet = FALSE) : ?array {
21
		$titleData = [];
22
23
		$title_url = str_replace('.','', $title_url);
24
		$title_url = $this->_fixTitle($title_url);
25
		$fullURL = "http://lhtranslation.com/{$title_url}/feed/";
26
		$content = $this->get_content($fullURL);
27
28
		$data = $content['body'];
29
		$xml = simplexml_load_string($data) or die("Error: Cannot create object");
30
		if(((string) $xml->{'channel'}->title) !== 'Comments on: '){
31
			if(isset($xml->{'channel'}->item[0])) {
32
				if($title = (string) $xml->{'channel'}->item[0]->category) {
33
					$titleData['title'] = trim($title);
34
35
					$titleData['latest_chapter'] = str_replace('-', '.', preg_replace('/^.*?-(?:.*?)chapter-(.*?)\/$/', '$1', (string) $xml->{'channel'}->item[0]->link));
36
					$titleData['last_updated']   = date("Y-m-d H:i:s", strtotime((string) $xml->{'channel'}->item[0]->pubDate));
37
				} else {
38
					log_message('error', "Title is empty? - {$xml->{'channel'}->title} (LHTranslation): {$title_url}");
39
					return NULL;
40
				}
41
			}
42
		} else {
43
			log_message('error', "Series missing? (LHTranslation): {$title_url}");
44
			return NULL;
45
		}
46
47
		return (!empty($titleData) ? $titleData : NULL);
48
	}
49
50
	//FIXME: This is just a quick hack, we should really have a better way to handle this.
51
	//       Maybe when we finally add an "extra" data option for titles?
52
	private function _fixTitle(string $title_url) {
53
		//LHTranslation is a mess and doesn't work nice when we try to grab the chapter list.
54
		//This is just a quick fix for series with bad titles.
55
56
		$fixedTitles = [
57
			'genjitsu-shugi-yuusha-no-oukoku-saikenki' => 'genjitsushugisha-no-oukokukaizouki'
58
		];
59
		if(array_key_exists($title_url, $fixedTitles)) {
60
			$title_url = $fixedTitles[$title_url];
61
		}
62
63
		return $title_url;
64
	}
65
}
66