summaryrefslogtreecommitdiff
path: root/bridges/BakaUpdatesMangaReleasesBridge.php
blob: 27eca2803409abdf412f3487cfdc885c1ae99451 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
class BakaUpdatesMangaReleasesBridge extends BridgeAbstract {
	const NAME = 'Baka Updates Manga Releases';
	const URI = 'https://www.mangaupdates.com/';
	const DESCRIPTION = 'Get the latest series releases';
	const MAINTAINER = 'fulmeek';
	const PARAMETERS = array(array(
		'series_id' => array(
			'name'		=> 'Series ID',
			'type'		=> 'number',
			'required'	=> true,
			'exampleValue'	=> '12345'
		)
	));
	const LIMIT_COLS = 5;
	const LIMIT_ITEMS = 10;

	private $feedName = '';

	public function collectData() {
		$html = getSimpleHTMLDOM($this->getURI())
			or returnServerError('Series not found');

		// content is an unstructured pile of divs, ugly to parse
		$cols = $html->find('div#main_content div.row > div.text');
		if (!$cols)
			returnServerError('No releases');

		$rows = array_slice(
			array_chunk($cols, self::LIMIT_COLS), 0, self::LIMIT_ITEMS
		);

		if (isset($rows[0][1])) {
			$this->feedName = $this->filterHTML($rows[0][1]->plaintext);
		}

		foreach($rows as $cols) {
			if (count($cols) < self::LIMIT_COLS) continue;

			$item = array();
			$title = array();

			$item['content'] = '';

			$objDate = $cols[0];
			if ($objDate)
				$item['timestamp'] = strtotime($objDate->plaintext);

			$objTitle = $cols[1];
			if ($objTitle) {
				$title[] = $this->filterHTML($objTitle->plaintext);
				$item['content'] .= '<p>Series: ' . $this->filterText($objTitle->innertext) . '</p>';
			}

			$objVolume = $cols[2];
			if ($objVolume && !empty($objVolume->plaintext))
				$title[] = 'Vol.' . $objVolume->plaintext;

			$objChapter = $cols[3];
			if ($objChapter && !empty($objChapter->plaintext))
				$title[] = 'Chp.' . $objChapter->plaintext;

			$objAuthor = $cols[4];
			if ($objAuthor && !empty($objAuthor->plaintext)) {
				$item['author'] = $this->filterHTML($objAuthor->plaintext);
				$item['content'] .= '<p>Groups: ' . $this->filterText($objAuthor->innertext) . '</p>';
			}

			$item['title'] = implode(' ', $title);
			$item['uri'] = $this->getURI();
			$item['uid'] = $this->getSanitizedHash($item['title']);

			$this->items[] = $item;
		}
	}

	public function getURI(){
		$series_id = $this->getInput('series_id');
		if (!empty($series_id)) {
			return self::URI . 'releases.html?search=' . $series_id . '&stype=series';
		}
		return self::URI;
	}

	public function getName(){
		if(!empty($this->feedName)) {
			return $this->feedName . ' - ' . self::NAME;
		}
		return parent::getName();
	}

	private function getSanitizedHash($string) {
		return hash('sha1', preg_replace('/[^a-zA-Z0-9\-\.]/', '', ucwords(strtolower($string))));
	}

	private function filterText($text) {
		return rtrim($text, '* ');
	}

	private function filterHTML($text) {
		return $this->filterText(html_entity_decode($text));
	}
}