From c78c1254a85c14ec44c77ac1da983d4b07efcaf4 Mon Sep 17 00:00:00 2001 From: Korytov Pavel Date: Sun, 19 Jun 2022 04:57:40 +0300 Subject: [PATCH] [NovayaGazetaEuropeBridge] Add bridge (#2827) --- bridges/NovayaGazetaEuropeBridge.php | 141 +++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 bridges/NovayaGazetaEuropeBridge.php diff --git a/bridges/NovayaGazetaEuropeBridge.php b/bridges/NovayaGazetaEuropeBridge.php new file mode 100644 index 00000000..c7511a31 --- /dev/null +++ b/bridges/NovayaGazetaEuropeBridge.php @@ -0,0 +1,141 @@ + array( + 'language' => array( + 'name' => 'Language', + 'type' => 'list', + 'defaultValue' => 'ru', + 'values' => array( + 'Russian' => 'ru', + 'English' => 'en', + ) + ), + 'limit' => array( + 'name' => 'Limit', + 'type' => 'number', + 'required' => false, + 'title' => 'Maximum number of items to return', + 'defaultValue' => 20 + ) + ) + ); + + public function collectData() + { + $url = 'https://novayagazeta.eu/api/v1/get/main'; + if ($this->getInput('language') != 'ru') { + $url .= '?lang=' . $this->getInput('language'); + } + + $json = getContents($url); + $data = json_decode($json); + + foreach ($data->records as $record) { + foreach ($record->blocks as $block) { + if (!property_exists($block, 'date')) { + continue; + } + $title = strip_tags($block->title); + if (!empty($block->subtitle)) { + $title .= '. ' . strip_tags($block->subtitle); + } + $item = array( + 'uri' => self::URI . '/articles/' . $block->slug, + 'block' => $block, + 'title' => $title, + 'author' => join(', ', array_map(function ($author) { + return $author->name; + }, $block->authors)), + 'timestamp' => $block->date / 1000, + 'categories' => $block->tags + ); + $this->items[] = $item; + } + } + usort($this->items, function ($item1, $item2) { + return $item2['timestamp'] <=> $item1['timestamp']; + }); + if ($this->getInput('limit') !== null) { + $this->items = array_slice($this->items, 0, $this->getInput('limit')); + } + foreach ($this->items as &$item) { + $block = $item['block']; + $body = ''; + if (property_exists($block, 'body') && $block->body !== null) { + $body = self::convertBody($block); + } else { + $record_json = getContents("https://novayagazeta.eu/api/v1/get/record?slug={$block->slug}"); + $record_data = json_decode($record_json); + $body = self::convertBody($record_data->record); + } + $item['content'] = $body; + unset($item['block']); + } + } + + private function convertBody($data) { + $body = ''; + if ($data->previewUrl !== null && !$data->isPreviewHidden) { + $body .= '
'; + if ($data->previewCaption !== null) { + $body .= '
' . $data->previewCaption . '
'; + } + $body .= '
'; + } + if ($data->lead !== null) { + $body .= "

{$data->lead}

"; + } + if (!empty($data->body)) { + foreach ($data->body as $datum) { + $body .= self::convertElement($datum); + } + } + return $body; + } + + private function convertElement($datum) { + switch ($datum->type) { + case 'text': + return $datum->data; + case 'image/single': + $alt = strip_tags($datum->data); + $res = "
previewUrl}\" alt=\"{$alt}\" />"; + if ($datum->data !== null) { + $res .= "
{$datum->data}
"; + } + $res .= '
'; + return $res; + case 'text/quote': + return "
{$datum->data}

"; + case 'embed/native': + $desc = $datum->link; + if (property_exists($datum, 'caption')) { + $desc = $datum->caption; + } + return "

link}\">{$desc}

"; + case 'text/framed': + $res = ''; + if (property_exists($datum, 'typeDisplay')) { + $res .= "

{$datum->typeDisplay}

"; + } + $res .= "

{$datum->data}

"; + if (property_exists($datum, 'attachment') + && property_exists($datum->attachment, 'type')) { + $res .= self::convertElement($datum->attachment); + } + return $res; + default: + return ''; + } + } +}