Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#156 - add Sixt provider #172

Merged
merged 8 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
kamilpiech97 marked this conversation as resolved.
Show resolved Hide resolved
$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
2 changes: 2 additions & 0 deletions database/seeders/ProviderSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use App\Importers\NeuronDataImporter;
use App\Importers\QuickDataImporter;
use App\Importers\RydeDataImporter;
use App\Importers\SixtDataImporter;
use App\Importers\SpinDataImporter;
use App\Importers\TierDataImporter;
use App\Importers\UrentDataImporter;
Expand Down Expand Up @@ -48,6 +49,7 @@ public function run(): void
["name" => NeuronDataImporter::getProviderName(), "color" => "#445261"],
["name" => QuickDataImporter::getProviderName(), "color" => "#009ac7"],
["name" => SpinDataImporter::getProviderName(), "color" => "#ff5436"],
["name" => SixtDataImporter::getProviderName(), "color" => "#f25a04"],
["name" => TierDataImporter::getProviderName(), "color" => "#0E1A50"],
["name" => VoiDataImporter::getProviderName(), "color" => "#f46c63"],
["name" => UrentDataImporter::getProviderName(), "color" => "#9400FF"],
Expand Down
Binary file added public/providers/sixt.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading