Skip to content

Commit

Permalink
#156 - add Sixt provider (#172)
Browse files Browse the repository at this point in the history
* Sixt provider

* update Sixt provider

* fix Guzzle

* update composer.json

* update composer.lock

* update phpunit.xml
  • Loading branch information
AleksandraKozubal authored Nov 28, 2023
1 parent 40335dc commit fddc517
Show file tree
Hide file tree
Showing 12 changed files with 523 additions and 412 deletions.
77 changes: 77 additions & 0 deletions app/Importers/SixtDataImporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace App\Importers;

use GuzzleHttp\Exception\GuzzleException;
use Symfony\Component\DomCrawler\Crawler;

class SixtDataImporter extends DataImporter
{
protected Crawler $sections;

public function extract(): static
{
try {
$headers = [
"User-Agent" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
];
$response = $this->client->get("https://www.sixt.com/share/e-scooter/#/", ["headers" => $headers]);
$html = $response->getBody()->getContents();
} catch (GuzzleException) {
$this->createImportInfoDetails("400", self::getProviderName());

$this->stopExecution = true;

return $this;
}

$crawler = new Crawler($html);

$this->sections = $crawler->filter("div.item div.middle div.content ul");

if ($this->sections->count() === 0) {
$this->createImportInfoDetails("204", self::getProviderName());
$this->stopExecution = true;
}

return $this;
}

public function transform(): void
{
if ($this->stopExecution) {
return;
}

$existingCityProviders = [];
$countryName = "";

foreach ($this->sections as $section) {
$node = $section->parentNode->parentNode->parentNode;

foreach ($node->childNodes as $country) {
if ($country instanceof \DOMElement) {
$class = $country->getAttribute("class");

if ($class === "title") {
$countryName = trim(preg_replace("/[^a-zA-Z ]/", "", $country->nodeValue));
}
}
}

foreach ($section->childNodes as $city) {
if ($city->nodeName === "li") {
$cityName = trim(preg_replace('/\s+/', "", $city->nodeValue));
$provider = $this->load($cityName, $countryName);

if ($provider !== "") {
$existingCityProviders[] = $provider;
}
}
}
}
$this->deleteMissingProviders(self::getProviderName(), $existingCityProviders);
}
}
15 changes: 15 additions & 0 deletions app/Jobs/SixtDataImporterJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace App\Jobs;

use App\Importers\SixtDataImporter;

class SixtDataImporterJob extends DataImporterJob
{
public function handle(SixtDataImporter $importer): void
{
$importer->setImportInfo($this->importInfoId)->extract()->transform();
}
}
2 changes: 2 additions & 0 deletions app/Services/DataImporterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use App\Jobs\NeuronDataImporterJob;
use App\Jobs\QuickDataImporterJob;
use App\Jobs\RydeDataImporterJob;
use App\Jobs\SixtDataImporterJob;
use App\Jobs\SpinDataImporterJob;
use App\Jobs\TierDataImporterJob;
use App\Jobs\UrentDataImporterJob;
Expand Down Expand Up @@ -65,6 +66,7 @@ public function run(string $whoRunsIt = "admin"): void
new WheeMoveDataImporterJob($this->importInfoId),
new HopDataImporterJob($this->importInfoId),
new ZwingsDataImporterJob($this->importInfoId),
new SixtDataImporterJob($this->importInfoId),
])->finally(function (): void {
ImportInfo::query()->where("id", $this->importInfoId)->update([
"status" => "finished",
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"sentry/sentry-laravel": "^3.7",
"spatie/laravel-permission": "^5.10",
"stichoza/google-translate-php": "^5.1",
"symfony/dom-crawler": "^6.3"
"symfony/dom-crawler": "^6.3",
"ext-dom": "*"
},
"require-dev": {
"blumilksoftware/codestyle": "v2.5.0",
Expand Down
Loading

0 comments on commit fddc517

Please sign in to comment.