Skip to content

Commit

Permalink
[anime] add videos page parsing #12
Browse files Browse the repository at this point in the history
  • Loading branch information
Nekomata committed Jan 7, 2018
1 parent 49cac2d commit ad1a832
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 9 deletions.
5 changes: 3 additions & 2 deletions changelog.md
@@ -1,8 +1,9 @@
## Changelog

### 1.3.0 stable - January 7, 18
### 1.4.0 stable - January 7, 18
- **[Anime|Manga]**
- Add News Parsing
- Add News Parsing - 1.3.0
- Add Video Parsing - 1.4.0 [#12](/../../issues/12)

### 1.2.1 stable - December 13, 17
- **[Anime|Manga]**
Expand Down
2 changes: 1 addition & 1 deletion examples/test.php
Expand Up @@ -8,7 +8,7 @@

$jikan = new Jikan\Jikan;

$jikan->Manga(1, [NEWS]);
$jikan->Anime(1, [VIDEOS]);

var_dump($jikan->response);

Expand Down
14 changes: 12 additions & 2 deletions src/Get/Anime.php
Expand Up @@ -2,18 +2,19 @@

namespace Jikan\Get;

use Jikan\Lib\Parser\AnimeCharacterStaffParse;
use Jikan\Lib\Parser\AnimeParse;
use Jikan\Lib\Parser\AnimeCharacterStaffParse;
use Jikan\Lib\Parser\AnimeEpisodeParse;
use Jikan\Lib\Parser\AnimeNewsParse;
use Jikan\Lib\Parser\AnimeVideoParse;


class Anime extends Get
{

public $canonical_path;

private $validExtends = [CHARACTERS_STAFF, EPISODES, NEWS];
private $validExtends = [CHARACTERS_STAFF, EPISODES, NEWS, VIDEOS];

public function __construct($id = null, $extend = null) {

Expand Down Expand Up @@ -96,4 +97,13 @@ private function news() {
$this->response = array_merge($this->response, $this->parser->parse());
}

private function videos() {
$this->parser = new AnimeVideoParse;

$this->parser->setPath($this->canonical_path.'/video');
$this->parser->loadFile();

$this->response = array_merge($this->response, $this->parser->parse());
}

}
88 changes: 88 additions & 0 deletions src/Lib/Parser/AnimeVideoParse.php
@@ -0,0 +1,88 @@
<?php
namespace Jikan\Lib\Parser;

use Jikan\Model\AnimeVideos as AnimeVideoModel;

class AnimeVideoParse extends TemplateParse
{

private $return = [];

public function parse() : Array
{



$this->model = new AnimeVideoModel;

/*
* Rules
*/



$this->loadFile($this->filePath);

$this->addRule('episode', '~<h2 class="mt0 pt0 clearfix">~', function() {
$parsingLine = $this->file[$this->lineNo + 10];
preg_match_all('~<div class="video-list-outer">(.*?)</div>~', $parsingLine, $this->matches);

$episode = [];
if (!empty($this->matches)) {
foreach ($this->matches[1] as $key => $value) {
preg_match('~<a class="video-list di-ib po-r" href="/(.*)"><img class="thumbs lazyload" src="(.*)" data-src="(.*)" width="320" height="320" data-pin-no-hover="true" data-title="(.*)" data-video-id="(.*)" data-anime-id="(.*)"><div class="info-container clearfix"><span class="title">(.*)<span class=" ml4"></span><br><span class="episode-title" title="(.*)">(.*)</span></span>~', $value, $this->matches);
$episode[] = [
'url' => BASE_URL . $this->matches[1],
'image_url' => $this->matches[3],
'episode' => $this->matches[7],
'title' => $this->matches[8]
];
}
}

$this->model->set('AnimeVideos', 'episode', $episode);
});

$this->addRule('promo', '~<div class="video-block promotional-video mt16">~', function() {
$running = true;
$i = 1;

$promo = [];
while($running) {
$line = $this->file[$this->lineNo + $i];
if (preg_match('~</table>~', $line)) {
$running = false;
}

if (preg_match('~<div class="video-list-outer po-r pv"><a class="iframe js-fancybox-video video-list di-ib po-r" href="(.*)"><img class="thumbs lazyload" src="(.*)" data-src="(.*)" width="320" height="320" data-pin-no-hover="true" data-title="(.*)" data-video-id="(.*)" data-anime-id="(.*)"><div class="info-container"><span class="title">(.*)</span></div>(<span class="btn-play" style="background-color: rgba\(255, 255, 255, 0\);">play</span>|)</a>~', $line, $this->matches)) {

$promo[] = [
'video_url' => $this->matches[1],
'image_url' => $this->matches[3],
'title' => $this->matches[7]
];

}

$i++;
}

$this->model->set('AnimeVideos', 'promo', $promo);
});



/*
* Parsing
*/

foreach ($this->file as $lineNo => $line) {
$this->line = $line;
$this->lineNo = $lineNo;

$this->find();
}

return (array) $this->model;
}
}
13 changes: 13 additions & 0 deletions src/Model/AnimeVideos.php
@@ -0,0 +1,13 @@
<?php

namespace Jikan\Model;


class AnimeVideos extends Model
{

public $episode = [];

public $promo = [];

}
9 changes: 5 additions & 4 deletions src/config.php
Expand Up @@ -15,10 +15,11 @@
/*
* Extend Flags
*/
define( 'CHARACTERS_STAFF' , 'charactersStaff' );
define( 'CHARACTERS' , 'characters' );
define( 'EPISODES' , 'episodes' );
define( 'NEWS' , 'news' );
define( 'CHARACTERS_STAFF' , 'charactersStaff' );
define( 'CHARACTERS' , 'characters' );
define( 'EPISODES' , 'episodes' );
define( 'NEWS' , 'news' );
define( 'VIDEOS' , 'videos' );

define( 'ANIME' , 'anime' );
define( 'MANGA' , 'manga' );
Expand Down

0 comments on commit ad1a832

Please sign in to comment.