Skip to content

Commit

Permalink
fix #386 #385
Browse files Browse the repository at this point in the history
  • Loading branch information
irfan-dahir committed Jan 7, 2022
1 parent d41c453 commit de723b4
Showing 1 changed file with 50 additions and 21 deletions.
71 changes: 50 additions & 21 deletions src/Parser/Common/AnimeCardParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,22 @@ public function getAnimeUrl(): string
*/
public function getProducer(): array
{
return $this->crawler
->filterXPath('//span[contains(@class, "producer")]/a')
->each(
function (Crawler $crawler) {
return (new MalUrlParser($crawler))->getModel();
}
);
$node = $this->crawler->filterXPath('//div[contains(@class, "synopsis")]/div[contains(@class, "properties")]/div[1]/span');

$malUrl = [];

$node->each(function(Crawler $c) use(&$malUrl) {
$node = $c->filterXPath('//span');

if (str_contains($node->text(), "Studio") || str_contains($node->text(), "Studios")) {
$node->nextAll()->filterXPath('//a')
->each(function(Crawler $c) use(&$malUrl) {
$malUrl[] = (new MalUrlParser($c))->getModel();
});
}
});

return $malUrl;
}

/**
Expand All @@ -91,8 +100,17 @@ function (Crawler $crawler) {
*/
public function getEpisodes(): ?int
{
$eps = $this->crawler->filterXPath('//div[contains(@class, "eps")]')->text();
$eps = JString::cleanse($eps);
$eps = $this->crawler->filterXPath('//div/div[2]/div[2]/span[contains(@class, "item")][2]/span[1]');

if (!$eps->count()) {
$eps = $this->crawler->filterXPath('//div/div[2]/div[2]/span[contains(@class, "item")][3]/span[1]');
}

if (!$eps->count()) {
return null;
}

$eps = JString::cleanse($eps->text());
$eps = str_replace(' eps', '', $eps);

return $eps === '?' ? null : (int)$eps;
Expand All @@ -105,7 +123,7 @@ public function getEpisodes(): ?int
*/
public function getSource(): string
{
return $this->crawler->filterXPath('//span[contains(@class, "source")]')->text();
return $this->crawler->filterXPath('//div[contains(@class, "synopsis")]/div[contains(@class, "properties")]/div[2]/span[2]')->text();
}

/**
Expand Down Expand Up @@ -145,7 +163,7 @@ function (Crawler $crawler) {
*/
public function getDescription(): string
{
return $this->crawler->filterXPath('//div[contains(@class, "synopsis")]/span')->text();
return $this->crawler->filterXPath('//div[contains(@class, "synopsis")]/p')->text();
}

/**
Expand All @@ -155,6 +173,9 @@ public function getDescription(): string
*/
public function getType(): ?string
{
// this information is no longer available
return null;

$text = $this->crawler->filterXPath('//div[contains(@class, "info")]');

if (!$text->count()) {
Expand Down Expand Up @@ -182,7 +203,7 @@ public function getAirDates(): ?\DateTimeImmutable
$date = str_replace(
'(JST)',
'',
JString::cleanse($this->crawler->filterXPath('//span[contains(@class, "remain-time")]')->text())
JString::cleanse($this->crawler->filterXPath('//div/div[2]/div[2]/span[contains(@class, "item")][1]')->text())
);

try {
Expand All @@ -200,10 +221,15 @@ public function getAirDates(): ?\DateTimeImmutable
*/
public function getMembers(): int
{
$count = $this->crawler->filterXPath('//div[contains(@class, "scormem")]/span')->text();
$count = $this->crawler->filterXPath('//div[contains(@class, "information")]/div/div/div[2]')->text();

$count = JString::cleanse($count);

return (int)str_replace(',', '', $count);
$count = str_replace('K', '000', $count);
$count = str_replace('M', '000000', $count);


return (int)str_replace([',', '.'], '', $count);
}

/**
Expand Down Expand Up @@ -265,7 +291,8 @@ public function getAnimeImage(): ?string
*/
public function getAnimeScore(): ?float
{
$score = JString::cleanse($this->crawler->filterXPath('//span[contains(@class, "score")]')->text());
$score = JString::cleanse($this->crawler->filterXPath('//div[contains(@class, "information")]/div/div/div[1]')->text());

if ($score === 'N/A') {
return null;
}
Expand All @@ -280,6 +307,9 @@ public function getAnimeScore(): ?float
*/
public function getLicensors(): array
{
// this information is no longer available
return [];

// if anyone can fix this spaghetti code, most welcome
$node = $this->crawler->filterXPath('//div[contains(@class, "synopsis")]//p[contains(@class, "mb4 mt8")]');

Expand All @@ -306,15 +336,15 @@ public function getLicensors(): array
*/
public function getThemes(): array
{
// if anyone can fix this spaghetti code, most welcome
$node = $this->crawler->filterXPath('//div[contains(@class, "synopsis")]//p[contains(@class, "mb4 mt8")]');

$node = $this->crawler->filterXPath('//div[contains(@class, "synopsis")]/div[contains(@class, "properties")]/div[3]/span');

$malUrl = [];

$node->each(function(Crawler $c) use(&$malUrl) {
$node = $c->filterXPath('//span');

if (str_contains($node->text(), "Theme")) {
if (str_contains($node->text(), "Theme") || str_contains($node->text(), "Themes")) {
$node->nextAll()->filterXPath('//a')
->each(function(Crawler $c) use(&$malUrl) {
$malUrl[] = (new MalUrlParser($c))->getModel();
Expand All @@ -332,15 +362,14 @@ public function getThemes(): array
*/
public function getDemographics(): array
{
// if anyone can fix this spaghetti code, most welcome
$node = $this->crawler->filterXPath('//div[contains(@class, "synopsis")]//p[contains(@class, "mb4 mt8")]');
$node = $this->crawler->filterXPath('//div[contains(@class, "synopsis")]/div[contains(@class, "properties")]/div[4]/span');

$malUrl = [];

$node->each(function(Crawler $c) use(&$malUrl) {
$node = $c->filterXPath('//span');

if (str_contains($node->text(), "Demographic")) {
if (str_contains($node->text(), "Demographic") || str_contains($node->text(), "Demographics")) {
$node->nextAll()->filterXPath('//a')
->each(function(Crawler $c) use(&$malUrl) {
$malUrl[] = (new MalUrlParser($c))->getModel();
Expand Down

0 comments on commit de723b4

Please sign in to comment.