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

#17 - add Tier provider #116

Merged
merged 3 commits into from
Aug 16, 2023
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
1 change: 0 additions & 1 deletion app/Importers/DataImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ protected function load(string $cityName, string $countryName, string $lat = "",
if (!$countCoordinates) {
$this->createImportInfoDetails("419", self::getProviderName());
}

$city = City::query()->create([
"name" => $cityName,
"latitude" => ($countCoordinates > 0) ? $coordinates[0] : null,
Expand Down
71 changes: 71 additions & 0 deletions app/Importers/TierDataImporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace App\Importers;

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

class TierDataImporter extends DataImporter
{
protected Crawler $sections;
protected string $countryName;

public function extract(): static
{
try {
$response = $this->client->get("https://www.tier.app/en/where-to-find-us");
$html = $response->getBody()->getContents();
} catch (GuzzleException) {
$this->createImportInfoDetails("400", self::getProviderName());

$this->stopExecution = true;

return $this;
}

$crawler = new Crawler($html);
$this->sections = $crawler->filter("main div.relative section.Accordion__AccordionWrapper-sc-ehu24-0.fvluSp>li > div.items-center");

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

$this->stopExecution = true;
}

return $this;
}

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

$existingCityProviders = [];

foreach ($this->sections as $section) {
foreach ($section->childNodes as $node) {
if ($node->nodeName === "h5") {
$this->countryName = trim($node->nodeValue);

continue 2;
}

foreach ($node->childNodes as $childNode) {
if (trim($childNode->nodeValue) !== ""){
$cityName = trim($childNode->nodeValue);
$provider = $this->load($cityName, $this->countryName);

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

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

class TierDataImporterJob extends DataImporterJob
{
public function handle(TierDataImporter $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 @@ -14,6 +14,7 @@
use App\Jobs\QuickDataImporterJob;
use App\Jobs\RydeDataImporterJob;
use App\Jobs\SpinDataImporterJob;
use App\Jobs\TierDataImporterJob;
use App\Jobs\UrentDataImporterJob;
use App\Jobs\VoiDataImporterJob;
use App\Jobs\ZwingsDataImporterJob;
Expand Down Expand Up @@ -44,6 +45,7 @@ public function run(string $whoRunsIt = "admin"): void
new QuickDataImporterJob($this->importInfoId),
new RydeDataImporterJob($this->importInfoId),
new SpinDataImporterJob($this->importInfoId),
new TierDataImporterJob($this->importInfoId),
new UrentDataImporterJob($this->importInfoId),
new VoiDataImporterJob($this->importInfoId),
new ZwingsDataImporterJob($this->importInfoId),
Expand Down
5 changes: 2 additions & 3 deletions app/Services/MapboxGeocodingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ public function getCoordinatesFromApi(string $cityName, string $countryName): ar
);

$coordinates = json_decode($response->getBody()->getContents(), associative: true)["features"][0]["center"];

return [$coordinates[1], $coordinates[0]];
} catch (Throwable $exception) {
throw new MapboxGeocodingServiceException(previous: $exception);
}

return [$coordinates[1] ?? null, $coordinates[0] ?? null];
}

/**
Expand Down
2 changes: 2 additions & 0 deletions database/seeders/ProviderSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use App\Importers\QuickDataImporter;
use App\Importers\RydeDataImporter;
use App\Importers\SpinDataImporter;
use App\Importers\TierDataImporter;
use App\Importers\UrentDataImporter;
use App\Importers\VoiDataImporter;
use App\Importers\ZwingsDataImporter;
Expand All @@ -34,6 +35,7 @@ public function run(): void
["name" => NeuronDataImporter::getProviderName(), "color" => "#445261"],
["name" => QuickDataImporter::getProviderName(), "color" => "#009ac7"],
["name" => SpinDataImporter::getProviderName(), "color" => "#ff5436"],
["name" => TierDataImporter::getProviderName(), "color" => "#0E1A50"],
["name" => VoiDataImporter::getProviderName(), "color" => "#f46c63"],
["name" => UrentDataImporter::getProviderName(), "color" => "#9400FF"],
["name" => ZwingsDataImporter::getProviderName(), "color" => "#abb8c3"],
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ npm run lintf
```
Runs a queue that processes importers' jobs:
```
php artisan queue:work --queue="importers" --timeout=0
php artisan queue:work
```

### Containers
Expand Down