Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
floviolleau committed Aug 8, 2023
2 parents b02b970 + b86ee57 commit 6ab3a5f
Show file tree
Hide file tree
Showing 24 changed files with 305 additions and 49 deletions.
10 changes: 6 additions & 4 deletions actions/ConnectivityAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ public function execute(array $request)
throw new \Exception('This action is only available in debug mode!');
}

if (!isset($request['bridge'])) {
$bridgeName = $request['bridge'] ?? null;
if (!$bridgeName) {
return render_template('connectivity.html.php');
}

$bridgeClassName = $this->bridgeFactory->createBridgeClassName($request['bridge']);

$bridgeClassName = $this->bridgeFactory->createBridgeClassName($bridgeName);
if (!$bridgeClassName) {
throw new \Exception(sprintf('Bridge not found: %s', $bridgeName));
}
return $this->reportBridgeConnectivity($bridgeClassName);
}

Expand Down
9 changes: 7 additions & 2 deletions actions/DisplayAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,22 @@ public function execute(array $request)
private function createResponse(array $request)
{
$bridgeFactory = new BridgeFactory();
$bridgeClassName = $bridgeFactory->createBridgeClassName($request['bridge'] ?? '');
$formatFactory = new FormatFactory();

$bridgeName = $request['bridge'] ?? null;
$format = $request['format'] ?? null;

$bridgeClassName = $bridgeFactory->createBridgeClassName($bridgeName);
if (!$bridgeClassName) {
throw new \Exception(sprintf('Bridge not found: %s', $bridgeName));
}
if (!$format) {
throw new \Exception('You must specify a format!');
}
if (!$bridgeFactory->isEnabled($bridgeClassName)) {
throw new \Exception('This bridge is not whitelisted');
}

$formatFactory = new FormatFactory();
$format = $formatFactory->create($format);

$bridge = $bridgeFactory->create($bridgeClassName);
Expand Down
10 changes: 9 additions & 1 deletion actions/FrontpageAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ final class FrontpageAction implements ActionInterface
{
public function execute(array $request)
{
$messages = [];
$showInactive = (bool) ($request['show_inactive'] ?? null);
$activeBridges = 0;

$bridgeFactory = new BridgeFactory();
$bridgeClassNames = $bridgeFactory->getBridgeClassNames();

foreach ($bridgeFactory->getMissingEnabledBridges() as $missingEnabledBridge) {
$messages[] = [
'body' => sprintf('Warning : Bridge "%s" not found', $missingEnabledBridge),
'level' => 'warning'
];
}

$formatFactory = new FormatFactory();
$formats = $formatFactory->getFormatNames();

Expand All @@ -24,7 +32,7 @@ public function execute(array $request)
}

return render(__DIR__ . '/../templates/frontpage.html.php', [
'messages' => [],
'messages' => $messages,
'admin_email' => Configuration::getConfig('admin', 'email'),
'admin_telegram' => Configuration::getConfig('admin', 'telegram'),
'bridges' => $body,
Expand Down
6 changes: 5 additions & 1 deletion actions/SetBridgeCacheAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ public function execute(array $request)

$bridgeFactory = new BridgeFactory();

$bridgeClassName = $bridgeFactory->createBridgeClassName($request['bridge'] ?? '');
$bridgeName = $request['bridge'] ?? null;
$bridgeClassName = $bridgeFactory->createBridgeClassName($bridgeName);
if (!$bridgeClassName) {
throw new \Exception(sprintf('Bridge not found: %s', $bridgeName));
}

// whitelist control
if (!$bridgeFactory->isEnabled($bridgeClassName)) {
Expand Down
4 changes: 3 additions & 1 deletion bridges/AppleMusicBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public function collectData()

foreach ($json->results as $obj) {
if ($obj->wrapperType === 'collection') {
$copyright = $obj->copyright ?? '';

$this->items[] = [
'title' => $obj->artistName . ' - ' . $obj->collectionName,
'uri' => $obj->collectionViewUrl,
Expand All @@ -49,7 +51,7 @@ public function collectData()
. '><img src="' . $obj->artworkUrl100 . '" /></a><br><br>'
. $obj->artistName . ' - ' . $obj->collectionName
. '<br>'
. $obj->copyright,
. $copyright,
];
}
}
Expand Down
3 changes: 2 additions & 1 deletion bridges/AskfmBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public function collectData()

$item['timestamp'] = strtotime($element->find('time', 0)->datetime);

$answer = trim($element->find('div.streamItem_content', 0)->innertext);
$var = $element->find('div.streamItem_content', 0);
$answer = trim($var->innertext ?? '');

// This probably should be cleaned up, especially for YouTube embeds
if ($visual = $element->find('div.streamItem_visual', 0)) {
Expand Down
7 changes: 7 additions & 0 deletions bridges/CssSelectorBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ protected function htmlFindEntries($page, $url_selector, $url_pattern = '', $lim
}
if ($link->tag != 'a') {
$link = $link->find('a', 0);
if (is_null($link)) {
continue;
}
}
$item['uri'] = $link->href;
$item['title'] = $link->plaintext;
Expand All @@ -209,6 +212,10 @@ protected function htmlFindEntries($page, $url_selector, $url_pattern = '', $lim
$link_to_item[$link->href] = $item;
}

if (empty($link_to_item)) {
returnClientError('The provided URL selector matches some elements, but they do not contain links.');
}

$links = $this->filterUrlList(array_keys($link_to_item), $url_pattern, $limit);

if (empty($links)) {
Expand Down
2 changes: 1 addition & 1 deletion bridges/EconomistBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ private function processContent($html, $elem)
$svelte->parent->removeChild($svelte);
}
foreach ($elem->find('img') as $strange_img) {
if (!str_contains($strange_img->src, 'https://economist.com')) {
if (!str_contains($strange_img->src, 'economist.com')) {
$strange_img->src = 'https://economist.com' . $strange_img->src;
}
}
Expand Down
17 changes: 9 additions & 8 deletions bridges/FeedReducerBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ class FeedReducerBridge extends FeedExpander

public function collectData()
{
if (preg_match('#^http(s?)://#i', $this->getInput('url'))) {
$this->collectExpandableDatas($this->getInput('url'));
$url = $this->getInput('url');
if (preg_match('#^http(s?)://#i', $url)) {
$this->collectExpandableDatas($url);
} else {
throw new Exception('URI must begin with http(s)://');
}
Expand All @@ -35,21 +36,21 @@ public function getItems()
$filteredItems = [];
$intPercentage = (int)preg_replace('/[^0-9]/', '', $this->getInput('percentage'));

foreach ($this->items as $thisItem) {
foreach ($this->items as $item) {
// The URL is included in the hash:
// - so you can change the output by adding a local-part to the URL
// - so items with the same URI in different feeds won't be correlated

// $pseudoRandomInteger will be a 16 bit unsigned int mod 100.
// This won't be uniformly distributed 1-100, but should be close enough.

$pseudoRandomInteger = unpack(
'S', // unsigned 16-bit int
hash('sha256', $thisItem['uri'] . '::' . $this->getInput('url'), true)
)[1] % 100;
$data = $item['uri'] . '::' . $this->getInput('url');
$hash = hash('sha256', $data, true);
// S = unsigned 16-bit int
$pseudoRandomInteger = unpack('S', $hash)[1] % 100;

if ($pseudoRandomInteger < $intPercentage) {
$filteredItems[] = $thisItem;
$filteredItems[] = $item;
}
}

Expand Down
2 changes: 1 addition & 1 deletion bridges/FilterBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class FilterBridge extends FeedExpander
'url' => [
'name' => 'Feed URL',
'type' => 'text',
'defaultValue' => 'https://lorem-rss.herokuapp.com/feed?unit=day',
'exampleValue' => 'https://lorem-rss.herokuapp.com/feed?unit=day',
'required' => true,
],
'filter' => [
Expand Down
3 changes: 2 additions & 1 deletion bridges/FourchanBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public function collectData()
$file = $element->find('.file', 0);

if (!empty($file)) {
$item['image'] = $element->find('.file a', 0)->href;
$var = $element->find('.file a', 0);
$item['image'] = $var->href ?? '';
$item['imageThumb'] = $element->find('.file img', 0)->src;
if (!isset($item['imageThumb']) and strpos($item['image'], '.swf') !== false) {
$item['imageThumb'] = 'http://i.imgur.com/eO0cxf9.jpg';
Expand Down
3 changes: 2 additions & 1 deletion bridges/GettrBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ class GettrBridge extends BridgeAbstract

public function collectData()
{
$user = $this->getInput('user');
$api = sprintf(
'https://api.gettr.com/u/user/%s/posts?offset=0&max=%s&dir=fwd&incl=posts&fp=f_uo',
$this->getInput('user'),
$user,
min($this->getInput('limit'), 20)
);
$data = json_decode(getContents($api), false);
Expand Down
17 changes: 15 additions & 2 deletions bridges/HeiseBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ class HeiseBridge extends FeedExpander
'required' => false,
'title' => 'Specify number of full articles to return',
'defaultValue' => 5
],
'sessioncookie' => [
'name' => 'Session Cookie',
'required' => false,
'title' => <<<'TITLE'
If you have a heise+ subscription,
you can enter your cookie (ssohls) here to
have heise+ articles displayed in full.
By default the cookie is 1 year valid.
TITLE,
]
]];
const LIMIT = 5;
Expand All @@ -118,6 +128,7 @@ public function collectData()
protected function parseItem($feedItem)
{
$item = parent::parseItem($feedItem);
$sessioncookie = $this->getInput('sessioncookie');

// strip rss parameter
$item['uri'] = explode('?', $item['uri'])[0];
Expand All @@ -128,13 +139,15 @@ protected function parseItem($feedItem)
}

// abort on heise+ articles and link to archive.ph for full-text content
if (str_starts_with($item['title'], 'heise+ |')) {
if ($sessioncookie == '' && str_starts_with($item['title'], 'heise+ |')) {
$item['uri'] = 'https://archive.ph/?run=1&url=' . urlencode($item['uri']);
return $item;
}

$item['uri'] .= '?seite=all';
$article = getSimpleHTMLDOMCached($item['uri']);
$article = getSimpleHTMLDOM($item['uri'], [
'cookie: ssohls=' . $sessioncookie
]);

if ($article) {
$article = defaultLinkTo($article, $item['uri']);
Expand Down
32 changes: 32 additions & 0 deletions bridges/NiusBridge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

class NiusBridge extends XPathAbstract
{
const NAME = 'Nius';
const URI = 'https://www.nius.de/news';
const DESCRIPTION = 'Die Stimme der Mehrheit';
const MAINTAINER = 'Niehztog';
//const PARAMETERS = array();
const CACHE_TIMEOUT = 3600;

const FEED_SOURCE_URL = 'https://www.nius.de/news';
const XPATH_EXPRESSION_ITEM = './/div[contains(@class, "compact-story") or contains(@class, "regular-story")]';
const XPATH_EXPRESSION_ITEM_TITLE = './/h2[@class="title"]//node()';
const XPATH_EXPRESSION_ITEM_CONTENT = './/h2[@class="title"]//node()';
const XPATH_EXPRESSION_ITEM_URI = './/a[1]/@href';

const XPATH_EXPR_AUTHOR_PART1 = 'normalize-space(.//span[@class="author"]/text()[1])';
const XPATH_EXPR_AUTHOR_PART2 = 'normalize-space(.//span[@class="author"]/text()[2])';
const XPATH_EXPRESSION_ITEM_AUTHOR = 'substring-after(concat(' . self::XPATH_EXPR_AUTHOR_PART1 . ', " ", ' . self::XPATH_EXPR_AUTHOR_PART2 . '), " ")';

//const XPATH_EXPRESSION_ITEM_TIMESTAMP = './/td[3]';
const XPATH_EXPRESSION_ITEM_ENCLOSURES = './/img[1]/@src';
const XPATH_EXPRESSION_ITEM_CATEGORIES = './/div[@class="subtitle"]/text()';
const SETTING_FIX_ENCODING = false;

protected function cleanMediaUrl($mediaUrl)
{
$result = preg_match('~https:\/\/www\.nius\.de\/_next\/image\?url=(.*)\?~', $mediaUrl, $matches);
return $result ? $matches[1] : $mediaUrl;
}
}
Loading

0 comments on commit 6ab3a5f

Please sign in to comment.