[twitter] add 'size' option (#1881)

This commit is contained in:
Mike Fährmann
2021-10-05 18:58:10 +02:00
parent df8050b81d
commit cd66c3c415
2 changed files with 31 additions and 19 deletions

View File

@@ -1241,7 +1241,7 @@ Default
``"mp4"``
Description
The name of the preferred animation format, which can be one of
``"mp4"``, ``"webm"``, ``"gif"``, ``"webp"`` or ``"mjpg"``.
``"mp4"``, ``"webm"``, ``"gif"``, ``"webp"``, or ``"mjpg"``.
If the selected format is not available, ``"mp4"``, ``"webm"``
and ``"gif"`` (in that order) will be tried instead, until an
@@ -1266,16 +1266,6 @@ Description
You can use ``"all"`` instead of listing all values separately.
extractor.hentainexus.original
------------------------------
Type
``bool``
Default
``true``
Description
Download original files instead of WebP versions.
extractor.hitomi.metadata
-------------------------
Type
@@ -1842,8 +1832,7 @@ Default
``"mp4"``
Description
The name of the preferred format, which can be one of
``"mp4"``, ``"webm"``, ``"gif"``, ``"webp"``, ``"mobile"``,
or ``"mini"``.
``"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
@@ -1972,6 +1961,21 @@ Description
<https://help.twitter.com/en/using-twitter/twitter-conversations>`__.
extractor.twitter.size
----------------------
Type
``list`` of ``strings``
Default
``["orig", "large", "medium", "small"]``
Description
The image version to download.
Any entries after the first one will be used for potential
`fallback <extractor.*.fallback_>`_ URLs.
Known available sizes are
``4096x4096``, ``orig``, ``large``, ``medium``, and ``small``.
extractor.twitter.logout
------------------------
Type

View File

@@ -41,6 +41,16 @@ class TwitterExtractor(Extractor):
self.cards = self.config("cards", False)
self._user_cache = {}
size = self.config("size")
if size is None:
self._size_image = "orig"
self._size_fallback = ("large", "medium", "small")
else:
if isinstance(size, str):
size = size.split(",")
self._size_image = size[0]
self._size_fallback = size[1:]
def items(self):
self.login()
metadata = self.metadata()
@@ -115,7 +125,7 @@ class TwitterExtractor(Extractor):
base, _, fmt = url.rpartition(".")
base += "?format=" + fmt + "&name="
files.append(text.nameext_from_url(url, {
"url" : base + "orig",
"url" : base + self._size_image,
"width" : width,
"height" : height,
"_fallback": self._image_fallback(base),
@@ -123,11 +133,9 @@ class TwitterExtractor(Extractor):
else:
files.append({"url": media["media_url"]})
@staticmethod
def _image_fallback(base):
yield base + "large"
yield base + "medium"
yield base + "small"
def _image_fallback(self, base):
for fmt in self._size_fallback:
yield base + fmt
def _extract_card(self, tweet, files):
card = tweet["card"]