Skip to content

Commit

Permalink
anime/user reviews: fix entry details
Browse files Browse the repository at this point in the history
  • Loading branch information
irfan-dahir committed Oct 25, 2022
1 parent e2b6571 commit 4329864
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/Model/User/Reviews/UserAnimeReview.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static function fromParser(AnimeReviewParser $parser): UserAnimeReview
{
$instance = new self();

$instance->entry = $parser->getAnime();
$instance->entry = $parser->getAnime('user');
$instance->malId = $parser->getId();
$instance->url = $parser->getUrl();
$instance->type = $parser->getType();
Expand Down
4 changes: 2 additions & 2 deletions src/Model/User/Reviews/UserMangaReview.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class UserMangaReview extends MangaReview
* @var MangaMeta
*/
private MangaMeta $entry;

/**
* @param MangaReviewParser $parser
* @return UserAnimeReview
Expand All @@ -28,7 +28,7 @@ public static function fromParser(MangaReviewParser $parser): UserMangaReview
{
$instance = new self();

$instance->entry = $parser->getManga();
$instance->entry = $parser->getManga('user');
$instance->malId = $parser->getId();
$instance->url = $parser->getUrl();
$instance->type = $parser->getType();
Expand Down
52 changes: 31 additions & 21 deletions src/Parser/Reviews/AnimeReviewParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Jikan\Parser\Reviews;

use Jikan\Exception\ParserException;
use Jikan\Helper\JString;
use Jikan\Helper\Parser;
use Jikan\Model\Anime\AnimeReview;
Expand Down Expand Up @@ -47,13 +48,20 @@ public function getModel(): AnimeReview
/**
* @return AnimeMeta
*/
public function getAnime() : AnimeMeta
public function getAnime(string $page = null) : AnimeMeta
{
return new AnimeMeta(
$this->getAnimeTitle(),
$this->getAnimeURL(),
$this->getAnimeImageUrl()
);
return match ($page) {
'user' => new AnimeMeta(
$this->getAnimeTitle(),
$this->getAnimeURL(),
$this->getAnimeImageUrlFromUserPage()
),
default => new AnimeMeta(
$this->getAnimeTitle(),
$this->getAnimeURL(),
$this->getAnimeImageUrl()
),
};
}

/**
Expand Down Expand Up @@ -84,7 +92,7 @@ public function getUrl(): string
public function getAnimeTitle(): string
{
return $this->crawler
->filterXPath('//div[1]/div/div[2]/div/a')
->filterXPath('//div[contains(@class, "titleblock")]/a')
->text();
}

Expand All @@ -95,16 +103,16 @@ public function getAnimeTitle(): string
public function getAnimeUrl(): string
{
// User UserReviews page
$node = $this->crawler
->filterXPath('//div[1]/div/div[2]/div/a');

if ($node->count()) {
return $node->attr('href');
}
// $node = $this->crawler
// ->filterXPath('//div[1]/div/div[2]/div/a');
//
// if ($node->count()) {
// return $node->attr('href');
// }

// Recent UserReviews page
$node = $this->crawler
->filterXPath('//div[1]/div[2]/div[1]/div[1]/a');
->filterXPath('//div[contains(@class, "titleblock")]/a');

return $node->attr('href');
}
Expand All @@ -115,17 +123,19 @@ public function getAnimeUrl(): string
*/
public function getAnimeImageUrl(): string
{
// User UserReviews page
$node = $this->crawler
->filterXPath('//div[1]/div/div[1]/a/img');
->filterXPath('//div[contains(@class, "thumbbody")]/div[contains(@class, "body")]/div[contains(@class, "text")]/div[contains(@class, "thumb-right")]/a/img');

if ($node->count()) {
return Parser::parseImageQuality($node->attr('data-src'));
}
return Parser::parseImageQuality($node->attr('data-src'));
}

// Recent UserReviews page
/**
* @return string
*/
public function getAnimeImageUrlFromUserPage(): string
{
$node = $this->crawler
->filterXPath('//div[1]/div[2]/div[1]/div[1]/a/img');
->filterXPath('//div[contains(@class, "thumbbody")]/div[contains(@class, "thumb")]/a/img');

return Parser::parseImageQuality($node->attr('data-src'));
}
Expand Down
59 changes: 31 additions & 28 deletions src/Parser/Reviews/MangaReviewParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,20 @@ public function getModel(): MangaReview
/**
* @throws \Jikan\Exception\ParserException
*/
public function getManga() : MangaMeta
public function getManga(string $page = null) : MangaMeta
{
return new MangaMeta(
$this->getMangaTitle(),
$this->getMangaUrl(),
$this->getMangaImageUrl()
);
return match ($page) {
'user' => new MangaMeta(
$this->getMangaTitle(),
$this->getMangaUrl(),
$this->getMangaImageUrlFromUserPage()
),
default => new MangaMeta(
$this->getMangaTitle(),
$this->getMangaUrl(),
$this->getMangaImageUrl()
),
};
}

/**
Expand Down Expand Up @@ -196,34 +203,30 @@ public function getType(): ?string
public function getMangaTitle(): string
{
return $this->crawler
->filterXPath('//div[1]/div/div[2]/div/a')
->filterXPath('//div[contains(@class, "titleblock")]/a')
->text();
}

/**
* @return string
* @throws \InvalidArgumentException
* @throws \Jikan\Exception\ParserException
*/
public function getMangaImageUrl(): string
{
// User UserReviews page
$node = $this->crawler
->filterXPath('//div[1]/div/div[1]/a/img');
->filterXPath('//div[contains(@class, "thumbbody")]/div[contains(@class, "body")]/div[contains(@class, "text")]/div[contains(@class, "thumb-right")]/a/img');

if ($node->count()) {
return Parser::parseImageQuality($node->attr('data-src'));
}
return Parser::parseImageQuality($node->attr('data-src'));
}

// Recent UserReviews Anime page
/**
* @return string
*/
public function getMangaImageUrlFromUserPage(): string
{
$node = $this->crawler
->filterXPath('//div[1]/div[2]/div[1]/div[1]/a/img');

if ($node->count()) {
return Parser::parseImageQuality($node->attr('data-src'));
}
->filterXPath('//div[contains(@class, "thumbbody")]/div[contains(@class, "thumb")]/a/img');

throw new ParserException("Couldn't find the URL of reviewed image.");
return Parser::parseImageQuality($node->attr('data-src'));
}

/**
Expand All @@ -233,16 +236,16 @@ public function getMangaImageUrl(): string
public function getMangaUrl(): string
{
// User UserReviews page
$node = $this->crawler
->filterXPath('//div[1]/div/div[2]/div/a');

if ($node->count()) {
return $node->attr('href');
}
// $node = $this->crawler
// ->filterXPath('//div[1]/div/div[2]/div/a');
//
// if ($node->count()) {
// return $node->attr('href');
// }

// Recent UserReviews Anime page
$node = $this->crawler
->filterXPath('//div[1]/div[2]/div[1]/div[1]/a');
->filterXPath('//div[contains(@class, "titleblock")]/a');

return $node->attr('href');
}
Expand Down

0 comments on commit 4329864

Please sign in to comment.