2025-02-19 00:44:49 +01:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2025 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Services\InfoProviderSystem\Providers;
|
|
|
|
|
2025-02-19 23:55:58 +01:00
|
|
|
use App\Services\InfoProviderSystem\DTOs\FileDTO;
|
2025-02-20 00:18:05 +01:00
|
|
|
use App\Services\InfoProviderSystem\DTOs\ParameterDTO;
|
2025-02-19 00:44:49 +01:00
|
|
|
use App\Services\InfoProviderSystem\DTOs\PartDetailDTO;
|
2025-02-20 00:32:03 +01:00
|
|
|
use App\Services\InfoProviderSystem\DTOs\PriceDTO;
|
|
|
|
use App\Services\InfoProviderSystem\DTOs\PurchaseInfoDTO;
|
2025-02-19 00:44:49 +01:00
|
|
|
use App\Services\InfoProviderSystem\DTOs\SearchResultDTO;
|
2025-02-20 16:06:10 +01:00
|
|
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
2025-02-19 00:44:49 +01:00
|
|
|
use Symfony\Component\DomCrawler\Crawler;
|
|
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
|
|
|
|
|
|
|
class ReicheltProvider implements InfoProviderInterface
|
|
|
|
{
|
|
|
|
|
2025-02-20 00:32:03 +01:00
|
|
|
public const DISTRIBUTOR_NAME = "Reichelt";
|
|
|
|
|
2025-02-20 16:06:10 +01:00
|
|
|
public function __construct(private readonly HttpClientInterface $client,
|
|
|
|
#[Autowire(env: "bool:PROVIDER_REICHELT_ENABLED")]
|
|
|
|
private readonly bool $enabled = true,
|
|
|
|
#[Autowire(env: "PROVIDER_REICHELT_LANGUAGE")]
|
|
|
|
private readonly string $language = "en",
|
|
|
|
#[Autowire(env: "PROVIDER_REICHELT_COUNTRY")]
|
2025-02-20 16:29:37 +01:00
|
|
|
private readonly string $country = "DE",
|
|
|
|
#[Autowire(env: "PROVIDER_REICHELT_INCLUDE_VAT")]
|
2025-02-20 16:37:13 +01:00
|
|
|
private readonly bool $includeVAT = false,
|
|
|
|
#[Autowire(env: "PROVIDER_REICHELT_CURRENCY")]
|
|
|
|
private readonly string $currency = "EUR",
|
2025-02-20 16:29:37 +01:00
|
|
|
)
|
2025-02-19 00:44:49 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getProviderInfo(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'name' => 'Reichelt',
|
2025-02-20 15:59:35 +01:00
|
|
|
'description' => 'Webscrapping from reichelt.com to get part information',
|
|
|
|
'url' => 'https://www.reichelt.com/',
|
2025-02-20 16:56:21 +01:00
|
|
|
'disabled_help' => 'Set PROVIDER_REICHELT_ENABLED env to 1'
|
2025-02-19 00:44:49 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getProviderKey(): string
|
|
|
|
{
|
|
|
|
return 'reichelt';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isActive(): bool
|
|
|
|
{
|
2025-02-20 15:59:35 +01:00
|
|
|
return $this->enabled;
|
2025-02-19 00:44:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function searchByKeyword(string $keyword): array
|
|
|
|
{
|
2025-02-20 15:59:35 +01:00
|
|
|
$response = $this->client->request('GET', sprintf($this->getBaseURL() . '/shop/search/%s', $keyword));
|
2025-02-19 00:44:49 +01:00
|
|
|
$html = $response->getContent();
|
|
|
|
|
|
|
|
//Parse the HTML and return the results
|
|
|
|
$dom = new Crawler($html);
|
|
|
|
//Iterate over all div.al_gallery_article elements
|
|
|
|
$results = [];
|
|
|
|
$dom->filter('div.al_gallery_article')->each(function (Crawler $element) use (&$results) {
|
|
|
|
|
2025-02-19 23:55:58 +01:00
|
|
|
//Extract product id from data-product attribute
|
|
|
|
$artId = json_decode($element->attr('data-product'), true, 2, JSON_THROW_ON_ERROR)['artid'];
|
|
|
|
|
2025-02-19 00:44:49 +01:00
|
|
|
$productID = $element->filter('meta[itemprop="productID"]')->attr('content');
|
|
|
|
$name = $element->filter('meta[itemprop="name"]')->attr('content');
|
|
|
|
$sku = $element->filter('meta[itemprop="sku"]')->attr('content');
|
|
|
|
|
|
|
|
//Try to extract a picture URL:
|
|
|
|
$pictureURL = $element->filter("div.al_artlogo img")->attr('src');
|
|
|
|
|
|
|
|
$results[] = new SearchResultDTO(
|
|
|
|
provider_key: $this->getProviderKey(),
|
2025-02-19 23:55:58 +01:00
|
|
|
provider_id: $artId,
|
2025-02-19 00:44:49 +01:00
|
|
|
name: $productID,
|
|
|
|
description: $name,
|
|
|
|
category: null,
|
|
|
|
manufacturer: $sku,
|
|
|
|
preview_image_url: $pictureURL,
|
|
|
|
provider_url: $element->filter('a.al_artinfo_link')->attr('href')
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDetails(string $id): PartDetailDTO
|
|
|
|
{
|
2025-02-19 23:55:58 +01:00
|
|
|
//Check that the ID is a number
|
|
|
|
if (!is_numeric($id)) {
|
|
|
|
throw new \InvalidArgumentException("Invalid ID");
|
|
|
|
}
|
|
|
|
|
|
|
|
//Use this endpoint to resolve the artID to a product page
|
2025-02-20 15:59:35 +01:00
|
|
|
$response = $this->client->request('GET',
|
|
|
|
sprintf(
|
|
|
|
'https://www.reichelt.com/?ACTION=514&id=74&article=%s&LANGUAGE=%s&CCOUNTRY=%s',
|
|
|
|
$id,
|
|
|
|
strtoupper($this->language),
|
|
|
|
strtoupper($this->country)
|
|
|
|
)
|
|
|
|
);
|
2025-02-19 23:55:58 +01:00
|
|
|
$json = $response->toArray();
|
|
|
|
|
|
|
|
//Retrieve the product page from the response
|
|
|
|
$productPage = $this->getBaseURL() . '/shop/product' . $json[0]['article_path'];
|
|
|
|
|
|
|
|
|
2025-02-20 16:29:37 +01:00
|
|
|
$response = $this->client->request('GET', $productPage, [
|
|
|
|
'query' => [
|
|
|
|
'CCTYPE' => $this->includeVAT ? 'private' : 'business',
|
2025-02-20 16:37:13 +01:00
|
|
|
'currency' => $this->currency,
|
2025-02-20 16:29:37 +01:00
|
|
|
],
|
|
|
|
]);
|
2025-02-19 23:55:58 +01:00
|
|
|
$html = $response->getContent();
|
|
|
|
$dom = new Crawler($html);
|
|
|
|
|
|
|
|
//Extract the product notes
|
|
|
|
$notes = $dom->filter('p[itemprop="description"]')->html();
|
|
|
|
|
|
|
|
//Extract datasheets
|
|
|
|
$datasheets = [];
|
|
|
|
$dom->filter('div.articleDatasheet a')->each(function (Crawler $element) use (&$datasheets) {
|
|
|
|
$datasheets[] = new FileDTO($element->attr('href'), $element->filter('span')->text());
|
|
|
|
});
|
|
|
|
|
2025-02-20 16:29:37 +01:00
|
|
|
//Determine price for one unit
|
|
|
|
$priceString = $dom->filter('meta[itemprop="price"]')->attr('content');
|
|
|
|
$currency = $dom->filter('meta[itemprop="priceCurrency"]')->attr('content', 'EUR');
|
|
|
|
|
2025-02-20 00:32:03 +01:00
|
|
|
//Create purchase info
|
|
|
|
$purchaseInfo = new PurchaseInfoDTO(
|
|
|
|
distributor_name: self::DISTRIBUTOR_NAME,
|
|
|
|
order_number: $json[0]['article_artnr'],
|
2025-02-20 16:33:37 +01:00
|
|
|
prices: array_merge(
|
|
|
|
[new PriceDTO(1.0, $priceString, $currency, $this->includeVAT)]
|
|
|
|
, $this->parseBatchPrices($dom, $currency)),
|
2025-02-20 00:32:03 +01:00
|
|
|
product_url: $productPage
|
|
|
|
);
|
|
|
|
|
2025-02-19 23:55:58 +01:00
|
|
|
//Create part object
|
|
|
|
return new PartDetailDTO(
|
|
|
|
provider_key: $this->getProviderKey(),
|
|
|
|
provider_id: $id,
|
|
|
|
name: $json[0]['article_artnr'],
|
|
|
|
description: $json[0]['article_besch'],
|
2025-02-20 00:24:46 +01:00
|
|
|
category: $this->parseCategory($dom),
|
2025-02-19 23:55:58 +01:00
|
|
|
manufacturer: $json[0]['manufacturer_name'],
|
2025-02-20 15:39:35 +01:00
|
|
|
mpn: $this->parseMPN($dom),
|
2025-02-19 23:55:58 +01:00
|
|
|
preview_image_url: $json[0]['article_picture'],
|
|
|
|
provider_url: $productPage,
|
|
|
|
notes: $notes,
|
2025-02-20 00:18:05 +01:00
|
|
|
datasheets: $datasheets,
|
2025-02-20 00:32:03 +01:00
|
|
|
parameters: $this->parseParameters($dom),
|
|
|
|
vendor_infos: [$purchaseInfo]
|
2025-02-19 23:55:58 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2025-02-20 20:35:00 +01:00
|
|
|
private function parseMPN(Crawler $dom): string
|
2025-02-20 15:39:35 +01:00
|
|
|
{
|
|
|
|
//Find the small element directly after meta[itemprop="url"] element
|
|
|
|
$element = $dom->filter('meta[itemprop="url"] + small');
|
|
|
|
//If the text contains GTIN text, take the small element afterwards
|
|
|
|
if (str_contains($element->text(), 'GTIN')) {
|
|
|
|
$element = $dom->filter('meta[itemprop="url"] + small + small');
|
|
|
|
}
|
|
|
|
|
|
|
|
//The MPN is contained in the span inside the element
|
|
|
|
return $element->filter('span')->text();
|
|
|
|
}
|
|
|
|
|
2025-02-20 16:29:37 +01:00
|
|
|
private function parseBatchPrices(Crawler $dom, string $currency): array
|
2025-02-20 15:14:59 +01:00
|
|
|
{
|
|
|
|
//Iterate over each a.inline-block element in div.discountValue
|
|
|
|
$prices = [];
|
2025-02-20 16:29:37 +01:00
|
|
|
$dom->filter('div.discountValue a.inline-block')->each(function (Crawler $element) use (&$prices, $currency) {
|
2025-02-20 15:14:59 +01:00
|
|
|
//The minimum amount is the number in the span.block element
|
|
|
|
$minAmountText = $element->filter('span.block')->text();
|
|
|
|
|
|
|
|
//Extract a integer from the text
|
|
|
|
$matches = [];
|
|
|
|
if (!preg_match('/\d+/', $minAmountText, $matches)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$minAmount = (int) $matches[0];
|
|
|
|
|
|
|
|
//The price is the text of the p.productPrice element
|
|
|
|
$priceString = $element->filter('p.productPrice')->text();
|
|
|
|
//Replace comma with dot
|
|
|
|
$priceString = str_replace(',', '.', $priceString);
|
|
|
|
//Strip any non-numeric characters
|
|
|
|
$priceString = preg_replace('/[^0-9.]/', '', $priceString);
|
|
|
|
|
2025-02-20 16:29:37 +01:00
|
|
|
$prices[] = new PriceDTO($minAmount, $priceString, $currency, $this->includeVAT);
|
2025-02-20 15:14:59 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return $prices;
|
|
|
|
}
|
|
|
|
|
2025-02-20 00:24:46 +01:00
|
|
|
|
|
|
|
private function parseCategory(Crawler $dom): string
|
|
|
|
{
|
|
|
|
// Look for ol.breadcrumb and iterate over the li elements
|
|
|
|
$category = '';
|
|
|
|
$dom->filter('ol.breadcrumb li.triangle-left')->each(function (Crawler $element) use (&$category) {
|
|
|
|
//Do not include the .breadcrumb-showmore element
|
|
|
|
if ($element->attr('id') === 'breadcrumb-showmore') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$category .= $element->text() . ' -> ';
|
|
|
|
});
|
|
|
|
//Remove the trailing ' -> '
|
|
|
|
$category = substr($category, 0, -4);
|
|
|
|
|
|
|
|
return $category;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Crawler $dom
|
|
|
|
* @return ParameterDTO[]
|
|
|
|
*/
|
2025-02-20 00:18:05 +01:00
|
|
|
private function parseParameters(Crawler $dom): array
|
|
|
|
{
|
|
|
|
$parameters = [];
|
|
|
|
//Iterate over each ul.articleTechnicalData which contains the specifications of each group
|
|
|
|
$dom->filter('ul.articleTechnicalData')->each(function (Crawler $groupElement) use (&$parameters) {
|
|
|
|
$groupName = $groupElement->filter('li.articleTechnicalHeadline')->text();
|
|
|
|
|
|
|
|
//Iterate over each second li in ul.articleAttribute, which contains the specifications
|
|
|
|
$groupElement->filter('ul.articleAttribute li:nth-child(2n)')->each(function (Crawler $specElement) use (&$parameters, $groupName) {
|
2025-02-20 19:43:46 +01:00
|
|
|
$parameters[] = ParameterDTO::parseValueIncludingUnit(
|
2025-02-20 00:18:05 +01:00
|
|
|
name: $specElement->previousAll()->text(),
|
|
|
|
value: $specElement->text(),
|
|
|
|
group: $groupName
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return $parameters;
|
|
|
|
}
|
|
|
|
|
2025-02-19 23:55:58 +01:00
|
|
|
private function getBaseURL(): string
|
|
|
|
{
|
|
|
|
//Without the trailing slash
|
2025-02-20 15:59:35 +01:00
|
|
|
return 'https://www.reichelt.com/' . strtolower($this->country) . '/' . strtolower($this->language);
|
2025-02-19 00:44:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getCapabilities(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
ProviderCapabilities::BASIC,
|
|
|
|
ProviderCapabilities::PICTURE,
|
|
|
|
ProviderCapabilities::DATASHEET,
|
|
|
|
ProviderCapabilities::PRICE,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|