From 33312687acce22c46c864c14904397e4655eecdd Mon Sep 17 00:00:00 2001 From: Eli Lipsitz Date: Sun, 12 Sep 2021 16:50:31 -0500 Subject: [PATCH] imgur: download videos as mp4 instead of gif Some imgur URLS have the extension ".gifv" and show up as a gif, even though they're actually supposed to be mp4 videos. Imgur serves all videos/gifs as both .gif and .mp4. The image dict has a key "prefer_video" to distinguish the two. This commit overrides the .gif extension if "prefer_video" is true to ensure we download the submission as originally intended. --- bdfr/site_downloaders/imgur.py | 6 +++++- tests/site_downloaders/test_imgur.py | 10 +++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/bdfr/site_downloaders/imgur.py b/bdfr/site_downloaders/imgur.py index f0b7012..a3e3135 100644 --- a/bdfr/site_downloaders/imgur.py +++ b/bdfr/site_downloaders/imgur.py @@ -32,7 +32,11 @@ class Imgur(BaseDownloader): return out def _compute_image_url(self, image: dict) -> Resource: - image_url = 'https://i.imgur.com/' + image['hash'] + self._validate_extension(image['ext']) + ext = self._validate_extension(image['ext']) + if image.get('prefer_video', False): + ext = '.mp4' + + image_url = 'https://i.imgur.com/' + image['hash'] + ext return Resource(self.post, image_url, Resource.retry_download(image_url)) @staticmethod diff --git a/tests/site_downloaders/test_imgur.py b/tests/site_downloaders/test_imgur.py index bfb7405..4c754ec 100644 --- a/tests/site_downloaders/test_imgur.py +++ b/tests/site_downloaders/test_imgur.py @@ -111,7 +111,7 @@ def test_imgur_extension_validation_bad(test_extension: str): ), ( 'https://imgur.com/gallery/IjJJdlC', - ('7227d4312a9779b74302724a0cfa9081',), + ('740b006cf9ec9d6f734b6e8f5130bdab',), ), ( 'https://imgur.com/a/dcc84Gt', @@ -142,6 +142,14 @@ def test_imgur_extension_validation_bad(test_extension: str): 'https://imgur.com/ubYwpbk.GIFV', ('d4a774aac1667783f9ed3a1bd02fac0c',), ), + ( + 'https://i.imgur.com/j1CNCZY.gifv', + ('58e7e6d972058c18b7ecde910ca147e3',), + ), + ( + 'https://i.imgur.com/uTvtQsw.gifv', + ('46c86533aa60fc0e09f2a758513e3ac2',), + ), )) def test_find_resources(test_url: str, expected_hashes: list[str]): mock_download = Mock()