Skip to content

Commit

Permalink
Park4NightService: added processing description
Browse files Browse the repository at this point in the history
  • Loading branch information
DJTommek committed Aug 19, 2024
1 parent c6a4f1e commit 5ac29cf
Show file tree
Hide file tree
Showing 6 changed files with 439 additions and 7 deletions.
31 changes: 31 additions & 0 deletions src/libs/BetterLocation/Service/Park4NightService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\BetterLocation\BetterLocation;
use App\Config;
use App\Utils\Requestor;
use App\Utils\StringUtils;
use App\Utils\Utils;
use DJTommek\Coordinates\Coordinates;
use DJTommek\Coordinates\CoordinatesInterface;
Expand Down Expand Up @@ -59,6 +60,12 @@ public function process(): void
$placeName,
));
}

$description = self::extractDescription($finder);
if ($description !== null) {
$location->addDescription($description);
}

$this->collection->add($location);
}

Expand All @@ -79,4 +86,28 @@ private static function extractCoordinates(string $link): CoordinatesInterface
$coordsRaw = $googleNavigationUrl->getQueryParameter('destination');
return Coordinates::fromString($coordsRaw);
}

private static function extractDescription(\DOMXPath $finder): ?string
{
$xpaths = [
'//div[contains(@class, "place-info-description")]/p[@lang="en"]', // English
'//div[contains(@class, "place-info-description")]/p', // First available language
];

foreach ($xpaths as $xpath) {
$placeDescriptionEn = $finder->query($xpath);
if ($placeDescriptionEn->count() === 0) {
continue;
}

$description = $placeDescriptionEn->item(0)->textContent;
$description = htmlspecialchars($description);
$description = StringUtils::replaceNewlines($description, '');
if (mb_strlen($description) > 250) {
$description = trim(mb_substr($description, 0, 200)) . StringUtils::ELLIPSIS;
}
return $description;
}
return null;
}
}
2 changes: 2 additions & 0 deletions src/libs/Utils/StringUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class StringUtils
public const NEWLINE_UNIX = "\n";
public const NEWLINE_MAC = "\r";

public const ELLIPSIS = '&#8230';

/** Replace or remove some characters */
public static function translit(string $text): string
{
Expand Down
38 changes: 31 additions & 7 deletions tests/BetterLocation/Service/Park4NightServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,51 +56,75 @@ public static function processProvider(): array
50.092859,
14.429902,
'<a href="https://park4night.com/en/place/536148">park4night</a> - <a href="https://park4night.com/en/place/536148">(110 00) Petrská čtvrť - 7 Novomlýnská</a>',
[],
[
'Quiet and safe place',
],
'https://park4night.com/en/place/536148',
],
[
46.664101,
11.1591,
'<a href="https://park4night.com/en/place/12960">park4night</a> - <a href="https://park4night.com/en/place/12960">(39012) Campeggio di Merano | Live Merano Camping ****</a>',
[],
[
'Well located campsite, very large pitches, mostly shaded. New and clean toilets. Very friendly welcome. Reopened after renovation on September 14, 2020. Open all year round.',
],
'https://park4night.com/en/place/12960',
],
[
46.664101,
11.1591,
'<a href="https://park4night.com/en/lieu/12960/open/">park4night</a> - <a href="https://park4night.com/en/place/12960">(39012) Campeggio di Merano | Live Merano Camping ****</a>',
[],
[
'Well located campsite, very large pitches, mostly shaded. New and clean toilets. Very friendly welcome. Reopened after renovation on September 14, 2020. Open all year round.',
],
'https://park4night.com/en/lieu/12960/open/',
],
[
50.05665,
14.41376,
'<a href="https://park4night.com/en/place/539465">park4night</a> - <a href="https://park4night.com/en/place/539465">(150 00) - 30 Císařská louka</a>',
[],
[
// English language is not available
'Parkeerplaats waar camperen gedoogd wordt. 30 kronen per uur maar er is geen betaalmogelijkheid of instructie hoe of waar dat te voldoen. En geen controle. Camping ernaast heeft leuk restaurant 2 lounge met uitzicht op de Moldau en zwemsteigertje.',
],
'https://park4night.com/en/place/539465',
],
[
-32.880737,
-54.448637,
'<a href="https://park4night.com/en/place/525875">park4night</a> - <a href="https://park4night.com/en/place/525875">(33000) Posada El Capricho</a>',
[],
[
'Camping of the Posada El Capricho, next to the entrance to the Quebrada de los Cuervos. It is a private, wild campsite, with little mobile signal. All the services. They allow pets, and that is what d&#8230',
],
'https://park4night.com/en/place/525875',
],
[
42.555554,
27.426904,
'<a href="https://park4night.com/en/place/327177">park4night</a> - <a href="https://park4night.com/en/place/327177">() Burgas - E871</a>',
[],
[
'Rompetrol station with a fountain that has a water tap. We filled a tank; no idea if it&#039;s good for drinking though.',
],
'https://park4night.com/en/place/327177',
],
[
42.5322,
27.46926,
'<a href="https://park4night.com/en/place/434138">park4night</a> - <a href="https://park4night.com/en/place/434138">(8008) Burgas - 8000 булевард „Транспортна“</a>',
[],
[
'Gas station with water tap. I do not know if you can drink it. Ask workers where to find the tap. It is behind by the building.',
],
'https://park4night.com/en/place/434138',
],
[ // Extra long description including newlines
50.924343,
13.855146,
'<a href="https://park4night.com/en/place/186610">park4night</a> - <a href="https://park4night.com/en/place/186610">(01809) Stellplatz Abendruhe</a>',
[
'***We are open and look forward to the 2024 camping season***Please register by phone | SMS | WhatsApp.Please arrive by 9 p.m., departure is possible at any time.Our pitch is on private property - sec&#8230',
],
'https://park4night.com/en/place/186610',
],
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
200
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Date":["Mon, 19 Aug 2024 19:37:48 GMT"],"Server":["Apache"],"Expires":["Thu, 19 Nov 1981 08:52:00 GMT"],"Cache-Control":["no-store, no-cache, must-revalidate"],"Pragma":["no-cache"],"Set-Cookie":["PHPSESSID=km2ce15hjviiicrtcn9lpr01lm; path=/; domain=.park4night.com"],"Upgrade":["h2,h2c"],"Connection":["Upgrade, close"],"Vary":["Accept-Encoding,User-Agent"],"Transfer-Encoding":["chunked"],"Content-Type":["text/html; charset=UTF-8"]}
Loading

0 comments on commit 5ac29cf

Please sign in to comment.