Skip to content

Commit

Permalink
Merge branch '#10-regulations-for-escooters' of https://github.com/bl…
Browse files Browse the repository at this point in the history
…umilksoftware/escooters into #10-regulations-for-escooters
  • Loading branch information
zmigroo committed Jan 13, 2024
2 parents 0d53f75 + 15af93e commit 6195273
Show file tree
Hide file tree
Showing 22 changed files with 552 additions and 181 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/RulesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function getRules($country, $city): array
->where("city_id", $city->id)
->first();

if(!$rules || !$rules->rulesENG || !$rules->rulesPL){
if(!$rules || $rules->rulesENG === null || $rules->rulesPL === null){
$cityData = [
"city_id" => $city->id,
"country_id" => $country_id,
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Resources/ProviderResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public function toArray(Request $request): array
return [
"name" => $this->name,
"url" => $this->url,
"android_url" => $this->android_url,
"ios_url" => $this->ios_url,
"color" => $this->color,
];
}
Expand Down
66 changes: 38 additions & 28 deletions app/Importers/LinkDataImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
class LinkDataImporter extends DataImporter
{
protected Crawler $sections;
private string $countryName = "";

public function extract(): static
{
Expand All @@ -26,7 +25,7 @@ public function extract(): static
}

$crawler = new Crawler($html);
$this->sections = $crawler->filter(".Main-content .sqs-row.row > .col p > strong");
$this->sections = $crawler->filter(".Main-content .sqs-row.row > .col p");

if (count($this->sections) === 0) {
$this->createImportInfoDetails("204", self::getProviderName());
Expand All @@ -39,48 +38,59 @@ public function extract(): static

public function transform(): void
{
$countryName = "";

$cityName = "";

$states = [
"Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida",
"Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine",
"Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska",
"Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota",
"Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee",
"Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming",
];

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

$existingCityProviders = [];

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

foreach ($node->parentNode->parentNode->parentNode->childNodes as $i => $cityName) {
if ($i === 0 || !trim($cityName->nodeValue)) {
continue;
}

$name = $cityName->nodeValue;
$skipFirstIteration = true;

$cities = [];
foreach ($this->sections as $section) {
if ($skipFirstIteration) {
$skipFirstIteration = false;

if (str_contains($name, "(") && str_contains($name, ")")) {
$names = explode("(", $name)[1];
$names = explode(")", $names)[0];
$names = explode(", ", $names);
continue;
}

foreach ($names as $name) {
$cities[] = str_replace("*", "", $name);
}
foreach ($section->childNodes as $node) {
if ($node->nodeName === "strong") {
if (!in_array($node->nodeValue, $states, true)) {
$countryName = trim($node->nodeValue);
} else {
$cities[] = $name;
$countryName = "United States";
}
}

foreach ($cities as $name) {
$provider = $this->load($name, $countryName);
if ($node->nodeName === "#text") {
$cityName = $node->nodeValue;
} else if ($node->nodeName === "a") {
$cityName = $node->nodeValue;
}

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

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

$this->deleteMissingProviders(self::getProviderName(), $existingCityProviders);
}
}
7 changes: 5 additions & 2 deletions app/Services/OpenAIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct()

public function importRulesForAllCities(bool $force): void
{
$cities = City::query()->whereHas('cityProviders')->get();
$cities = City::query()->whereHas('cityProviders')->orderBy('country.name')->get();
foreach ($cities as $city) {
$cityData = [
"city_id" => $city->id,
Expand Down Expand Up @@ -56,7 +56,7 @@ public function importRulesForCity(array $cityData, bool $force): array
$country_name = $cityData["country_name"];

$promptENG = "Act as a helpful assistant. Explain what are the legal limitations for riding electric scooters in $city_name, $country_name? Contain information about: max speed, helmet requirements, allowed ABV, passengers, other relevant details. Be formal, speak English. Don't include city name in your response. If you don't have information answering the question, write 'null'";
$promptPL = "Zachowuj się jako pomocny asystent, wyjaśnij jakie są prawa dotyczące jazdy na hulajnogach elektrycznych w $city_name, $country_name? Zawrzyj informacje o: maksymalnej prędkości, potrzebie kasku, dozwolonym alkoholu we krwi, pasażerach, inne. Bądź formalny, mów po polsku. Nie zawieraj nazwy miasta w odpowiedzi. Jeśli nie masz informacji odpowiadających na pytanie, napisz 'null'";
$promptPL = "Zachowuj się jako pomocny asystent. wyjaśnij jakie są prawa dotyczące jazdy na hulajnogach elektrycznych w $city_name, $country_name? Zawrzyj informacje o: maksymalnej prędkości, potrzebie kasku, dozwolonym alkoholu we krwi, pasażerach, inne. Bądź formalny, mów po polsku. Nie zawieraj nazwy miasta w odpowiedzi. Jeśli nie masz informacji odpowiadających na pytanie, napisz 'null'";

if (!$force) {
$currentRules = Rules::query()->where("city_id", $city_id)->where("country_id", $country_id)->first();
Expand Down Expand Up @@ -90,6 +90,9 @@ public function importRulesForCity(array $cityData, bool $force): array
];
}

$rulesENG = str_replace("\n", "<br>", $rulesENG);
$rulesPL = str_replace("\n", "<br>", $rulesPL);

Rules::query()->updateOrCreate([
"city_id" => $city_id,
"country_id" => $country_id,
Expand Down
Loading

0 comments on commit 6195273

Please sign in to comment.