Merge pull request #659 from Soulsuck24/development

This commit is contained in:
Serene
2022-09-20 17:24:54 +10:00
committed by GitHub
3 changed files with 21 additions and 32 deletions

View File

@@ -27,6 +27,8 @@ class DownloadFactory:
sanitised_url = DownloadFactory.sanitise_url(url) sanitised_url = DownloadFactory.sanitise_url(url)
if re.match(r'(i\.)?imgur.*\.gif.+$', sanitised_url): if re.match(r'(i\.)?imgur.*\.gif.+$', sanitised_url):
return Imgur return Imgur
elif re.match(r'(i\.)?(redgifs|gifdeliverynetwork)', sanitised_url):
return Redgifs
elif re.match(r'.*/.*\.\w{3,4}(\?[\w;&=]*)?$', sanitised_url) and \ elif re.match(r'.*/.*\.\w{3,4}(\?[\w;&=]*)?$', sanitised_url) and \
not DownloadFactory.is_web_resource(sanitised_url): not DownloadFactory.is_web_resource(sanitised_url):
return Direct return Direct
@@ -40,8 +42,6 @@ class DownloadFactory:
return Gfycat return Gfycat
elif re.match(r'(m\.)?imgur.*', sanitised_url): elif re.match(r'(m\.)?imgur.*', sanitised_url):
return Imgur return Imgur
elif re.match(r'(redgifs|gifdeliverynetwork)', sanitised_url):
return Redgifs
elif re.match(r'reddit\.com/r/', sanitised_url): elif re.match(r'reddit\.com/r/', sanitised_url):
return SelfPost return SelfPost
elif re.match(r'(m\.)?youtu\.?be', sanitised_url): elif re.match(r'(m\.)?youtu\.?be', sanitised_url):

View File

@@ -24,16 +24,11 @@ class Redgifs(BaseDownloader):
@staticmethod @staticmethod
def _get_link(url: str) -> set[str]: def _get_link(url: str) -> set[str]:
try: try:
redgif_id = re.match(r'.*/(.*?)/?$', url).group(1) redgif_id = re.match(r'.*/(.*?)(\..{0,})?$', url).group(1)
except AttributeError: except AttributeError:
raise SiteDownloaderError(f'Could not extract Redgifs ID from {url}') raise SiteDownloaderError(f'Could not extract Redgifs ID from {url}')
headers = { content = Redgifs.retrieve_url(f'https://api.redgifs.com/v2/gifs/{redgif_id}')
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/90.0.4430.93 Safari/537.36',
}
content = Redgifs.retrieve_url(f'https://api.redgifs.com/v2/gifs/{redgif_id}', headers=headers)
if content is None: if content is None:
raise SiteDownloaderError('Could not read the page source') raise SiteDownloaderError('Could not read the page source')
@@ -50,9 +45,7 @@ class Redgifs(BaseDownloader):
elif response_json['gif']['type'] == 2: # type 2 is an image elif response_json['gif']['type'] == 2: # type 2 is an image
if response_json['gif']['gallery']: if response_json['gif']['gallery']:
content = Redgifs.retrieve_url( content = Redgifs.retrieve_url(
f'https://api.redgifs.com/v2/gallery/{response_json["gif"]["gallery"]}', f'https://api.redgifs.com/v2/gallery/{response_json["gif"]["gallery"]}')
headers=headers,
)
response_json = json.loads(content.text) response_json = json.loads(content.text)
out = {p['urls']['hd'] for p in response_json['gifs']} out = {p['urls']['hd'] for p in response_json['gifs']}
else: else:
@@ -62,14 +55,7 @@ class Redgifs(BaseDownloader):
except (KeyError, AttributeError): except (KeyError, AttributeError):
raise SiteDownloaderError('Failed to find JSON data in page') raise SiteDownloaderError('Failed to find JSON data in page')
# returned domain seems to be being phased out # Update subdomain if old one is returned
out = {re.sub('thumbs2', 'thumbs3', link) for link in out} out = {re.sub('thumbs2', 'thumbs3', link) for link in out}
out = {Redgifs._clean_thumbs4_link(link) for link in out} out = {re.sub('thumbs3', 'thumbs4', link) for link in out}
return out
@staticmethod
def _clean_thumbs4_link(url: str) -> str:
split_url = urllib.parse.urlsplit(url)
out = split_url.scheme + '://' + split_url.netloc + split_url.path
out = re.sub('thumbs4', 'thumbs3', out)
return out return out

View File

@@ -2,6 +2,7 @@
# coding=utf-8 # coding=utf-8
from unittest.mock import Mock from unittest.mock import Mock
import re
import pytest import pytest
@@ -12,24 +13,26 @@ from bdfr.site_downloaders.redgifs import Redgifs
@pytest.mark.online @pytest.mark.online
@pytest.mark.parametrize(('test_url', 'expected'), ( @pytest.mark.parametrize(('test_url', 'expected'), (
('https://redgifs.com/watch/frighteningvictorioussalamander', ('https://redgifs.com/watch/frighteningvictorioussalamander',
{'https://thumbs3.redgifs.com/FrighteningVictoriousSalamander.mp4'}), {'FrighteningVictoriousSalamander.mp4'}),
('https://redgifs.com/watch/springgreendecisivetaruca', ('https://redgifs.com/watch/springgreendecisivetaruca',
{'https://thumbs3.redgifs.com/SpringgreenDecisiveTaruca.mp4'}), {'SpringgreenDecisiveTaruca.mp4'}),
('https://www.redgifs.com/watch/palegoldenrodrawhalibut', ('https://www.redgifs.com/watch/palegoldenrodrawhalibut',
{'https://thumbs3.redgifs.com/PalegoldenrodRawHalibut.mp4'}), {'PalegoldenrodRawHalibut.mp4'}),
('https://redgifs.com/watch/hollowintentsnowyowl', ('https://redgifs.com/watch/hollowintentsnowyowl',
{'https://thumbs3.redgifs.com/HollowIntentSnowyowl-large.jpg'}), {'HollowIntentSnowyowl-large.jpg'}),
('https://www.redgifs.com/watch/lustrousstickywaxwing', ('https://www.redgifs.com/watch/lustrousstickywaxwing',
{'https://thumbs3.redgifs.com/EntireEnchantingHypsilophodon-large.jpg', {'EntireEnchantingHypsilophodon-large.jpg',
'https://thumbs3.redgifs.com/FancyMagnificentAdamsstaghornedbeetle-large.jpg', 'FancyMagnificentAdamsstaghornedbeetle-large.jpg',
'https://thumbs3.redgifs.com/LustrousStickyWaxwing-large.jpg', 'LustrousStickyWaxwing-large.jpg',
'https://thumbs3.redgifs.com/ParchedWindyArmyworm-large.jpg', 'ParchedWindyArmyworm-large.jpg',
'https://thumbs3.redgifs.com/ThunderousColorlessErmine-large.jpg', 'ThunderousColorlessErmine-large.jpg',
'https://thumbs3.redgifs.com/UnripeUnkemptWoodpecker-large.jpg'}), 'UnripeUnkemptWoodpecker-large.jpg'}),
)) ))
def test_get_link(test_url: str, expected: set[str]): def test_get_link(test_url: str, expected: set[str]):
result = Redgifs._get_link(test_url) result = Redgifs._get_link(test_url)
assert result == expected result = list(result)
patterns = [r'https://thumbs\d\.redgifs\.com/' + e + r'.*' for e in expected]
assert all([re.match(p, r) for p in patterns] for r in result)
@pytest.mark.online @pytest.mark.online