From 7881c87bed8ccade2d49c7eab758fb4c80a4a0c5 Mon Sep 17 00:00:00 2001 From: Dag Date: Sat, 8 Jul 2023 23:21:55 +0200 Subject: [PATCH] fix: various small fixes (#3517) * fix(asrocknews): Trying to get property src of non-object Trying to get property 'src' of non-object at bridges/ASRockNewsBridge.php line 37 * refactor(http): tweak max redirs config * fix(tiktok) * fix(gizmodo) * fix(craig) * fix(nationalg) * fix(roadandtrack) * fix(etsy) --- bridges/ASRockNewsBridge.php | 7 ++++++- bridges/CraigslistBridge.php | 2 +- bridges/EtsyBridge.php | 4 ++-- bridges/GizmodoBridge.php | 2 +- bridges/NationalGeographicBridge.php | 2 +- bridges/RoadAndTrackBridge.php | 2 +- bridges/TikTokBridge.php | 3 ++- lib/contents.php | 9 ++++++--- 8 files changed, 20 insertions(+), 11 deletions(-) diff --git a/bridges/ASRockNewsBridge.php b/bridges/ASRockNewsBridge.php index 1b516377..1a327978 100644 --- a/bridges/ASRockNewsBridge.php +++ b/bridges/ASRockNewsBridge.php @@ -34,7 +34,12 @@ class ASRockNewsBridge extends BridgeAbstract $item['content'] = $contents->innertext; $item['timestamp'] = $this->extractDate($a->plaintext); - $item['enclosures'][] = $a->find('img', 0)->src; + + $img = $a->find('img', 0); + if ($img) { + $item['enclosures'][] = $img->src; + } + $this->items[] = $item; if (count($this->items) >= 10) { diff --git a/bridges/CraigslistBridge.php b/bridges/CraigslistBridge.php index a230422e..9a2c1358 100644 --- a/bridges/CraigslistBridge.php +++ b/bridges/CraigslistBridge.php @@ -63,7 +63,7 @@ class CraigslistBridge extends BridgeAbstract $html = getSimpleHTMLDOM($uri); // Check if no results page is shown (nearby results) - if ($html->find('.displaycountShow', 0)->plaintext == '0') { + if (($html->find('.displaycountShow', 0)->plaintext ?? '') == '0') { return; } diff --git a/bridges/EtsyBridge.php b/bridges/EtsyBridge.php index 05bf7d26..493dd236 100644 --- a/bridges/EtsyBridge.php +++ b/bridges/EtsyBridge.php @@ -50,8 +50,8 @@ class EtsyBridge extends BridgeAbstract $item['author'] = $result->find('p.wt-text-gray > span', 2)->plaintext; $item['content'] = '

' - . $result->find('span.currency-symbol', 0)->plaintext - . $result->find('span.currency-value', 0)->plaintext + . ($result->find('span.currency-symbol', 0)->plaintext ?? '') + . ($result->find('span.currency-value', 0)->plaintext ?? '') . '

' . $result->find('a', 0)->title . '

'; diff --git a/bridges/GizmodoBridge.php b/bridges/GizmodoBridge.php index 64e2fc8a..8ed30704 100644 --- a/bridges/GizmodoBridge.php +++ b/bridges/GizmodoBridge.php @@ -22,7 +22,7 @@ class GizmodoBridge extends FeedExpander // Get header image $image = $html->find('meta[property="og:image"]', 0)->content; - $item['content'] = $html->find('div.js_post-content', 0)->innertext; + $item['content'] = $html->find('div.js_post-content', 0)->innertext ?? ''; // Get categories $categories = explode(',', $html->find('meta[name="keywords"]', 0)->content); diff --git a/bridges/NationalGeographicBridge.php b/bridges/NationalGeographicBridge.php index 79a0600b..f7572240 100644 --- a/bridges/NationalGeographicBridge.php +++ b/bridges/NationalGeographicBridge.php @@ -319,7 +319,7 @@ EOD; $content .= $module['note']; break; case 'listicle': - $content .= '

' . $module['title'] . '

'; + $content .= '

' . ($module['title'] ?? '(no title)') . '

'; if (isset($module['image'])) { $content .= $this->handleImages($module['image'], $module['image']['cmsType']); } diff --git a/bridges/RoadAndTrackBridge.php b/bridges/RoadAndTrackBridge.php index d666b6bd..31bd7ec8 100644 --- a/bridges/RoadAndTrackBridge.php +++ b/bridges/RoadAndTrackBridge.php @@ -49,7 +49,7 @@ class RoadAndTrackBridge extends BridgeAbstract $item['title'] = $title->innertext; } - $item['author'] = $article->find('.byline-name', 0)->innertext; + $item['author'] = $article->find('.byline-name', 0)->innertext ?? ''; $item['timestamp'] = strtotime($article->find('.content-info-date', 0)->getAttribute('datetime')); $content = $article->find('.content-container', 0); diff --git a/bridges/TikTokBridge.php b/bridges/TikTokBridge.php index 48de2ba5..1a30570d 100644 --- a/bridges/TikTokBridge.php +++ b/bridges/TikTokBridge.php @@ -39,7 +39,8 @@ class TikTokBridge extends BridgeAbstract // todo: find proper link to tiktok item $link = $div->find('a', 0)->href; - $image = $div->find('img', 0)->src; + $image = $div->find('img', 0)->src ?? ''; + $views = $div->find('strong.video-count', 0)->plaintext; if ($link === 'https://www.tiktok.com/') { diff --git a/lib/contents.php b/lib/contents.php index 67ad2d3f..c7b74242 100644 --- a/lib/contents.php +++ b/lib/contents.php @@ -222,13 +222,14 @@ function _http_request(string $url, array $config = []): array 'if_not_modified_since' => null, 'retries' => 3, 'max_filesize' => null, + 'max_redirections' => 5, ]; $config = array_merge($defaults, $config); $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); - curl_setopt($ch, CURLOPT_MAXREDIRS, 5); + curl_setopt($ch, CURLOPT_MAXREDIRS, $config['max_redirections']); curl_setopt($ch, CURLOPT_HEADER, false); $httpHeaders = []; foreach ($config['headers'] as $name => $value) { @@ -302,10 +303,12 @@ function _http_request(string $url, array $config = []): array } if ($attempts > $config['retries']) { // Finally give up + $curl_error = curl_error($ch); + $curl_errno = curl_errno($ch); throw new HttpException(sprintf( 'cURL error %s: %s (%s) for %s', - curl_error($ch), - curl_errno($ch), + $curl_error, + $curl_errno, 'https://curl.haxx.se/libcurl/c/libcurl-errors.html', $url ));