Skip to content

Commit

Permalink
update 1.5.5 - Add Search functionality [4/4]
Browse files Browse the repository at this point in the history
- Added search functionality for Person w/ pagination support #7
  • Loading branch information
Nekomata committed Jan 24, 2018
1 parent 71aab0c commit 343d278
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 11 deletions.
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## Changelog

### 1.5.5 stable - January 24, 18
- **[Search]**
- Character Search: `nickname` field to `nicknames` since there can be multiple - 1.5.4
- Add `id` to Anime results 1.5.4
- Add Person/People search functionality w/ pagination support. e.g `$jikan->Search('query', PEOPLE)` `$jikan->Search('query', PERSON)` (`PEOPLE` is an alias of `PERSON`) - 1.5.5


### 1.5.3 stable - January 23, 18
- **[Core]** 1.4.6
Expand Down
7 changes: 2 additions & 5 deletions examples/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@
$jikan = new Jikan\Jikan;

$time_start = microtime(true);
// add extracted ID
// add extracted ID
// add extracted ID
//$jikan->Search('Code', ANIME); // add extracted ID
$jikan->Character(1);
//$jikan->Search('Code', ANIME);
$jikan->Search('shou', PEOPLE, 2);
$time_end = microtime(true);
$execution_time = ($time_end - $time_start);

Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Jikan - The Unofficial MyAnimelist PHP API
[![build](https://travis-ci.org/jikan-me/jikan.svg?branch=master)](https://travis-ci.org/jikan-me/jikan?branch=dev) [![stable](https://img.shields.io/badge/stable-1.5.3-blue.svg?style=flat)]() [![REST](https://img.shields.io/badge/REST-online-brightgreen.svg?style=flat)](https://jikan.me) [![Average time to resolve an issue](http://isitmaintained.com/badge/resolution/jikan-me/jikan.svg)](http://isitmaintained.com/project/jikan-me/jikan "Average time to resolve an issue") [![Percentage of issues still open](http://isitmaintained.com/badge/open/jikan-me/jikan.svg)](http://isitmaintained.com/project/jikan-me/jikan "Percentage of issues still open")
[![build](https://travis-ci.org/jikan-me/jikan.svg?branch=master)](https://travis-ci.org/jikan-me/jikan?branch=dev) [![stable](https://img.shields.io/badge/stable-1.5.5-blue.svg?style=flat)]() [![REST](https://img.shields.io/badge/REST-online-brightgreen.svg?style=flat)](https://jikan.me) [![Average time to resolve an issue](http://isitmaintained.com/badge/resolution/jikan-me/jikan.svg)](http://isitmaintained.com/project/jikan-me/jikan "Average time to resolve an issue") [![Percentage of issues still open](http://isitmaintained.com/badge/open/jikan-me/jikan.svg)](http://isitmaintained.com/project/jikan-me/jikan "Percentage of issues still open")

## The REST API & Documentation is available at [https://jikan.me](http://jikan.me)

Expand Down
56 changes: 52 additions & 4 deletions src/Lib/Parser/SearchParse.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function parse($type = ANIME) : Array
$results = [];
while (true) {
$result = [
'id' => null,
'url' => null,
'image_url' => null,
'title' => null,
Expand All @@ -45,7 +46,8 @@ public function parse($type = ANIME) : Array
if (preg_match('~^<td class="borderClass bgColor(0?|1?)" valign="top" width="50">$~', $line)) {
$i += 2;

preg_match('~<a class="hoverinfo_trigger" href="(.*)" id="(.*)" rel="(.*)">~', $this->file[$this->lineNo + $i], $this->matches);
preg_match('~<a class="hoverinfo_trigger" href="((.*)/(.*)/(.*))" id="(.*)" rel="(.*)">~', $this->file[$this->lineNo + $i], $this->matches);
$result['id'] = (int) $this->matches[3];
$result['url'] = $this->matches[1];

$i++;
Expand Down Expand Up @@ -88,6 +90,7 @@ public function parse($type = ANIME) : Array

break;
case CHARACTER:
var_dump($this->matches);
$this->addRule('result', '~<td class="normal_header" colspan="4">Search Results</td>~', function() {
$i = 0;
$results = [];
Expand All @@ -96,7 +99,7 @@ public function parse($type = ANIME) : Array
'url' => null,
'image_url' => null,
'name' => null,
'nickname' => null,
'nicknames' => null,
'anime' => [],
'manga' => []
];
Expand All @@ -116,8 +119,8 @@ public function parse($type = ANIME) : Array
preg_match('~<a href="(.*)">(.*?)</a>(<br /><small>(.*)</small>|)~', $this->file[$this->lineNo + $i], $this->matches);
$result['name'] = $this->matches[2];
(isset($this->matches[4]) && !empty($this->matches[4])) ?
$result['nickname'] = str_replace(['(', ')'], '', trim($this->matches[4]))
: $result['nickname'] = null ;
$result['nicknames'] = str_replace(['(', ')'], '', trim($this->matches[4]))
: $result['nicknames'] = null ;

$i += 2;
// :uh: i give up finding a pattern here, let's just do it the nasty way
Expand Down Expand Up @@ -174,6 +177,51 @@ public function parse($type = ANIME) : Array
});
break;
case PERSON:
$this->addRule('result', '~<td class="normal_header" colspan="4">Search Results</td>~', function() {
$i = 0;
$results = [];
while (true) {
$result = [
'id' => null,
'url' => null,
'image_url' => null,
'name' => null,
'nicknames' => null,
];
$line = $this->file[$this->lineNo + $i];

if (preg_match('~</table>~', $line)) {
break;
}

if (preg_match('~<td class="borderClass" width="25"><div class="picSurround"><a href="(/(.*)/(.*)/(.*))"><img src="(.*)"></a></div></td>~', $line, $this->matches)) {

$result['id'] = (int) $this->matches[3];
$result['url'] = BASE_URL . $this->matches[1];
$result['image_url'] = $this->matches[5];

$i++;
preg_match('~<td class="borderClass"><a href="(.*)">(.*)</a>(<br><small>(.*)</small></td>|)~', $this->file[$this->lineNo + $i], $this->matches);
$result['name'] = $this->matches[2];
(isset($this->matches[4]) && !empty($this->matches[4])) ?
$result['nicknames'] = str_replace(['(', ')'], '', trim($this->matches[4]))
: $result['nicknames'] = null ;

$results[] = $result;
}

$i++;
}

$this->model->set('Search', 'result', $results);
});


$this->addRule('result_last_page', '~<div class="spaceit" style="text-align: right;">~', function() {
preg_match_all('~<a href="(.*?)">(.*?)</a>~', $this->line, $this->matches);

$this->model->set('Search', 'result_last_page', (int) end($this->matches[2]));
});
break;
}

Expand Down
3 changes: 2 additions & 1 deletion src/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
define( 'ANIME' , 'anime' );
define( 'MANGA' , 'manga' );
define( 'CHARACTER' , 'character' );
define( 'PERSON' , 'person' );
define( 'PERSON' , 'person' );
define( 'PEOPLE', 'person'); // alias

0 comments on commit 343d278

Please sign in to comment.