rss-bridge/bridges/NordbayernBridge.php

204 lines
6.8 KiB
PHP
Raw Normal View History

2020-03-31 22:14:16 +03:00
<?php
class NordbayernBridge extends BridgeAbstract
{
const MAINTAINER = 'schabi.org';
const NAME = 'Nordbayern';
const CACHE_TIMEOUT = 3600;
const URI = 'https://www.nordbayern.de';
const DESCRIPTION = 'Bridge for Bavarian regional news site nordbayern.de';
const PARAMETERS = [ [
'region' => [
'name' => 'region',
'type' => 'list',
'exampleValue' => 'Nürnberg',
'title' => 'Select a region',
'values' => [
'Ansbach' => 'ansbach',
'Bamberg' => 'bamberg',
2022-08-13 20:41:05 +03:00
'Bayreuth' => 'bayreuth',
'Erlangen' => 'erlangen',
'Forchheim' => 'forchheim',
2022-08-13 20:41:05 +03:00
'Fürth' => 'fuerth',
'Gunzenhausen' => 'gunzenhausen',
'Herzogenaurach' => 'herzogenaurach',
'Höchstadt' => 'hoechstadt',
'Neumarkt' => 'neumarkt',
2022-08-13 20:41:05 +03:00
'Neustadt/Aisch-Bad Windsheim' => 'neustadt-aisch-bad-windsheim',
'Nürnberg' => 'nuernberg',
'Nürnberger Land' => 'nuernberger-land',
'Regensburg' => 'regensburg',
'Roth' => 'roth',
2022-08-13 20:41:05 +03:00
'Schwabach' => 'schwabach',
'Weißenburg' => 'weissenburg'
]
],
'policeReports' => [
'name' => 'Police Reports',
'type' => 'checkbox',
'exampleValue' => 'checked',
'title' => 'Include Police Reports',
2022-10-08 16:34:26 +03:00
],
'hideNNPlus' => [
'name' => 'Hide NN+ articles',
'type' => 'checkbox',
'exampleValue' => 'unchecked',
'title' => 'Hide all paywall articles on NN'
],
'hideDPA' => [
'name' => 'Hide dpa articles',
'type' => 'checkbox',
'exampleValue' => 'unchecked',
'title' => 'Hide external articles from dpa'
]
]];
2020-03-31 22:14:16 +03:00
private function getValidImage($picture)
{
$img = $picture->find('img', 0);
if ($img) {
$imgUrl = $img->src;
if (!preg_match('#/logo-.*\.png#', $imgUrl)) {
return '<br><img src="' . $imgUrl . '">';
}
}
return '';
}
2020-03-31 22:14:16 +03:00
private function getUseFullContent($rawContent)
{
$content = '';
foreach ($rawContent->children as $element) {
if (
($element->tag === 'p' || $element->tag === 'h3') &&
$element->class !== 'article__teaser'
) {
$content .= $element;
} elseif ($element->tag === 'main') {
$content .= self::getUseFullContent($element->find('article', 0));
} elseif ($element->tag === 'header') {
$content .= self::getUseFullContent($element);
} elseif (
$element->tag === 'div' &&
!str_contains($element->class, 'article__infobox') &&
!str_contains($element->class, 'authorinfo')
) {
$content .= self::getUseFullContent($element);
} elseif (
$element->tag === 'section' &&
(str_contains($element->class, 'article__richtext') ||
str_contains($element->class, 'article__context'))
) {
$content .= self::getUseFullContent($element);
} elseif ($element->tag === 'picture') {
$content .= self::getValidImage($element);
}
}
return $content;
}
2022-05-02 20:06:30 +03:00
private function getTeaser($content)
{
$teaser = $content->find('p[class=article__teaser]', 0);
if ($teaser === null) {
return '';
}
$teaser = $teaser->plaintext;
$teaser = preg_replace('/[ ]{2,}/', ' ', $teaser);
$teaser = '<p class="article__teaser">' . $teaser . '</p>';
return $teaser;
}
2020-03-31 22:14:16 +03:00
private function getArticle($link)
{
$item = [];
$article = getSimpleHTMLDOM($link);
defaultLinkTo($article, self::URI);
$content = $article->find('article[id=article]', 0);
$item['uri'] = $link;
2022-06-04 21:50:16 +03:00
$author = $article->find('.article__author', 1);
if ($author !== null) {
$item['author'] = trim($author->plaintext);
}
$createdAt = $article->find('[class=article__release]', 0);
if ($createdAt) {
$item['timestamp'] = strtotime(str_replace('Uhr', '', $createdAt->plaintext));
}
if ($article->find('h2', 0) === null) {
$item['title'] = $article->find('h3', 0)->innertext;
} else {
$item['title'] = $article->find('h2', 0)->innertext;
}
$item['content'] = '';
if ($article->find('section[class*=article__richtext]', 0) === null) {
$content = $article->find('div[class*=modul__teaser]', 0)
->find('p', 0);
$item['content'] .= $content;
} else {
$content = $article->find('article', 0);
// change order of article teaser in order to show it on top
// of the title image. If we didn't do this some rss programs
// would show the subtitle of the title image as teaser instead
// of the actuall article teaser.
$item['content'] .= self::getTeaser($content);
$item['content'] .= self::getUseFullContent($content);
}
2020-03-31 22:14:16 +03:00
$article->clear();
return $item;
}
private function handleNewsblock($listSite)
{
$main = $listSite->find('main', 0);
foreach ($main->find('article') as $article) {
$url = $article->find('a', 0)->href;
$url = urljoin(self::URI, $url);
2022-10-08 16:34:26 +03:00
// exclude nn+ articles if desired
if (
$this->getInput('hideNNPlus') &&
str_contains($url, 'www.nn.de')
) {
continue;
}
$item = self::getArticle($url);
// exclude police reports if desired
if (
!$this->getInput('policeReports') &&
str_contains($item['content'], 'Hier geht es zu allen aktuellen Polizeimeldungen.')
2022-10-08 16:34:26 +03:00
) {
continue;
2022-10-08 16:34:26 +03:00
}
// exclude dpa articles
if (
$this->getInput('hideDPA') &&
str_contains($item['author'], 'dpa')
) {
continue;
}
$this->items[] = $item;
}
}
2020-03-31 22:14:16 +03:00
public function collectData()
{
$region = $this->getInput('region');
if ($region === 'rothenburg-o-d-t') {
$region = 'rothenburg-ob-der-tauber';
}
$url = self::URI . '/region/' . $region;
$listSite = getSimpleHTMLDOM($url);
2020-03-31 22:14:16 +03:00
self::handleNewsblock($listSite);
}
2020-03-31 22:14:16 +03:00
}