From eb272622500a633a8ab73f88943436365eddb150 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sun, 23 Sep 2018 14:22:48 +0200 Subject: [PATCH] Fix video fetching from RSS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The way it was build worked from the debug page but not from the RSS feed (which is the mostly common use …) --- src/FeedBundle/Extractor/RedditVideo.php | 17 +++++++++++++---- tests/FeedBundle/Extractor/RedditVideoTest.php | 4 +--- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/FeedBundle/Extractor/RedditVideo.php b/src/FeedBundle/Extractor/RedditVideo.php index e625ef4d..60b8d7c5 100644 --- a/src/FeedBundle/Extractor/RedditVideo.php +++ b/src/FeedBundle/Extractor/RedditVideo.php @@ -20,15 +20,24 @@ public function match($url) return false; } - if (!\in_array($host, ['reddit.com', 'www.reddit.com'], true)) { + // usually happen when fetching from the debug page + if (\in_array($host, ['reddit.com', 'www.reddit.com'], true)) { + $jsonUrl = 'https://' . $host . $path . '/.json'; + } + + // usually happen when fetching from the RSS feed + if (\in_array($host, ['v.redd.it'], true)) { + $jsonUrl = 'https://www.reddit.com/video/' . $path . '/.json'; + } + + if (!isset($jsonUrl)) { return false; } - $url = 'https://' . $host . $path . '/.json'; - $url = str_replace('//.json', '/.json', $url); + $jsonUrl = str_replace('//', '/', $jsonUrl); try { - $data = $this->client->get($url)->json(); + $data = $this->client->get($jsonUrl)->json(); } catch (RequestException $e) { return false; } diff --git a/tests/FeedBundle/Extractor/RedditVideoTest.php b/tests/FeedBundle/Extractor/RedditVideoTest.php index f25c6c05..6c0a8e6e 100644 --- a/tests/FeedBundle/Extractor/RedditVideoTest.php +++ b/tests/FeedBundle/Extractor/RedditVideoTest.php @@ -16,8 +16,6 @@ class RedditVideoTest extends TestCase public function dataMatch() { return [ - ['https://v.redd.it/funfg141o3hz', false], - ['http://v.redd.it/', false], ['https://goog.co', false], ['http://user@:80', false], ]; @@ -45,7 +43,7 @@ public function testMatchRedditBadRequest() $redditVideo = new RedditVideo(); $redditVideo->setClient($client); - $this->assertFalse($redditVideo->match('https://www.reddit.com/r/videos/comments/6rrwyj/that_small_heart_attack/')); + $this->assertFalse($redditVideo->match('https://v.redd.it/funfg141o3hz')); } public function testMatchRedditNotAVideo()