2023-03-07 19:03:50 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class InternationalInstituteForStrategicStudiesBridge extends BridgeAbstract
|
|
|
|
{
|
|
|
|
const MAINTAINER = 'sqrtminusone';
|
|
|
|
const NAME = 'International Institute For Strategic Studies Bridge';
|
|
|
|
const URI = 'https://www.iiss.org';
|
|
|
|
|
|
|
|
const CACHE_TIMEOUT = 3600; // 1 hour
|
|
|
|
const DESCRIPTION = 'Returns the latest blog posts from the IISS';
|
|
|
|
|
2023-04-08 23:09:07 +03:00
|
|
|
const TEMPLATE_ID = ['BlogArticlePage', 'BlogPage'];
|
|
|
|
const COMPONENT_ID = '9b0c6919-c78b-4910-9be9-d73e6ee40e50';
|
2023-03-07 19:03:50 +03:00
|
|
|
|
|
|
|
public function collectData()
|
|
|
|
{
|
2023-04-08 23:09:07 +03:00
|
|
|
$url = 'https://www.iiss.org/api/filteredlist/filter';
|
2023-03-07 19:03:50 +03:00
|
|
|
$opts = [
|
|
|
|
CURLOPT_CUSTOMREQUEST => 'POST',
|
|
|
|
CURLOPT_POSTFIELDS => json_encode([
|
2023-04-08 23:09:07 +03:00
|
|
|
'templateId' => self::TEMPLATE_ID,
|
2023-03-07 19:03:50 +03:00
|
|
|
'componentId' => self::COMPONENT_ID,
|
|
|
|
'page' => '1',
|
2023-04-08 23:09:07 +03:00
|
|
|
'amount' => 1,
|
2023-03-07 19:03:50 +03:00
|
|
|
'filter' => (object)[],
|
|
|
|
'tags' => null,
|
2023-04-08 23:09:07 +03:00
|
|
|
'sortType' => 'Newest',
|
|
|
|
'restrictionType' => 'Any'
|
2023-03-07 19:03:50 +03:00
|
|
|
])
|
|
|
|
];
|
|
|
|
$headers = [
|
|
|
|
'Accept: application/json, text/plain, */*',
|
2024-07-31 18:30:06 +03:00
|
|
|
'Content-Type: application/json;charset=UTF-8',
|
2023-03-07 19:03:50 +03:00
|
|
|
];
|
|
|
|
$json = getContents($url, $headers, $opts);
|
|
|
|
$data = json_decode($json);
|
|
|
|
|
|
|
|
foreach ($data->model->Results as $record) {
|
2023-04-08 23:09:07 +03:00
|
|
|
[$content, $enclosures] = $this->getContents(self::URI . $record->Link);
|
2023-03-07 19:03:50 +03:00
|
|
|
$this->items[] = [
|
|
|
|
'uri' => self::URI . $record->Link,
|
|
|
|
'title' => $record->Heading,
|
|
|
|
'categories' => [$record->Topic],
|
|
|
|
'author' => join(', ', array_map(function ($author) {
|
|
|
|
return $author->Name;
|
|
|
|
}, $record->Authors)),
|
|
|
|
'timestamp' => DateTime::createFromFormat('jS F Y', $record->Date)->format('U'),
|
2023-04-08 23:09:07 +03:00
|
|
|
'content' => $content,
|
|
|
|
'enclosures' => $enclosures
|
2023-03-07 19:03:50 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getContents($uri)
|
|
|
|
{
|
|
|
|
$html = getSimpleHTMLDOMCached($uri);
|
|
|
|
$body = $html->find('body', 0);
|
|
|
|
$scripts = $body->find('script');
|
|
|
|
$result = '';
|
|
|
|
|
2023-04-08 23:09:07 +03:00
|
|
|
$enclosures = [];
|
|
|
|
|
2023-03-07 19:03:50 +03:00
|
|
|
foreach ($scripts as $script) {
|
|
|
|
$script_text = $script->innertext;
|
|
|
|
if (str_contains($script_text, 'ReactDOM.render(React.createElement(Components.Reading')) {
|
|
|
|
$args = $this->getRenderArguments($script_text);
|
|
|
|
$result .= $args->Html;
|
|
|
|
} elseif (str_contains($script_text, 'ReactDOM.render(React.createElement(Components.ImagePanel')) {
|
|
|
|
$args = $this->getRenderArguments($script_text);
|
2023-04-08 23:09:07 +03:00
|
|
|
$result .= '<figure><img src="' . self::URI . $args->Image . '"></img></figure>';
|
2023-03-07 19:03:50 +03:00
|
|
|
} elseif (str_contains($script_text, 'ReactDOM.render(React.createElement(Components.Intro')) {
|
|
|
|
$args = $this->getRenderArguments($script_text);
|
|
|
|
$result .= '<p>' . $args->Intro . '</p>';
|
2023-04-08 23:09:07 +03:00
|
|
|
} elseif (str_contains($script_text, 'ReactDOM.render(React.createElement(Components.Footnotes')) {
|
|
|
|
$args = $this->getRenderArguments($script_text);
|
|
|
|
$result .= '<p>' . $args->Content . '</p>';
|
|
|
|
} elseif (str_contains($script_text, 'ReactDOM.render(React.createElement(Components.List')) {
|
|
|
|
$args = $this->getRenderArguments($script_text);
|
|
|
|
foreach ($args->Items as $item) {
|
|
|
|
if ($item->Url != null) {
|
|
|
|
$match = preg_match('/\\"(.*)\\"/', $item->Url, $matches);
|
|
|
|
if ($match > 0) {
|
|
|
|
array_push($enclosures, self::URI . $matches[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-07 19:03:50 +03:00
|
|
|
}
|
|
|
|
}
|
2023-04-08 23:09:07 +03:00
|
|
|
return [$result, $enclosures];
|
2023-03-07 19:03:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getRenderArguments($script_text)
|
|
|
|
{
|
|
|
|
$matches = [];
|
|
|
|
preg_match('/React\.createElement\(Components\.\w+, {(.*)}\),/', $script_text, $matches);
|
|
|
|
return json_decode('{' . $matches[1] . '}');
|
|
|
|
}
|
|
|
|
}
|