aboutsummaryrefslogtreecommitdiff
path: root/bridges/MangareaderBridge.php
blob: e8d53f48eb1ee3da6df97b31cd90226276648b11 (plain) (blame)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<?php
define('MANGAREADER_LIMIT', 10); // The default limit
class MangareaderBridge extends BridgeAbstract{

	public function loadMetadatas() {

		$this->maintainer = "logmanoriginal";
		$this->name = "Mangareader Bridge";
		$this->uri = "http://www.mangareader.net";
		$this->description = "Returns the latest updates, popular mangas or manga updates (new chapters)";
		$this->update = "2016-08-09";

		$this->parameters["Get latest updates"] = '[]';
        $this->parameters["Get popular mangas"] = 
        '[
            {
                "name" : "Category",
                "identifier" : "category",
                "type" : "list",
                "required" : "true",
                "values" : [
                    { 
                        "name" : "All", 
                        "value" : "all" 
                    },
                    {
                        "name" : "Action",
                        "value" : "action"
                    },
                    {
                        "name" : "Adventure",
                        "value" : "adventure"
                    },
                    {
                        "name" : "Comedy",
                        "value" : "comedy"
                    },
                    {
                        "name" : "Demons",
                        "value" : "demons"
                    },
                    {
                        "name" : "Drama",
                        "value" : "drama"
                    },
                    {
                        "name" : "Ecchi",
                        "value" : "ecchi"
                    },
                    {
                        "name" : "Fantasy",
                        "value" : "fantasy"
                    },
                    {
                        "name" : "Gender Bender",
                        "value" : "gender-bender"
                    },
                    {
                        "name" : "Harem",
                        "value" : "harem"
                    },
                    {
                        "name" : "Historical",
                        "value" : "historical"
                    },
                    {
                        "name" : "Horror",
                        "value" : "horror"
                    },
                    {
                        "name" : "Josei",
                        "value" : "josei"
                    },
                    {
                        "name" : "Magic",
                        "value" : "magic"
                    },
                    {
                        "name" : "Martial Arts",
                        "value" : "martial-arts"
                    },
                    {
                        "name" : "Mature",
                        "value" : "mature"
                    },
                    {
                        "name" : "Mecha",
                        "value" : "mecha"
                    },
                    {
                        "name" : "Military",
                        "value" : "military"
                    },
                    {
                        "name" : "Mystery",
                        "value" : "mystery"
                    },
                    {
                        "name" : "One Shot",
                        "value" : "one-shot"
                    },
                    {
                        "name" : "Psychological",
                        "value" : "psychological"
                    },
                    {
                        "name" : "Romance",
                        "value" : "romance"
                    },
                    {
                        "name" : "School Life",
                        "value" : "school-life"
                    },
                    {
                        "name" : "Sci-Fi",
                        "value" : "sci-fi"
                    },
                    {
                        "name" : "Seinen",
                        "value" : "seinen"
                    },
                    {
                        "name" : "Shoujo",
                        "value" : "shoujo"
                    },
                    {
                        "name" : "Shoujoai",
                        "value" : "shoujoai"
                    },
                    {
                        "name" : "Shounen",
                        "value" : "shounen"
                    },
                    {
                        "name" : "Shounenai",
                        "value" : "shounenai"
                    },
                    {
                        "name" : "Slice of Life",
                        "value" : "slice-of-life"
                    },
                    {
                        "name" : "Smut",
                        "value" : "smut"
                    },
                    {
                        "name" : "Sports",
                        "value" : "sports"
                    },
                    {
                        "name" : "Super Power",
                        "value" : "super-power"
                    },
                    {
                        "name" : "Supernatural",
                        "value" : "supernatural"
                    },
                    {
                        "name" : "Tragedy",
                        "value" : "tragedy"
                    },
                    {
                        "name" : "Vampire",
                        "value" : "vampire"
                    },
                    {
                        "name" : "Yaoi",
                        "value" : "yaoi"
                    },
                    {
                        "name" : "Yuri",
                        "value" : "yuri"
                    }
                ],
                "exampleValue" : "All",
                "title" : "Select your category"
            }
        ]';
        $this->parameters["Get manga updates"] = 
        '[
           {
               "name" : "Path",
               "identifier" : "path",
               "type" : "text",
               "required" : "true",
               "pattern" : "[a-zA-Z0-9-_]*",
               "exampleValue" : "bleach, umi-no-kishidan",
               "title" : "URL part of desired manga"
           },
           {
               "name" : "Limit",
               "identifier" : "limit",
               "type" : "number",
               "exampleValue" : "10",
               "title" : "Number of items to return.\n-1 returns all"
           }
        ]';
	}
    
	public function collectData(array $param){

        $this->request = '';

        $type = "latest"; // can be "latest", "popular" or "path". Default is "latest"!
        $path = "latest";
        $limit = MANGAREADER_LIMIT;
        
        if(isset($param['category'])){ // Get popular updates
            $type = "popular";
            $path = "popular";
            if($param['category'] !== "all"){
                $path .= "/" . $param['category'];
            }
        }
        
        if(isset($param['path'])){ // Get manga updates
            $type = "path";
            $path = $param['path'];
        } 
        
        if(isset($param['limit']) && $param['limit'] !== ""){ // Get manga updates (optional parameter)
            $limit = $param['limit'];
        }
        
		// We'll use the DOM parser for this as it makes navigation easier
		$html = file_get_contents("http://www.mangareader.net/" . $path);
        if(!$html){
            $this->returnError('Could not receive data for ' . $path . '!', 400);
        }
        $doc = new DomDocument;
		@$doc->loadHTML($html);
		
		// Navigate via XPath
		$xpath = new DomXPath($doc);

        // Build feed based on the context (site updates or manga updates)
        if($type === "latest"){
            
            $this->request = 'Latest updates';
            
            // Query each item (consists of Manga + chapters)
            $nodes = $xpath->query("//*[@id='latestchapters']/table//td");

            foreach ($nodes as $node){
                // Query the manga
                $manga = $xpath->query("a[@class='chapter']", $node)->item(0);
                
                // Collect the chapters for each Manga
                $chapters = $xpath->query("a[@class='chaptersrec']", $node);

                if (isset($manga) && $chapters->length >= 1){
                    $item = new \Item();
                    $item->uri = 'http://www.mangareader.net' . htmlspecialchars($manga->getAttribute('href'));
                    $item->title = htmlspecialchars($manga->nodeValue);
                    
                    // Add each chapter to the feed	
                    $item->content = "";
                    
                    foreach ($chapters as $chapter){
                        if($item->content <> ""){
                            $item->content .= "<br>";
                        }
                        $item->content .= "<a href='http://www.mangareader.net" . htmlspecialchars($chapter->getAttribute('href')) . "'>" . htmlspecialchars($chapter->nodeValue) . "</a>";
                    }
                    
                    $this->items[] = $item;
                }
            }
        } 
        
        if($type === "popular"){
            
            $pagetitle = $xpath->query(".//*[@id='bodyalt']/h1")->item(0)->nodeValue;
            $this->request = substr($pagetitle, 0, strrpos($pagetitle, " -")); // "Popular mangas for ..."
            
            // Query all mangas
            $mangas = $xpath->query("//*[@id='mangaresults']/*[@class='mangaresultitem']");
            
            foreach ($mangas as $manga){ 
                
                // The thumbnail is encrypted in a css-style...
                // format: "background-image:url('<the part which is actually interesting>')"
                $mangaimgelement = $xpath->query(".//*[@class='imgsearchresults']", $manga)->item(0)->getAttribute('style'); 
                $thumbnail = substr($mangaimgelement, 22, strlen($mangaimgelement) - 24);

                $item = new \Item();
                $item->title = htmlspecialchars($xpath->query(".//*[@class='manga_name']//a", $manga)->item(0)->nodeValue);
                $item->uri = 'http://www.mangareader.net' . $xpath->query(".//*[@class='manga_name']//a", $manga)->item(0)->getAttribute('href');
                $item->author = htmlspecialchars($xpath->query("//*[@class='author_name']", $manga)->item(0)->nodeValue);
                $item->chaptercount = $xpath->query(".//*[@class='chapter_count']", $manga)->item(0)->nodeValue;
                $item->genre = htmlspecialchars($xpath->query(".//*[@class='manga_genre']", $manga)->item(0)->nodeValue);
                $item->content = '<a href="' . $item->uri . '"><img src="' . $thumbnail . '" alt="' . $item->title . '" /></a><p>' . $item->genre . '</p><p>' . $item->chaptercount . '</p>';
                $this->items[] = $item;
            }
        }
        
        if($type === "path") {
            
            $this->request = $xpath->query(".//*[@id='mangaproperties']//*[@class='aname']")->item(0)->nodeValue;
            
            $query = "(.//*[@id='listing']//tr)[position() > 1]";
                        
            if($limit !== -1){
                $query = "(.//*[@id='listing']//tr)[position() > 1][position() > last() - " . $limit . "]";
            } 
            
            $chapters = $xpath->query($query);
            
            foreach ($chapters as $chapter){
                $item = new \Item();
                $item->title = htmlspecialchars($xpath->query("td[1]", $chapter)->item(0)->nodeValue);
                $item->uri = 'http://www.mangareader.net' . $xpath->query("td[1]/a", $chapter)->item(0)->getAttribute('href');
                $item->timestamp = strtotime($xpath->query("td[2]", $chapter)->item(0)->nodeValue);
                array_unshift($this->items, $item);
            }           
        }
        
		// Return some dummy-data if no content available
		if(count($this->items) == 0){
			$item = new \Item();
			$item->content = "<p>No updates available</p>";
			
			$this->items[] = $item;
		}		
	}

	public function getName(){
		return (!empty($this->request) ? $this->request . ' - ' : '') . 'Mangareader Bridge';
	}

	public function getCacheDuration(){
		return 10800; // 3 hours
	}
}
?>