[ScientificAmericanBridge] Update bridge (#4074)

* [ScientificAmericanBridge] Update bridge

* [ScientificAmericanBridge] Fix lint
This commit is contained in:
Korytov Pavel 2024-04-12 02:57:55 +03:00 committed by GitHub
parent 58c254ad3b
commit 428c6c3c66
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -25,7 +25,7 @@ class ScientificAmericanBridge extends FeedExpander
]; ];
const FEED = 'http://rss.sciam.com/ScientificAmerican-Global'; const FEED = 'http://rss.sciam.com/ScientificAmerican-Global';
const ISSUES = 'https://www.scientificamerican.com/archive/issues/2020s/'; const ISSUES = 'https://www.scientificamerican.com/archive/issues/';
public function collectData() public function collectData()
{ {
@ -50,7 +50,7 @@ class ScientificAmericanBridge extends FeedExpander
if ($this->getInput('addContents') == 1) { if ($this->getInput('addContents') == 1) {
usort($this->items, function ($item1, $item2) { usort($this->items, function ($item1, $item2) {
return $item1['timestamp'] - $item2['timestamp']; return $item2['timestamp'] - $item1['timestamp'];
}); });
} }
} }
@ -66,8 +66,12 @@ class ScientificAmericanBridge extends FeedExpander
private function collectIssues() private function collectIssues()
{ {
$html = getSimpleHTMLDOMCached(self::ISSUES); $html = getSimpleHTMLDOMCached(self::ISSUES);
$content = $html->getElementById('app')->children(3); $content = $html->getElementById('app');
$issues = $content->children(); $issues_list = $content->find('div[class^="issue__list"]', 0);
if ($issues_list == null) {
return [];
}
$issues = $issues_list->find('div[class^="list__item"]');
$issues_count = min( $issues_count = min(
(int)$this->getInput('parseIssues'), (int)$this->getInput('parseIssues'),
count($issues) count($issues)
@ -87,36 +91,19 @@ class ScientificAmericanBridge extends FeedExpander
$items = []; $items = [];
$html = getSimpleHTMLDOMCached($issue_link); $html = getSimpleHTMLDOMCached($issue_link);
$features = $html->find('[class^=Detail_issue__article__previews__featured]', 0); $blocks = $html->find('[class^="issueArchiveArticleListCompact"]');
if ($features != null) { foreach ($blocks as $block) {
$articles = $features->find('div', 0)->children(); $articles = $block->find('article[class*="article"]');
foreach ($articles as $article) { foreach ($articles as $article) {
$h4 = $article->find('h4', 0); $a = $article->find('a[class^="articleLink"]', 0);
$a = $h4->find('a', 0);
$link = 'https://scientificamerican.com' . $a->getAttribute('href'); $link = 'https://scientificamerican.com' . $a->getAttribute('href');
$title = $a->plaintext; $title = $a->find('h2[class^="articleTitle"]', 0);
$items[] = [ array_push($items, [
'uri' => $link, 'uri' => $link,
'title' => $title, 'title' => $title->plaintext,
'uid' => $link, 'uid' => $link,
'content' => '' 'content' => ''
]; ]);
}
}
$departments = $html->find('[class^=Detail_issue__article__previews__departments]', 0);
if ($departments != null) {
$headers = $departments->find('[class*=Listing_article__listing__title]');
foreach ($headers as $header) {
$a = $header->find('a', 0);
$link = 'https://scientificamerican.com' . $a->getAttribute('href');
$title = $a->plaintext;
$items[] = [
'uri' => $link,
'title' => $title,
'uid' => $link,
'content' => ''
];
} }
} }
@ -125,65 +112,67 @@ class ScientificAmericanBridge extends FeedExpander
private function updateItem($item) private function updateItem($item)
{ {
return $item;
$html = getSimpleHTMLDOMCached($item['uri']); $html = getSimpleHTMLDOMCached($item['uri']);
$article = $html->find('#sa_body', 0)->find('article', 0); $article = $html->find('#app', 0)->find('article', 0);
$time = $article->find('time[itemprop="datePublished"]', 0); $time = $article->find('p[class^="article_pub_date"]', 0);
if ($time == null) {
$time = $html->find('span[itemprop="datePublished"]', 0);
}
if ($time) { if ($time) {
$datetime = DateTime::createFromFormat('F j, Y', $time->plaintext); $datetime = DateTime::createFromFormat('F j, Y', $time->plaintext);
$datetime->setTime(0, 0, 0, 0);
$item['timestamp'] = $datetime->format('U'); $item['timestamp'] = $datetime->format('U');
} }
$main = $article->find('section.article-grid__main', 0);
if ($main == null) { $authors = $article->find('a[class^="article_authors__link"]');
$main = $article->find('div.article-text', 0); if ($authors) {
$author = implode('; ', array_map(fn($a) => $a->plaintext, $authors));
$item['author'] = $author;
} }
if ($main == null) { $res = '';
return $item; $desc = $article->find('div[class^="article_dek"]', 0);
if ($desc) {
$res .= $desc->innertext;
} }
foreach ($main->find('img') as $img) { $lead_figure = $article->find('figure[class^="lead_image"]', 0);
$img->removeAttribute('width'); if ($lead_figure) {
$img->removeAttribute('height'); $res .= $lead_figure->outertext;
$img->setAttribute('style', 'height: auto; width: auto; max-height: 768px');
} }
$rights_link = $main->find('div.article-rightslink', 0); $content = $article->find('div[class^="article__content"]', 0);
if ($rights_link != null) { if ($content) {
$rights_link->parent->removeChild($rights_link); foreach ($content->children() as $block) {
} if (str_contains($block->innertext, 'On supporting science journalism')) {
$reprints_link = $main->find('div.article-reprintsLink', 0); continue;
if ($reprints_link != null) { }
$reprints_link->parent->removeChild($reprints_link); if (
} ($block->tag == 'p' && $block->getAttribute('data-block') == 'sciam/paragraph')
$about_section = $main->find('section.article-author-container', 0); || ($block->tag == 'figure' && str_starts_with($block->class, 'article__image'))
if ($about_section != null) { ) {
$about_section->parent->removeChild($about_section); $iframe = $block->find('iframe', 0);
} if ($iframe) {
$read_next = $main->find('#read-next', 0); $res .= "<a href=\"{$iframe->src}\">{$iframe->src}</a>";
if ($read_next != null) { } else {
$read_next->parent->removeChild($read_next); $res .= $block->outertext;
}
} else if ($block->tag == 'h2') {
$res .= '<h3>' . $block->innertext . '</h3>';
} else if ($block->tag == 'blockquote') {
$res .= $block->outertext;
} else if ($block->tag == 'hr' && $block->getAttribute('data-block') == 'sciam/raw_html') {
$res .= '<hr />';
}
}
} }
foreach ($main->find('iframe') as $iframe) { $footer = $article->find('footer[class*="footer"]', 0);
$a = $html->createElement('a'); if ($footer) {
$a->href = $iframe->src; $bios = $footer->find('div[class^=bio]');
$a->innertext = $iframe->src; $bio = implode('', array_map(fn($b) => $b->innertext, $bios));
$iframe->parent->appendChild($a); $res .= $bio;
$iframe->parent->removeChild($iframe);
} }
$authors = $main->find('span[itemprop="author"]', 0); $item['content'] = $res;
if ($authors != null) {
$item['author'] = $authors->plaintext;
}
$item['content'] = $main->innertext;
return $item; return $item;
} }
} }