Skip to content

Commit 9770ffa

Browse files
frank-fjbtronics
andauthored
LCSC: Follow first 'pdfUrl' link to get real datasheet URL (#582)
* Follow first 'pdfUrl' link to get real datasheet URL * Fix @param * Fix @param * Remove User-Agent header It's not needed - LCSC was just having some server troubles over the weekend * Added comment explaining the json_decode in getRealDatasheetUrl --------- Co-authored-by: Jan Böhmer <[email protected]>
1 parent da75cca commit 9770ffa

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/Services/InfoProviderSystem/Providers/LCSCProvider.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,28 @@ private function queryDetail(string $id): PartDetailDTO
9090
return $this->getPartDetail($product);
9191
}
9292

93+
/**
94+
* @param string $url
95+
* @return String
96+
*/
97+
private function getRealDatasheetUrl(?string $url): string
98+
{
99+
if (!empty($url) && preg_match("/^https:\/\/(datasheet\.lcsc\.com|www\.lcsc\.com\/datasheet)\/.*(C\d+)\.pdf$/", $url, $matches) > 0) {
100+
$response = $this->lcscClient->request('GET', $url, [
101+
'headers' => [
102+
'Referer' => 'https://www.lcsc.com/product-detail/_' . $matches[2] . '.html'
103+
],
104+
]);
105+
if (preg_match('/(pdfUrl): ?("[^"]+wmsc\.lcsc\.com[^"]+\.pdf")/', $response->getContent(), $matches) > 0) {
106+
//HACKY: The URL string contains escaped characters like \u002F, etc. To decode it, the JSON decoding is reused
107+
//See https://github.com/Part-DB/Part-DB-server/pull/582#issuecomment-2033125934
108+
$jsonObj = json_decode('{"' . $matches[1] . '": ' . $matches[2] . '}');
109+
$url = $jsonObj->pdfUrl;
110+
}
111+
}
112+
return $url;
113+
}
114+
93115
/**
94116
* @param string $term
95117
* @return PartDetailDTO[]
@@ -273,7 +295,9 @@ private function getProductDatasheets(?string $url): array
273295
return [];
274296
}
275297

276-
return [new FileDTO($url, null)];
298+
$realUrl = $this->getRealDatasheetUrl($url);
299+
300+
return [new FileDTO($realUrl, null)];
277301
}
278302

279303
/**

0 commit comments

Comments
 (0)