2022-04-08 22:21:13 +03:00
|
|
|
<?php
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-03-12 01:26:22 +03:00
|
|
|
class GatesNotesBridge extends BridgeAbstract
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
|
|
|
const MAINTAINER = 'corenting';
|
|
|
|
const NAME = 'Gates Notes';
|
|
|
|
const URI = 'https://www.gatesnotes.com';
|
|
|
|
const DESCRIPTION = 'Returns the newest articles.';
|
|
|
|
const CACHE_TIMEOUT = 21600; // 6h
|
|
|
|
|
2023-03-12 01:26:22 +03:00
|
|
|
public function collectData()
|
|
|
|
{
|
|
|
|
$params = [
|
|
|
|
'validYearsString' => 'all',
|
|
|
|
'setNumber' => '0',
|
|
|
|
'sortByVideo' => 'all',
|
|
|
|
'sortByTopic' => 'all'
|
|
|
|
];
|
|
|
|
$api_endpoint = '/api/TGNWebAPI/Get_Filtered_Article_Set?';
|
|
|
|
$apiUrl = self::URI . $api_endpoint . http_build_query($params);
|
|
|
|
|
|
|
|
$rawContent = getContents($apiUrl);
|
2024-01-29 23:51:23 +03:00
|
|
|
$cleanedContent = trim($rawContent, '"');
|
|
|
|
$cleanedContent = str_replace('\r\n', "\n", $cleanedContent);
|
|
|
|
$cleanedContent = stripslashes($cleanedContent);
|
2023-03-12 01:26:22 +03:00
|
|
|
|
2023-07-06 04:36:01 +03:00
|
|
|
$json = Json::decode($cleanedContent, false);
|
2023-12-19 10:46:37 +03:00
|
|
|
if (is_string($json)) {
|
|
|
|
throw new \Exception('wtf? ' . $json);
|
|
|
|
}
|
2023-03-12 01:26:22 +03:00
|
|
|
|
|
|
|
foreach ($json as $article) {
|
|
|
|
$item = [];
|
|
|
|
|
|
|
|
$articleUri = self::URI . '/' . $article->{'_system_'}->name;
|
|
|
|
|
|
|
|
$item['uri'] = $articleUri;
|
|
|
|
$item['title'] = $article->headline;
|
|
|
|
$item['content'] = self::getItemContent($articleUri);
|
|
|
|
$item['timestamp'] = strtotime($article->date);
|
|
|
|
|
|
|
|
$this->items[] = $item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getItemContent($articleUri)
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2023-03-12 01:26:22 +03:00
|
|
|
// We need to change the headers as the normal desktop website
|
|
|
|
// use canvas-based image carousels for some pictures
|
|
|
|
$headers = [
|
|
|
|
'User-Agent: Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
|
|
|
|
];
|
|
|
|
$article_html = getSimpleHTMLDOMCached($articleUri, 86400, $headers);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-03-12 01:26:22 +03:00
|
|
|
$content = '';
|
2022-07-01 16:10:30 +03:00
|
|
|
if (!$article_html) {
|
2023-03-12 01:26:22 +03:00
|
|
|
$content .= '<p><em>Could not request ' . $this->getName() . ': ' . $articleUri . '</em></p>';
|
|
|
|
return $content;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
$article_html = defaultLinkTo($article_html, $this->getURI());
|
|
|
|
|
|
|
|
$top_description = '<p>' . $article_html->find('div.article_top_description', 0)->innertext . '</p>';
|
2023-07-06 04:36:01 +03:00
|
|
|
$heroImage = $article_html->find('img.article_top_DMT_Image', 0);
|
|
|
|
if ($heroImage) {
|
|
|
|
$hero_image = '<img src=' . $heroImage->getAttribute('data-src') . '>';
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
$article_body = $article_html->find('div.TGN_Article_ReadTimeSection', 0);
|
2023-03-12 01:26:22 +03:00
|
|
|
|
|
|
|
// Remove the menu bar on some articles (PDF download etc.)
|
|
|
|
foreach ($article_body->find('.TGN_MenuHolder') as $found) {
|
|
|
|
$found->remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
// For the carousels pictures, we still to remove the lazy-loading and force the real picture
|
|
|
|
foreach ($article_body->find('canvas') as $found) {
|
|
|
|
$found->remove();
|
|
|
|
}
|
|
|
|
foreach ($article_body->find('.TGN_PE_C_Img') as $found) {
|
|
|
|
$found->setAttribute('src', $found->getAttribute('data-src'));
|
|
|
|
}
|
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
// Convert iframe of Youtube videos to link
|
|
|
|
foreach ($article_body->find('iframe') as $found) {
|
|
|
|
$iframeUrl = $found->getAttribute('src');
|
|
|
|
|
|
|
|
if ($iframeUrl) {
|
|
|
|
$text = 'Embedded Youtube video, click here to watch on Youtube.com';
|
|
|
|
$found->outertext = '<p><a href="' . $iframeUrl . '">' . $text . '</a></p>';
|
|
|
|
}
|
|
|
|
}
|
2023-03-12 01:26:22 +03:00
|
|
|
|
2022-07-01 16:10:30 +03:00
|
|
|
// Remove <link> CSS ressources
|
|
|
|
foreach ($article_body->find('link') as $found) {
|
|
|
|
$linkedRessourceUrl = $found->getAttribute('href');
|
|
|
|
|
|
|
|
if (str_ends_with($linkedRessourceUrl, '.css')) {
|
|
|
|
$found->outertext = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$article_body = sanitize($article_body->innertext);
|
|
|
|
|
2023-10-12 20:49:04 +03:00
|
|
|
$content = $top_description . ($hero_image ?? '') . $article_body;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-03-12 01:26:22 +03:00
|
|
|
return $content;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2022-04-08 22:21:13 +03:00
|
|
|
}
|