Skip to content

Commit

Permalink
[redgifs][gfycat] provide fallback URLs (fixes #1962)
Browse files Browse the repository at this point in the history
and extend the 'format' option
  • Loading branch information
mikf committed Oct 22, 2021
1 parent b6443c5 commit 8bb442f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 27 deletions.
34 changes: 21 additions & 13 deletions docs/configuration.rst
Expand Up @@ -1237,16 +1237,20 @@ Description
extractor.gfycat.format
-----------------------
Type
``string``
* ``list`` of ``strings``
* ``string``
Default
``"mp4"``
``["mp4", "webm", "mobile", "gif"]``
Description
The name of the preferred animation format, which can be one of
``"mp4"``, ``"webm"``, ``"gif"``, ``"webp"``, or ``"mjpg"``.
List of names of the preferred animation format, which can be
``"mp4"``, ``"webm"``, ``"mobile"``, ``"gif"``, or ``"webp"``.

If a selected format is not available, the next one in the list will be
tried until an available format is found.

If the selected format is not available, ``"mp4"``, ``"webm"``
and ``"gif"`` (in that order) will be tried instead, until an
available format is found.
If the format is given as ``string``, it will be extended with
``["mp4", "webm", "mobile", "gif"]``. Use a list with one element to
restrict it to only one possible format.


extractor.hentaifoundry.include
Expand Down Expand Up @@ -1864,16 +1868,20 @@ Description
extractor.redgifs.format
------------------------
Type
``string``
* ``list`` of ``strings``
* ``string``
Default
``"mp4"``
``["mp4", "webm", "mobile", "gif"]``
Description
The name of the preferred format, which can be one of
List of names of the preferred animation format, which can be
``"mp4"``, ``"webm"``, ``"gif"``, ``"webp"``, ``"mobile"``, or ``"mini"``.

If the selected format is not available, ``"mp4"``, ``"webm"``
and ``"gif"`` (in that order) will be tried instead, until an
available format is found.
If a selected format is not available, the next one in the list will be
tried until an available format is found.

If the format is given as ``string``, it will be extended with
``["mp4", "webm", "mobile", "gif"]``. Use a list with one element to
restrict it to only one possible format.


extractor.sankakucomplex.embeds
Expand Down
4 changes: 2 additions & 2 deletions docs/gallery-dl.conf
Expand Up @@ -107,7 +107,7 @@
},
"gfycat":
{
"format": "mp4"
"format": ["mp4", "webm", "mobile", "gif"]
},
"hentaifoundry":
{
Expand Down Expand Up @@ -222,7 +222,7 @@
},
"redgifs":
{
"format": "mp4"
"format": ["mp4", "webm", "mobile", "gif"]
},
"sankakucomplex":
{
Expand Down
31 changes: 19 additions & 12 deletions gallery_dl/extractor/gfycat.py
Expand Up @@ -22,31 +22,39 @@ class GfycatExtractor(Extractor):
def __init__(self, match):
Extractor.__init__(self, match)
self.key = match.group(1).lower()
self.formats = (self.config("format", "mp4"), "mp4", "webm", "gif")

formats = self.config("format")
if formats is None:
formats = ("mp4", "webm", "mobile", "gif")
elif isinstance(formats, str):
formats = (formats, "mp4", "webm", "mobile", "gif")
self.formats = formats

def items(self):
metadata = self.metadata()
for gfycat in self.gfycats():
if "gfyName" not in gfycat:
self.log.warning("Skipping '%s' (malformed)", gfycat["gfyId"])
continue
url = self._select_format(gfycat)
url = self._process(gfycat)
gfycat.update(metadata)
gfycat["date"] = text.parse_timestamp(gfycat.get("createDate"))
yield Message.Directory, gfycat
yield Message.Url, url, gfycat

def _select_format(self, gfyitem):
def _process(self, gfycat):
gfycat["_fallback"] = formats = self._formats(gfycat)
gfycat["date"] = text.parse_timestamp(gfycat.get("createDate"))
return next(formats, "")

def _formats(self, gfycat):
for fmt in self.formats:
key = fmt + "Url"
if key in gfyitem:
url = gfyitem[key]
if key in gfycat:
url = gfycat[key]
if url.startswith("http:"):
url = "https" + url[4:]
gfyitem["extension"] = url.rpartition(".")[2]
return url
gfyitem["extension"] = ""
return ""
gfycat["extension"] = url.rpartition(".")[2]
yield url

def metadata(self):
return {}
Expand Down Expand Up @@ -146,8 +154,7 @@ def items(self):
if "gfyName" not in gfycat:
self.log.warning("Skipping '%s' (malformed)", gfycat["gfyId"])
return
url = self._select_format(gfycat)
gfycat["date"] = text.parse_timestamp(gfycat.get("createDate"))
url = self._process(gfycat)
yield Message.Directory, gfycat
yield Message.Url, url, gfycat

Expand Down

0 comments on commit 8bb442f

Please sign in to comment.