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

#236 - Add GoSharing provider #237

Merged
merged 2 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
113 changes: 113 additions & 0 deletions app/Importers/GoSharingDataImporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

declare(strict_types=1);

namespace App\Importers;

use App\Enums\ServicesEnum;
use GuzzleHttp\Exception\GuzzleException;
use Symfony\Component\DomCrawler\Crawler;

class GoSharingDataImporter extends DataImporter
{
protected Crawler $sections;

public function extract(): static
{
try {
$response = $this->client->get("https://nl.go-sharing.com/en/locations/");
$html = $response->getBody()->getContents();
} catch (GuzzleException) {
$this->createImportInfoDetails("400", self::getProviderName());

$this->stopExecution = true;

return $this;
}

$crawler = new Crawler($html);
$this->sections = $crawler->filter('section[id="show-cities"]');

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

return $this;
}

public function transform(): void
{
if ($this->stopExecution) {
return;
}
$existingCityProviders = [];
$countryNames = [];

foreach ($this->sections as $section) {
$sectionCrawler = new Crawler($section);
$countries = $sectionCrawler->filter("h3");

foreach ($countries as $country) {
$countryNames[] = $country->textContent;
}

$countryCitiesRows = $sectionCrawler->filter('div[class="row location-row"]');
$countryCitiesArray = [];

foreach ($countryCitiesRows as $countryCities) {
$locationsCrawler = new Crawler($countryCities);
$locations = $locationsCrawler->filter('div[class="location"]');

$citiesArray = [];

foreach ($locations as $location) {
$cityCrawler = new Crawler($location);
$cityName = $cityCrawler->text();

$activeServicesArray = [];
$activeServices = $cityCrawler->filter('div[class="location__icons__icon active"]');

foreach ($activeServices as $activeService) {
$serviceCrawler = new Crawler($activeService);
$serviceType = $serviceCrawler->filter("img")->attr("alt");

if ($serviceType === "E-Scooter") {
$activeServicesArray[] = ServicesEnum::Escooter;
}

if ($serviceType === "E-Bike") {
$activeServicesArray[] = ServicesEnum::Bike;
}
}

$citiesArray[] = [
"cityName" => $cityName,
"services" => $activeServicesArray,
];
}
$countryCitiesArray[] = $citiesArray;
}

$countryCount = count($countryNames);

for ($i = 0; $i < $countryCount; $i++) {
$countryName = $countryNames[$i];
$cities = $countryCitiesArray[$i];

foreach ($cities as $city) {
$cityName = $city["cityName"];
$services = $city["services"];

$provider = $this->load($cityName, $countryName, $lat = "", $long = "", $services);

if ($provider !== "") {
$existingCityProviders[] = $provider;
}
}
}

$this->deleteMissingProviders(self::getProviderName(), $existingCityProviders);
}
}
}
15 changes: 15 additions & 0 deletions app/Jobs/GoSharingDataImporterJob.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\GoSharingDataImporter;

class GoSharingDataImporterJob extends DataImporterJob
{
public function handle(GoSharingDataImporter $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 @@ -11,6 +11,7 @@
use App\Jobs\BitMobilityDataImporterJob;
use App\Jobs\BoltDataImporterJob;
use App\Jobs\DottDataImporterJob;
use App\Jobs\GoSharingDataImporterJob;
use App\Jobs\HopDataImporterJob;
use App\Jobs\HoppDataImporterJob;
use App\Jobs\HulajDataImporterJob;
Expand Down Expand Up @@ -68,6 +69,7 @@ public function run(string $whoRunsIt = "admin"): void
new WheeMoveDataImporterJob($this->importInfoId),
new HopDataImporterJob($this->importInfoId),
new ZwingsDataImporterJob($this->importInfoId),
new GoSharingDataImporterJob($this->importInfoId),
new SixtDataImporterJob($this->importInfoId),
])->finally(function (): void {
ImportInfo::query()->where("id", $this->importInfoId)->update([
Expand Down
2 changes: 2 additions & 0 deletions database/seeders/ProviderSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use App\Importers\BitMobilityDataImporter;
use App\Importers\BoltDataImporter;
use App\Importers\DottDataImporter;
use App\Importers\GoSharingDataImporter;
use App\Importers\HopDataImporter;
use App\Importers\HoppDataImporter;
use App\Importers\HulajDataImporter;
Expand Down Expand Up @@ -44,6 +45,7 @@ public function run(): void
["name" => BitMobilityDataImporter::getProviderName(), "color" => "#8da6e3", "url" => "https://bitmobility.it/en/", "android_url" => "https://play.google.com/store/apps/details?id=it.bitmobility.bit", "ios_url" => "https://apps.apple.com/it/app/bit/id1464155063"],
["name" => BoltDataImporter::getProviderName(), "color" => "#24f0a0", "url" => "https://bolt.eu/pl-pl/scooters/", "android_url" => "https://play.google.com/store/apps/details?id=ee.mtakso.client&hl=pl&gl=US", "ios_url" => "https://apps.apple.com/pl/app/bolt-przejazdy-hulajnogi/id675033630?l=pl"],
["name" => DottDataImporter::getProviderName(), "color" => "#f5c604", "url" => "https://ridedott.com/", "android_url" => "https://play.google.com/store/apps/details?id=com.ridedott.rider&pli=1", "ios_url" => "https://apps.apple.com/us/app/dott-unlock-your-city/id1440301673"],
["name" => GoSharingDataImporter::getProviderName(), "color" => "#81BB28", "url" => "https://nl.go-sharing.com/en/locations/", "android_url" => "https://play.google.com/store/apps/details?id=nl.gosharing.gourban.app&hl=pl&gl=US", "ios_url" => "https://apps.apple.com/pl/app/go-sharing/id1477465763?l=pl"],
["name" => HopDataImporter::getProviderName(), "color" => "#ea1821", "url" => "https://hop.bike/en/", "android_url" => "https://play.google.com/store/apps/details?id=com.hoplagit.rider&hl=pl&gl=US", "ios_url" => "https://apps.apple.com/pl/app/hop-enjoy-the-city/id1487640704?l=pl"],
["name" => HoppDataImporter::getProviderName(), "color" => "#1ce5be", "url" => "https://hopp.bike/", "android_url" => "https://play.google.com/store/apps/details?id=bike.hopp", "ios_url" => "https://apps.apple.com/us/app/hopp-scooters/id1471324642?ls=1"],
["name" => HulajDataImporter::getProviderName(), "color" => "#d6213f", "url" => "https://hulaj.eu/", "android_url" => "https://play.google.com/store/apps/details?id=eu.hulaj&hl=pl&gl=US"],
Expand Down
Binary file added public/providers/gosharing.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading