* images added

* text cleanup fixes
This commit is contained in:
Dávid Danyi 2018-07-26 16:51:35 +02:00
parent 6fa6c1ae89
commit 0d017aeabc
4 changed files with 122 additions and 65 deletions

View File

@ -40,6 +40,6 @@ class NewsAction extends AbstractAction
{ {
$id = (int)$request->getAttribute(self::IDENTIFIER_NAME); $id = (int)$request->getAttribute(self::IDENTIFIER_NAME);
$authHeader = $request->getHeaderLine("x-passthru-auth"); $authHeader = $request->getHeaderLine("x-passthru-auth");
return new JsonResponse($this->skiesClient->setAuthHeader($authHeader)->getNewsItem($id), 200, [], 0); return new JsonResponse($this->skiesClient->setAuthHeader($authHeader)->getNewsItem($id));
} }
} }

View File

@ -6,49 +6,34 @@ use Doctrine\Common\Collections\ArrayCollection;
class Activity implements \JsonSerializable class Activity implements \JsonSerializable
{ {
/** /** @var int */
* @var int
*/
private $id; private $id;
/** /** @var string */
* @var string private $imageUrl;
*/
/** @var string */
private $name; private $name;
/** /** @var \DateTime */
* @var \DateTime
*/
private $date; private $date;
/** /** @var string */
* @var string
*/
private $description; private $description;
/** /** @var \DateTime */
* @var \DateTime
*/
private $finalEntry; private $finalEntry;
/** /** @var string */
* @var string
*/
private $accountable; private $accountable;
/** /** @var ArrayCollection|Comment[] */
* @var ArrayCollection|Comment[]
*/
private $comments; private $comments;
/** /** @var string[] */
* @var string[]
*/
private $signedUsers; private $signedUsers;
/** /** @var bool */
* @var bool
*/
private $isSignedup = false; private $isSignedup = false;
/** /**
@ -80,6 +65,24 @@ class Activity implements \JsonSerializable
return $this; return $this;
} }
/**
* @return string
*/
public function getImageUrl(): ?string
{
return $this->imageUrl;
}
/**
* @param string $imageUrl
* @return Activity
*/
public function setImageUrl(?string $imageUrl): Activity
{
$this->imageUrl = $imageUrl;
return $this;
}
/** /**
* @return string * @return string
*/ */
@ -287,6 +290,7 @@ class Activity implements \JsonSerializable
{ {
return [ return [
'id' => $this->getId(), 'id' => $this->getId(),
'imageUrl' => $this->getImageUrl(),
'name' => $this->getName(), 'name' => $this->getName(),
'date' => $this->getDate() 'date' => $this->getDate()
? $this->getDate()->format("Y-m-d H:i:s") ? $this->getDate()->format("Y-m-d H:i:s")

View File

@ -6,39 +6,28 @@ use Doctrine\Common\Collections\ArrayCollection;
class News implements \JsonSerializable class News implements \JsonSerializable
{ {
/** /** @var int */
* @var int
*/
private $id; private $id;
/** /** @var string */
* @var string private $imageUrl;
*/
/** @var string */
private $name; private $name;
/** /** @var \DateTime */
* @var \DateTime
*/
private $date; private $date;
/** /** @var string */
* @var string
*/
private $description; private $description;
/** /** @var \DateTime */
* @var \DateTime
*/
private $finalEntry; private $finalEntry;
/** /** @var string */
* @var string
*/
private $accountable; private $accountable;
/** /** @var ArrayCollection|Comment[] */
* @var ArrayCollection|Comment[]
*/
private $comments; private $comments;
public function __construct() public function __construct()
@ -64,6 +53,24 @@ class News implements \JsonSerializable
return $this; return $this;
} }
/**
* @return string
*/
public function getImageUrl(): ?string
{
return $this->imageUrl;
}
/**
* @param string $imageUrl
* @return News
*/
public function setImageUrl(?string $imageUrl): News
{
$this->imageUrl = $imageUrl;
return $this;
}
/** /**
* @return string * @return string
*/ */
@ -200,6 +207,7 @@ class News implements \JsonSerializable
{ {
return [ return [
'id' => $this->getId(), 'id' => $this->getId(),
'imageUrl' => $this->getImageUrl(),
'name' => $this->getName(), 'name' => $this->getName(),
'date' => $this->getDate() 'date' => $this->getDate()
? $this->getDate()->format("Y-m-d H:i:s") ? $this->getDate()->format("Y-m-d H:i:s")

View File

@ -164,7 +164,7 @@ class SkiesClientService
public function getActivity(int $id): Activity public function getActivity(int $id): Activity
{ {
$response = $this->doSkiesRequest('GET', sprintf(self::SKIES_ACTIVITY_URL, $id)); $response = $this->doSkiesRequest('GET', sprintf(self::SKIES_ACTIVITY_URL, $id));
$htmlBody = $response->getBody(); $htmlBody = $response->getBody()->getContents();
return $this->parseActivityPage($htmlBody, $id); return $this->parseActivityPage($htmlBody, $id);
} }
@ -219,6 +219,14 @@ class SkiesClientService
$activityNode[0] $activityNode[0]
); );
/** Activity image */
$activityImages = Document\Query::execute(
'.//a[@data-fancybox-group="gallery"]/img',
$domDocument,
Document\Query::TYPE_XPATH,
$activityNode[0]
);
$divNodes = Document\Query::execute( $divNodes = Document\Query::execute(
".//div", ".//div",
$domDocument, $domDocument,
@ -251,6 +259,9 @@ class SkiesClientService
$activity = new Activity(); $activity = new Activity();
$activity->setId($id) $activity->setId($id)
->setImageUrl($activityImages[0]
? $activityImages[0]->attributes->getNamedItem("src")->textContent
: null)
->setName($this->clearTextNode($h5Nodes[0]->textContent)) ->setName($this->clearTextNode($h5Nodes[0]->textContent))
->setDescription($this->parseActivityDescription($activityNode[0])) ->setDescription($this->parseActivityDescription($activityNode[0]))
->setDate(new \DateTime(sprintf("%s %s", $matches[1], $matches[2]))) ->setDate(new \DateTime(sprintf("%s %s", $matches[1], $matches[2])))
@ -348,13 +359,14 @@ class SkiesClientService
{ {
$signed = []; $signed = [];
for ($i = 1; $i < $usersBlockText->count(); $i++) { for ($i = 1; $i < $usersBlockText->count(); $i++) {
preg_match( if (false != preg_match(
"#[0-9]+\.[^\p{L}]([\p{L}\s]+)#msiu", "#[0-9]+\.[^\p{L}]([\p{L}\s]+)#msiu",
$usersBlockText[$i]->textContent, $usersBlockText[$i]->textContent,
$usernameMatch $usernameMatch
); )) {
$signed[] = $usernameMatch[1]; $signed[] = $usernameMatch[1];
} }
}
$collator = new \Collator("hu_HU"); $collator = new \Collator("hu_HU");
$collator->sort($signed); $collator->sort($signed);
return $signed; return $signed;
@ -403,17 +415,30 @@ class SkiesClientService
{ {
$domDocument = new Document($htmlBody); $domDocument = new Document($htmlBody);
$newsNodes = Document\Query::execute( $newsNodes = Document\Query::execute(
"div.container div.row div.box div.one_block > a", "div.container div.row div.box div.one_block",
$domDocument, $domDocument,
Document\Query::TYPE_CSS Document\Query::TYPE_CSS
); );
$news = []; $news = [];
for ($i = 1; $i < $newsNodes->count(); $i++) { $newsCount = $newsNodes->count();
$href = $newsNodes[$i]->attributes->getNamedItem("href")->textContent; for ($i = 1; $i < ($newsCount < 10 ? $newsCount : 10); $i++) {
$aNodes = Document\Query::execute(
".//a[@class]",
$domDocument,
Document\Query::TYPE_XPATH,
$newsNodes[$i]
);
$href = $aNodes[0]->attributes->getNamedItem("href")->textContent;
$queryString = parse_url($href, PHP_URL_QUERY); $queryString = parse_url($href, PHP_URL_QUERY);
parse_str($queryString, $queryParams); parse_str($queryString, $queryParams);
if ($queryParams['rID'] == 200 && isset($queryParams['coreID'])) { if ($queryParams['rID'] == 200 && isset($queryParams['coreID'])) {
// $newsItem = $this->getNewsItem($queryParams["coreID"]);
// if ($aNodes->count() > 1) {
// $newsItem->setImageUrl($aNodes[1]->attributes->getNamedItem("href")->textContent);
// }
// $news[] = $newsItem;
$news[] = $this->getNewsItem($queryParams["coreID"]); $news[] = $this->getNewsItem($queryParams["coreID"]);
} }
} }
@ -428,7 +453,7 @@ class SkiesClientService
public function getNewsItem(int $id): News public function getNewsItem(int $id): News
{ {
$response = $this->doSkiesRequest('GET', sprintf(self::SKIES_NEWS_URL, $id)); $response = $this->doSkiesRequest('GET', sprintf(self::SKIES_NEWS_URL, $id));
$htmlBody = $response->getBody(); $htmlBody = $response->getBody()->getContents();
return $this->parseNewsPage($htmlBody, $id); return $this->parseNewsPage($htmlBody, $id);
} }
@ -478,6 +503,13 @@ class SkiesClientService
$activityNode[0] $activityNode[0]
); );
$imgNodes = Document\Query::execute(
'.//a[@data-fancybox-group="gallery"]',
$domDocument,
Document\Query::TYPE_XPATH,
$contentDivNodes[0]
);
/** Comment block */ /** Comment block */
$commentNodes = Document\Query::execute( $commentNodes = Document\Query::execute(
'.//div[@class="onecomment"]', './/div[@class="onecomment"]',
@ -488,11 +520,14 @@ class SkiesClientService
$news = new News(); $news = new News();
$news->setId($id) $news->setId($id)
->setImageUrl($imgNodes->count()
? $imgNodes[0]->attributes->getNamedItem("href")->textContent
: null)
->setName($this->clearTextNode($h5Nodes[0]->textContent)) ->setName($this->clearTextNode($h5Nodes[0]->textContent))
->setDescription($this->parseNewsDescription($contentDivNodes[0])) ->setDescription($this->parseNewsDescription($contentDivNodes[0]))
->setDate(isset($matches[1]) ? new \DateTime($matches[1]) : null) ->setDate(isset($matches[1]) ? new \DateTime($matches[1]) : null)
->setAccountable($this->clearTextNode($aNodes[0]->textContent)); ->setAccountable($this->clearTextNode($aNodes[0]->textContent));
// $this->parseNewsComments($commentNodes, $news); $this->parseNewsComments($commentNodes, $news);
return $news; return $news;
} }
@ -506,9 +541,17 @@ class SkiesClientService
$description = ""; $description = "";
/** @var \DOMElement $childNode */ /** @var \DOMElement $childNode */
foreach ($element->childNodes as $childNode) { foreach ($element->childNodes as $childNode) {
$description .= $childNode->nodeName == "br" switch ($childNode->nodeName) {
? "\n" case "br":
: rtrim($childNode->textContent); $description .= "\n";
break;
case "a":
$description .= "[" . $childNode->getAttribute("href") . "]";
break;
default:
$description .= rtrim($childNode->textContent);
break;
}
} }
return $this->clearTextNode($description); return $this->clearTextNode($description);
} }
@ -523,8 +566,9 @@ class SkiesClientService
foreach ($commentElements as $commentElement) { foreach ($commentElements as $commentElement) {
$divElements = $commentElement->getElementsByTagName("div"); $divElements = $commentElement->getElementsByTagName("div");
$userAnchor = $commentElement->getElementsByTagName("a");
$queryString = parse_url( $queryString = parse_url(
$commentElement->getElementsByTagName("a")->item(0)->getAttribute("href"), $userAnchor->item($userAnchor->length-1)->getAttribute("href"),
PHP_URL_QUERY PHP_URL_QUERY
); );
parse_str($queryString, $queryParams); parse_str($queryString, $queryParams);
@ -536,11 +580,11 @@ class SkiesClientService
$user = new User(); $user = new User();
$user->setDisplayName($matches[1]) $user->setDisplayName($matches[1])
->setUsername($queryParams['username']); ->setUsername($queryParams['username'] ?? "Anonymous");
$comment = new Comment(); $comment = new Comment();
$comment $comment
->setText($divElements->item(1)->textContent) ->setText($this->clearTextNode($divElements->item(1)->textContent))
->setUser($user) ->setUser($user)
->setCreatedAt(new \DateTime(sprintf( ->setCreatedAt(new \DateTime(sprintf(
"%s %s", "%s %s",
@ -582,9 +626,10 @@ class SkiesClientService
$text = str_replace(" ", " ", $text); $text = str_replace(" ", " ", $text);
$text = str_replace("–", "-", $text); $text = str_replace("–", "-", $text);
// $text = str_replace(chr(0xC2).chr(0x95), "-", $text); // $text = str_replace(chr(0xC2).chr(0x95), "-", $text);
$text = str_replace(chr(0xC2).chr(0xA0), "", $text);
$text = str_replace([chr(0xC2).chr(0x93), chr(0xC2).chr(0x94)], '"', $text); $text = str_replace([chr(0xC2).chr(0x93), chr(0xC2).chr(0x94)], '"', $text);
$text = preg_replace("#[ \t]+#msiu", " ", $text); $text = preg_replace("#[ \t]+#msiu", " ", $text);
return trim($text, " \t\n\r\0\x0B " . chr(0xC2) . chr(0xA0)); return trim($text, " \t\n\r\0\x0B ");
} }
/** /**