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

View File

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

View File

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