Follow first 'pdfUrl' link to get real datasheet URL

This commit is contained in:
Frank Fenor 2024-03-30 17:40:24 +01:00
parent 9b8d4c518a
commit 5dbe6559f5
No known key found for this signature in database
GPG key ID: E6B5029FCCE96376

View file

@ -90,6 +90,27 @@ class LCSCProvider implements InfoProviderInterface
return $this->getPartDetail($product); return $this->getPartDetail($product);
} }
/**
* @param string $pdfURL
* @return String
*/
private function getRealDatasheetUrl(?string $url): string
{
if (!empty($url) && preg_match("/^https:\/\/(datasheet\.lcsc\.com|www\.lcsc\.com\/datasheet)\/.*(C\d+)\.pdf$/", $url, $matches) > 0) {
$response = $this->lcscClient->request('GET', $url, [
'headers' => [
'User-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4.1 Safari/605.1.15',
'Referer' => 'https://www.lcsc.com/product-detail/_' . $matches[2] . '.html'
],
]);
if (preg_match('/(pdfUrl): ?("[^"]+wmsc\.lcsc\.com[^"]+\.pdf")/', $response->getContent(), $matches) > 0) {
$jsonObj = json_decode('{"' . $matches[1] . '": ' . $matches[2] . '}');
$url = $jsonObj->pdfUrl;
}
}
return $url;
}
/** /**
* @param string $term * @param string $term
* @return PartDetailDTO[] * @return PartDetailDTO[]
@ -273,7 +294,9 @@ class LCSCProvider implements InfoProviderInterface
return []; return [];
} }
return [new FileDTO($url, null)]; $realUrl = $this->getRealDatasheetUrl($url);
return [new FileDTO($realUrl, null)];
} }
/** /**