Skip to content

Commit

Permalink
Merge pull request #552 from jikan-me/bugfix/entry-relations
Browse files Browse the repository at this point in the history
Fixes #551
  • Loading branch information
irfan-dahir committed May 29, 2024
2 parents eac0085 + 2f8d192 commit 6853fac
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Model/Common/MalUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ class MalUrl
/**
* @var string
*/
private $name;
private string $name;

/**
* @var string
*/
private $url;
private string $url;

/**
* Genre constructor.
Expand Down
40 changes: 38 additions & 2 deletions src/Parser/Anime/AnimeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -724,11 +724,47 @@ public function getStreamingLinks(): array
public function getRelated(): array
{
$related = [];

// MAL has divided relations up into tiles and table format
// We first parse whatever there's in the tiles
$this->crawler
->filterXPath('//div[contains(@class, "related-entries")]/div[contains(@class, "entries-tile")]/div[contains(@class, "entry")]')
->each(
function (Crawler $c) use (&$related) {
$relation = $c->filterXPath('//div[@class="content"]/div[@class="relation"]')->text();

// strip entry type (if any)
$relation = JString::cleanse(
preg_replace("~\s\(.*\)~", '', $relation)
);

$links = $c->filterXPath('//div[@class="content"]/div[@class="title"]/a');

// Check for empty links #justMALThings
if ($links->count() === 1 // if it's the only link MAL has listed
&& empty($links->first()->text()) // and if its a bugged/empty link
) {
$related[$relation] = [];
return;
}

// Remove empty/bugged links #justMALThings
foreach ($links as $node) {
if (empty($node->textContent)) {
$node->parentNode->removeChild($node);
}
}

$related[$relation][] = (new MalUrlParser($links))->getModel();
}
);

// Then we'll parse the table
$this->crawler
->filterXPath('//table[contains(@class, "anime_detail_related_anime")]/tr')
->filterXPath('//table[contains(@class, "entries-table")]/tr')
->each(
function (Crawler $c) use (&$related) {
$links = $c->filterXPath('//td[2]/a');
$links = $c->filterXPath('//td[2]//a');
$relation = JString::cleanse(
str_replace(':', '', $c->filterXPath('//td[1]')->text())
);
Expand Down
40 changes: 38 additions & 2 deletions src/Parser/Manga/MangaParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -611,11 +611,47 @@ public function getExternalLinks(): array
public function getMangaRelated(): array
{
$related = [];

// MAL has divided relations up into tiles and table format
// We first parse whatever there's in the tiles
$this->crawler
->filterXPath('//div[contains(@class, "related-entries")]/div[contains(@class, "entries-tile")]/div[contains(@class, "entry")]')
->each(
function (Crawler $c) use (&$related) {
$relation = $c->filterXPath('//div[@class="content"]/div[@class="relation"]')->text();

// strip entry type (if any)
$relation = JString::cleanse(
preg_replace("~\s\(.*\)~", '', $relation)
);

$links = $c->filterXPath('//div[@class="content"]/div[@class="title"]/a');

// Check for empty links #justMALThings
if ($links->count() === 1 // if it's the only link MAL has listed
&& empty($links->first()->text()) // and if its a bugged/empty link
) {
$related[$relation] = [];
return;
}

// Remove empty/bugged links #justMALThings
foreach ($links as $node) {
if (empty($node->textContent)) {
$node->parentNode->removeChild($node);
}
}

$related[$relation][] = (new MalUrlParser($links))->getModel();
}
);

// Then we'll parse the table
$this->crawler
->filterXPath('//table[contains(@class, "anime_detail_related_anime")]/tr')
->filterXPath('//table[contains(@class, "entries-table")]/tr')
->each(
function (Crawler $c) use (&$related) {
$links = $c->filterXPath('//td[2]/a');
$links = $c->filterXPath('//td[2]//a');
$relation = JString::cleanse(
str_replace(':', '', $c->filterXPath('//td[1]')->text())
);
Expand Down

0 comments on commit 6853fac

Please sign in to comment.