fix(dribble) (#4044)

This commit is contained in:
Dag 2024-03-31 22:10:59 +02:00 committed by GitHub
parent 182567e434
commit d5d470cbc2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -18,12 +18,12 @@ favicon-63b2904a073c89b52b19aa08cebc16a154bcf83fee8ecc6439968b1e6db569c7.ico';
{ {
$html = getSimpleHTMLDOM(self::URI); $html = getSimpleHTMLDOM(self::URI);
$json = $this->loadEmbeddedJsonData($html); $data = $this->fetchData($html);
foreach ($html->find('li[id^="screenshot-"]') as $shot) { foreach ($html->find('li[id^="screenshot-"]') as $shot) {
$item = []; $item = [];
$additional_data = $this->findJsonForShot($shot, $json); $additional_data = $this->findJsonForShot($shot, $data);
if ($additional_data === null) { if ($additional_data === null) {
$item['uri'] = self::URI . $shot->find('a', 0)->href; $item['uri'] = self::URI . $shot->find('a', 0)->href;
$item['title'] = $shot->find('.shot-title', 0)->plaintext; $item['title'] = $shot->find('.shot-title', 0)->plaintext;
@ -46,9 +46,8 @@ favicon-63b2904a073c89b52b19aa08cebc16a154bcf83fee8ecc6439968b1e6db569c7.ico';
} }
} }
private function loadEmbeddedJsonData($html) private function fetchData($html)
{ {
$json = [];
$scripts = $html->find('script'); $scripts = $html->find('script');
foreach ($scripts as $script) { foreach ($scripts as $script) {
@ -69,12 +68,17 @@ favicon-63b2904a073c89b52b19aa08cebc16a154bcf83fee8ecc6439968b1e6db569c7.ico';
$end = strpos($script->innertext, '];') + 1; $end = strpos($script->innertext, '];') + 1;
// convert JSON to PHP array // convert JSON to PHP array
$json = json_decode(substr($script->innertext, $start, $end - $start), true); $json = substr($script->innertext, $start, $end - $start);
break;
}
}
return $json; try {
// TODO: fix broken json
return Json::decode($json);
} catch (\JsonException $e) {
return [];
}
}
}
return [];
} }
private function findJsonForShot($shot, $json) private function findJsonForShot($shot, $json)