From 106d7596b105f021e931fa064ca73d6591843a7b Mon Sep 17 00:00:00 2001 From: SoulSuck24 <79275800+Soulsuck24@users.noreply.github.com> Date: Sun, 18 Sep 2022 23:27:17 -0400 Subject: [PATCH 1/3] Imgur updates Update Imgur logic to cover malformed links that cause a redirect leading to the html of the page being saved as an image. --- bdfr/site_downloaders/download_factory.py | 4 +--- bdfr/site_downloaders/imgur.py | 10 ++++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/bdfr/site_downloaders/download_factory.py b/bdfr/site_downloaders/download_factory.py index 96e9a429..e618b68f 100644 --- a/bdfr/site_downloaders/download_factory.py +++ b/bdfr/site_downloaders/download_factory.py @@ -25,7 +25,7 @@ class DownloadFactory: @staticmethod def pull_lever(url: str) -> Type[BaseDownloader]: sanitised_url = DownloadFactory.sanitise_url(url) - if re.match(r'(i\.)?imgur.*\.gif.+$', sanitised_url): + if re.match(r'(i\.|m\.)?imgur', sanitised_url): return Imgur elif re.match(r'.*/.*\.\w{3,4}(\?[\w;&=]*)?$', sanitised_url) and \ not DownloadFactory.is_web_resource(sanitised_url): @@ -38,8 +38,6 @@ def pull_lever(url: str) -> Type[BaseDownloader]: return Gallery elif re.match(r'gfycat\.', sanitised_url): return Gfycat - elif re.match(r'(m\.)?imgur.*', sanitised_url): - return Imgur elif re.match(r'(redgifs|gifdeliverynetwork)', sanitised_url): return Redgifs elif re.match(r'reddit\.com/r/', sanitised_url): diff --git a/bdfr/site_downloaders/imgur.py b/bdfr/site_downloaders/imgur.py index 1f669d0c..f8957857 100644 --- a/bdfr/site_downloaders/imgur.py +++ b/bdfr/site_downloaders/imgur.py @@ -41,10 +41,12 @@ def _compute_image_url(self, image: dict) -> Resource: @staticmethod def _get_data(link: str) -> dict: - link = link.rstrip('?') - if re.match(r'(?i).*\.gif.+$', link): - link = link.replace('i.imgur', 'imgur') - link = re.sub('(?i)\\.gif.+$', '', link) + try: + imgur_id = re.match(r'.*/(.*?)(\..{0,})?$', link).group(1) + gallery = 'a/' if re.search(r'.*/(.*?)(gallery/|a/)', link) else '' + link = f'https://imgur.com/{gallery}{imgur_id}' + except AttributeError: + raise SiteDownloaderError(f'Could not extract Imgur ID from {link}') res = Imgur.retrieve_url(link, cookies={'over18': '1', 'postpagebeta': '0'}) From 1dff7500e7b2a08b3ba7c210bec5567506e27fe9 Mon Sep 17 00:00:00 2001 From: Serene-Arc Date: Tue, 20 Sep 2022 17:33:44 +1000 Subject: [PATCH 2/3] Remove duplicate entries --- bdfr/site_downloaders/download_factory.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/bdfr/site_downloaders/download_factory.py b/bdfr/site_downloaders/download_factory.py index 0ebbf2c7..f5e8d99b 100644 --- a/bdfr/site_downloaders/download_factory.py +++ b/bdfr/site_downloaders/download_factory.py @@ -40,10 +40,6 @@ def pull_lever(url: str) -> Type[BaseDownloader]: return Gallery elif re.match(r'gfycat\.', sanitised_url): return Gfycat - elif re.match(r'(redgifs|gifdeliverynetwork)', sanitised_url): - return Redgifs - elif re.match(r'(m\.)?imgur.*', sanitised_url): - return Imgur elif re.match(r'reddit\.com/r/', sanitised_url): return SelfPost elif re.match(r'(m\.)?youtu\.?be', sanitised_url): From cd05bc388ec3d71fcf7f20f9e3e2708b0d0e26b9 Mon Sep 17 00:00:00 2001 From: Serene-Arc Date: Tue, 20 Sep 2022 17:33:51 +1000 Subject: [PATCH 3/3] Fix tests --- tests/site_downloaders/test_download_factory.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/site_downloaders/test_download_factory.py b/tests/site_downloaders/test_download_factory.py index dcb5303b..d3fec6f3 100644 --- a/tests/site_downloaders/test_download_factory.py +++ b/tests/site_downloaders/test_download_factory.py @@ -24,12 +24,12 @@ @pytest.mark.parametrize(('test_submission_url', 'expected_class'), ( ('https://www.reddit.com/r/TwoXChromosomes/comments/lu29zn/i_refuse_to_live_my_life' '_in_anything_but_comfort/', SelfPost), - ('https://i.imgur.com/bZx1SJQ.jpg', Direct), + ('https://i.imgur.com/bZx1SJQ.jpg', Imgur), ('https://i.redd.it/affyv0axd5k61.png', Direct), - ('https://imgur.com/3ls94yv.jpeg', Direct), + ('https://imgur.com/3ls94yv.jpeg', Imgur), ('https://i.imgur.com/BuzvZwb.gifv', Imgur), ('https://imgur.com/BuzvZwb.gifv', Imgur), - ('https://i.imgur.com/6fNdLst.gif', Direct), + ('https://i.imgur.com/6fNdLst.gif', Imgur), ('https://imgur.com/a/MkxAzeg', Imgur), ('https://i.imgur.com/OGeVuAe.giff', Imgur), ('https://www.reddit.com/gallery/lu93m7', Gallery), @@ -40,7 +40,7 @@ ('https://www.gifdeliverynetwork.com/repulsivefinishedandalusianhorse', Redgifs), ('https://youtu.be/DevfjHOhuFc', Youtube), ('https://m.youtube.com/watch?v=kr-FeojxzUM', Youtube), - ('https://i.imgur.com/3SKrQfK.jpg?1', Direct), + ('https://i.imgur.com/3SKrQfK.jpg?1', Imgur), ('https://dynasty-scans.com/system/images_images/000/017/819/original/80215103_p0.png?1612232781', Direct), ('https://m.imgur.com/a/py3RW0j', Imgur), ('https://v.redd.it/9z1dnk3xr5k61', VReddit),