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

#214 - restructure database for different types of vehicles #224

Merged
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
14 changes: 14 additions & 0 deletions app/Enums/ServicesEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace App\Enums;

enum ServicesEnum: string
{
case Bike = "bike";
case Cargo = "cargo";
case Emoped = "emoped";
case Escooter = "escooter";
case Motorscooter = "motorscooter";
}
3 changes: 3 additions & 0 deletions app/Exceptions/ExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public function register(): void
});
}

/**
* @noinspection PhpParameterNameChangedDuringInheritanceInspection
*/
public function render($request, Throwable $exception)
{
if ($exception instanceof ValidationException) {
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Api/CityProviderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function index(): JsonResponse
->sortByDesc(fn(City $city): int => $city->cityProviders->count()),
);

$providers = ProviderResource::collection(Provider::all()->sortBy("name"));
$providers = ProviderResource::collection(Provider::all());
$countries = Country::whereHas("cities.cityProviders")
->with(["cities.cityAlternativeNames", "cities.cityProviders"])
->get()
Expand Down
81 changes: 36 additions & 45 deletions app/Importers/BeamDataImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Importers;

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

Expand All @@ -18,12 +19,10 @@ public function extract(): static
$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.find-beam-box");

Expand All @@ -37,58 +36,50 @@ public function extract(): static

public function transform(): void
{
$url = "https://assets-global.website-files.com/63c4acbedbab5dea8b1b98cd/63d8a5b60da91e7d71298637_map-vehicle-saturn.png";
$escooterImageUrl = "https://assets-global.website-files.com/63c4acbedbab5dea8b1b98cd/63d8a5b60da91e7d71298637_map-vehicle-saturn.png";
$bikeImageUrl = "https://assets-global.website-files.com/63c4acbedbab5dea8b1b98cd/63d8a5b7f06e4a42de02241b_map-vehicle-saturn-apollo.png";

if ($this->stopExecution) {
return;
}

$existingCityProviders = [];

foreach ($this->sections as $section) {
foreach ($section->childNodes as $node) {
if ($node->nodeName === "h4") {
$countryName = $node->nodeValue;
$crawler = new Crawler($section);
$countryName = $crawler->filter('h4[class="find-beam-title-map"]')->text();

$columns = $crawler->filter('div[class="beam-col"], div[class="beam-col-main-box"]');

foreach ($columns as $column) {
$columnCrawler = new Crawler($column);
$services = [];
$images = $columnCrawler->filter("img");

foreach ($images as $image) {
if ($image->getAttribute("src") === $escooterImageUrl) {
$services[] = ServicesEnum::Escooter;
} else if ($image->getAttribute("src") === $bikeImageUrl) {
$services[] = ServicesEnum::Bike;
}
}

if ($node->nodeName === "div") {
foreach ($node->childNodes as $div) {
if ($div->nodeName === "div") {
foreach ($div->childNodes as $city) {
if ($city->nodeName === "img" && $city->getAttribute("src") === $url) {
$hasEscooters = true;
} elseif ($city->nodeName === "img" && $city->getAttribute("src") !== $url) {
$hasEscooters = false;
}

if ($city->nodeName === "p" && $hasEscooters === true) {
$search = ["\u{00A0}", "\u{200D}", "Prefecture"];
$valueToDelete = "Selangor";
$cityName = preg_replace('/[\p{Hiragana}\p{Katakana}\p{Han}]+/u', "", $city->nodeValue);
$cityName = str_replace($search, "", $cityName);
$cityName = preg_replace('/(?<=[^\s_\-])(?=[A-Z])/', " ", $city->nodeValue);
$arrayOfCitiesNames = explode(" ", $cityName);
$arrayOfCitiesNames = array_filter($arrayOfCitiesNames, fn($value) => $value !== $valueToDelete);
$arrayOfCitiesNames = array_filter($arrayOfCitiesNames, fn($record) => strlen($record) > 1);
$arrayOfCitiesNames = array_filter($arrayOfCitiesNames, fn($record) => strpos($record, "") === false);

foreach ($arrayOfCitiesNames as $cityName) {
if ($cityName !== "Selangor") {
$cityName = trim($cityName);

if ($countryName === "Korea") {
$countryName = "South Korea";
}
$provider = $this->load($cityName, $countryName);

if ($provider !== "") {
$existingCityProviders[] = $provider;
}
}
}
}
}
}
$cityNamesHtml = $columnCrawler->filter("p")->html();
$cityNamesArray = explode("<br>", $cityNamesHtml);

foreach ($cityNamesArray as $cityName) {
$cityName = strip_tags($cityName);
$search = ["&nbsp;•", "&nbsp;"];
$cityName = str_replace($search, "", $cityName);
$cityName = trim($cityName);

if ($countryName === "Korea") {
$countryName = "South Korea";
}

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

if ($provider) {
$existingCityProviders[] = $provider;
}
}
}
Expand Down
110 changes: 61 additions & 49 deletions app/Importers/BerylDataImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Importers;

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

Expand All @@ -27,7 +28,7 @@ public function extract(): static
}

$crawler = new Crawler($html);
$this->sections = $crawler->filter("div > section > div .inner");
$this->sections = $crawler->filter('div[class="view-content col-xs-12 col-sm-12"]')->filter("div[class='views-row']");

if (count($this->sections) === 0) {
$this->createImportInfoDetails("204", self::getProviderName());
Expand All @@ -46,58 +47,69 @@ public function transform(): void
$existingCityProviders = [];

foreach ($this->sections as $section) {
foreach ($section->childNodes as $node) {
if ($node->nodeName === "div") {
foreach ($node->childNodes as $city) {
if ($city->nodeName === "h3") {
$cityName = $city->nodeValue;
}

$eScootersFound = false;

if ($city->nodeValue === "e-Scooters") {
$eScootersFound = true;
}

if ($eScootersFound) {
$cityName = str_replace(["E-scooters", "and ", ","], "", $cityName);
$cityName = trim($cityName);

if ($cityName === "Bournemouth Christchurch Poole") {
$arrayOfCityNames = explode(" ", $cityName);

foreach ($arrayOfCityNames as $cityName) {
$provider = $this->load($cityName, self::COUNTRY_NAME);

if ($provider !== "") {
$existingCityProviders[] = $provider;
}
}
} elseif ($cityName === "West Midlands") {
$cityName = "Birmingham";
}

if ($cityName === "Isle of Wight") {
$arrayOfCityNames = ["Cowes", "East Cowes", "Newport", "Ryde", "Sandown", "Shanklin"];

foreach ($arrayOfCityNames as $cityName) {
$provider = $this->load($cityName, self::COUNTRY_NAME);

if ($provider !== "") {
$existingCityProviders[] = $provider;
}
}
}
$provider = $this->load($cityName, self::COUNTRY_NAME);

if ($provider !== "") {
$existingCityProviders[] = $provider;
}
}
$crawler = new Crawler($section);
$cityName = $crawler->filter("h3")->text();
$this->data[] = $cityName;

$servicesNodes = $crawler->filter('div[class="field--label"]');
$availableServices = [];

foreach ($servicesNodes as $serviceNode) {
$serviceCrawler = new Crawler($serviceNode);
$service = $serviceCrawler->text();

if ($service === "e-Bikes") {
$availableServices[] = ServicesEnum::Bike;
}

if ($service === "e-Scooters") {
$availableServices[] = ServicesEnum::Escooter;
}

if ($service === "Cargo") {
$availableServices[] = ServicesEnum::Cargo;
}
}

if ($cityName === "Bournemouth Christchurch Poole") {
$arrayOfCityNames = explode(" ", $cityName);

foreach ($arrayOfCityNames as $cityName) {
$provider = $this->load($cityName, self::COUNTRY_NAME, $lat = "", $long = "", $availableServices);

if ($provider !== "") {
$existingCityProviders[] = $provider;
}
}
} elseif ($cityName === "West Midlands") {
$cityName = "Birmingham";
} elseif ($cityName === "Isle of Wight") {
$arrayOfCityNames = ["Cowes", "East Cowes", "Newport", "Ryde", "Sandown", "Shanklin"];

foreach ($arrayOfCityNames as $cityName) {
$provider = $this->load($cityName, self::COUNTRY_NAME, $lat = "", $long = "", $availableServices);

if ($provider !== "") {
$existingCityProviders[] = $provider;
}
}
}
$provider = $this->load($cityName, self::COUNTRY_NAME, $lat = "", $long = "", $availableServices);

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

$this->deleteMissingProviders(self::getProviderName(), $existingCityProviders);
}

public function test()
{
$this->extract()->transform();
$sectionCount = count($this->sections);
$this->data[] = ["sections" => "$sectionCount"];

return $this->data;
}
}
9 changes: 5 additions & 4 deletions app/Importers/BirdDataImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Importers;

use App\Enums\ServicesEnum;
use App\Exceptions\MapboxGeocodingServiceException;
use App\Models\City;
use App\Models\CityAlternativeName;
Expand Down Expand Up @@ -57,7 +58,7 @@ public function transform(): void

protected function parseData(string $html): array
{
$pattern = '/let features = \[([\s\S]*?)\];/';
$pattern = '/let features = \[([\s\S]*?)];/';

if (preg_match($pattern, $html, $matches)) {
$fetchedData = $matches[1];
Expand All @@ -75,15 +76,15 @@ protected function parseData(string $html): array
return array_map("trim", $coordinates);
}

protected function load(string $cityName, string $countryName, string $lat = "", string $long = ""): string
protected function load(string $cityName, string $countryName, string $lat = "", string $long = "", array $services = [ServicesEnum::Escooter]): string
{
$city = City::query()->where("name", $cityName)->first();
$alternativeCityName = CityAlternativeName::query()->where("name", $cityName)->first();

if ($city || $alternativeCityName) {
$cityId = $city ? $city->id : $alternativeCityName->city_id;

$this->createProvider($cityId, self::getProviderName());
$this->createProvider($cityId, self::getProviderName(), $services);

return strval($cityId);
}
Expand All @@ -97,7 +98,7 @@ protected function load(string $cityName, string $countryName, string $lat = "",
"country_id" => $country->id,
]);

$this->createProvider($city->id, self::getProviderName());
$this->createProvider($city->id, self::getProviderName(), $services);

return strval($city->id);
}
Expand Down
Loading
Loading