diff --git a/redditdownloader/processing/handlers/__init__.py b/redditdownloader/processing/handlers/__init__.py index e6328d97..bec686c6 100644 --- a/redditdownloader/processing/handlers/__init__.py +++ b/redditdownloader/processing/handlers/__init__.py @@ -4,9 +4,9 @@ def sorted_list(): """ A list of all available static Handlers, pre-sorted by order. """ - from processing.handlers import github, imgur, generic_newspaper, reddit_handler, ytdl, tumblr, direct_link, gfycat + from processing.handlers import github, imgur, generic_newspaper, reddit_handler, ytdl, tumblr, direct_link, gfycat, redgifs return sorted([ - generic_newspaper, github, imgur, reddit_handler, ytdl, tumblr, direct_link, gfycat + generic_newspaper, github, imgur, reddit_handler, ytdl, tumblr, direct_link, gfycat, redgifs ], key=lambda x: x.order, reverse=False) diff --git a/redditdownloader/processing/handlers/redgifs.py b/redditdownloader/processing/handlers/redgifs.py new file mode 100644 index 00000000..03e15984 --- /dev/null +++ b/redditdownloader/processing/handlers/redgifs.py @@ -0,0 +1,37 @@ +import re +from processing.wrappers import http_downloader + +tag = 'redgifs' +order = 1 + +""" + This Handler attempts to convert Gyfcat links into direct video links. +""" + +format_opts = ["webmUrl", "mp4Url", "gifUrl"] + + +def handle(task, progress): + url = task.url + try: + redgif_id = re.match(r'.*/(.*?)/?$', url).group(1) + except AttributeError: + return False + + headers = { + '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/gfycats/{redgif_id}', headers=headers) + + if content is None: + return False + + try: + out = json.loads(content.text)['gif']['urls']["gif"] + except (KeyError, AttributeError): + return False + except json.JSONDecodeError as e: + return False + progress.set_status("Downloading from redgifs..." % opt) + return http_downloader.download_binary(out, task.file, prog=progress, handler_id=tag)