Combine variously sized previews to a single key
This commit is contained in:
@@ -31,6 +31,7 @@ class Enum(aenum.Enum):
|
||||
return cls(value)
|
||||
|
||||
|
||||
# Frozen, so that it can be used in sets
|
||||
@attr.s(frozen=True, slots=True, kw_only=kw_only)
|
||||
class Image:
|
||||
#: URL to the image
|
||||
|
@@ -65,45 +65,44 @@ class ImageAttachment(Attachment):
|
||||
width = attr.ib(None, converter=lambda x: None if x is None else int(x))
|
||||
#: Height of original image
|
||||
height = attr.ib(None, converter=lambda x: None if x is None else int(x))
|
||||
|
||||
#: Whether the image is animated
|
||||
is_animated = attr.ib(None)
|
||||
|
||||
#: A thumbnail of the image
|
||||
thumbnail = attr.ib(None)
|
||||
#: A medium preview of the image
|
||||
preview = attr.ib(None)
|
||||
#: A large preview of the image
|
||||
large_preview = attr.ib(None)
|
||||
#: An animated preview of the image (e.g. for GIFs)
|
||||
animated_preview = attr.ib(None)
|
||||
#: A set, containing variously sized / various types of previews of the image
|
||||
previews = attr.ib(factory=set)
|
||||
|
||||
@classmethod
|
||||
def _from_graphql(cls, data):
|
||||
previews = {
|
||||
Image._from_uri_or_none(data.get("thumbnail")),
|
||||
Image._from_uri_or_none(data.get("preview") or data.get("preview_image")),
|
||||
Image._from_uri_or_none(data.get("large_preview")),
|
||||
Image._from_uri_or_none(data.get("animated_image")),
|
||||
}
|
||||
|
||||
return cls(
|
||||
original_extension=data.get("original_extension")
|
||||
or (data["filename"].split("-")[0] if data.get("filename") else None),
|
||||
width=data.get("original_dimensions", {}).get("width"),
|
||||
height=data.get("original_dimensions", {}).get("height"),
|
||||
is_animated=data["__typename"] == "MessageAnimatedImage",
|
||||
thumbnail=Image._from_uri_or_none(data.get("thumbnail")),
|
||||
preview=Image._from_uri_or_none(
|
||||
data.get("preview") or data.get("preview_image")
|
||||
),
|
||||
large_preview=Image._from_uri_or_none(data.get("large_preview")),
|
||||
animated_preview=Image._from_uri_or_none(data.get("animated_image")),
|
||||
previews={p for p in previews if p},
|
||||
uid=data.get("legacy_attachment_id"),
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _from_list(cls, data):
|
||||
data = data["node"]
|
||||
|
||||
previews = {
|
||||
Image._from_uri_or_none(data["image"]),
|
||||
Image._from_uri(data["image1"]),
|
||||
Image._from_uri(data["image2"]),
|
||||
}
|
||||
|
||||
return cls(
|
||||
width=data["original_dimensions"].get("x"),
|
||||
height=data["original_dimensions"].get("y"),
|
||||
thumbnail=Image._from_uri_or_none(data["image"]),
|
||||
large_preview=Image._from_uri(data["image2"]),
|
||||
preview=Image._from_uri(data["image1"]),
|
||||
previews={p for p in previews if p},
|
||||
uid=data["legacy_attachment_id"],
|
||||
)
|
||||
|
||||
@@ -122,47 +121,52 @@ class VideoAttachment(Attachment):
|
||||
duration = attr.ib(None)
|
||||
#: URL to very compressed preview video
|
||||
preview_url = attr.ib(None)
|
||||
|
||||
#: A small preview image of the video
|
||||
small_image = attr.ib(None)
|
||||
#: A medium preview image of the video
|
||||
medium_image = attr.ib(None)
|
||||
#: A large preview image of the video
|
||||
large_image = attr.ib(None)
|
||||
#: A set, containing variously sized previews of the video
|
||||
previews = attr.ib(factory=set)
|
||||
|
||||
@classmethod
|
||||
def _from_graphql(cls, data, size=None):
|
||||
previews = {
|
||||
Image._from_uri_or_none(data.get("chat_image")),
|
||||
Image._from_uri_or_none(data.get("inbox_image")),
|
||||
Image._from_uri_or_none(data.get("large_image")),
|
||||
}
|
||||
|
||||
return cls(
|
||||
size=size,
|
||||
width=data.get("original_dimensions", {}).get("width"),
|
||||
height=data.get("original_dimensions", {}).get("height"),
|
||||
duration=_util.millis_to_timedelta(data.get("playable_duration_in_ms")),
|
||||
preview_url=data.get("playable_url"),
|
||||
small_image=Image._from_uri_or_none(data.get("chat_image")),
|
||||
medium_image=Image._from_uri_or_none(data.get("inbox_image")),
|
||||
large_image=Image._from_uri_or_none(data.get("large_image")),
|
||||
previews={p for p in previews if p},
|
||||
uid=data.get("legacy_attachment_id"),
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _from_subattachment(cls, data):
|
||||
media = data["media"]
|
||||
image = Image._from_uri_or_none(media.get("image"))
|
||||
|
||||
return cls(
|
||||
duration=_util.millis_to_timedelta(media.get("playable_duration_in_ms")),
|
||||
preview_url=media.get("playable_url"),
|
||||
medium_image=Image._from_uri_or_none(media.get("image")),
|
||||
previews={image} if image else {},
|
||||
uid=data["target"].get("video_id"),
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _from_list(cls, data):
|
||||
data = data["node"]
|
||||
previews = {
|
||||
Image._from_uri(data["image"]),
|
||||
Image._from_uri(data["image1"]),
|
||||
Image._from_uri(data["image2"]),
|
||||
}
|
||||
|
||||
return cls(
|
||||
width=data["original_dimensions"].get("x"),
|
||||
height=data["original_dimensions"].get("y"),
|
||||
small_image=Image._from_uri(data["image"]),
|
||||
medium_image=Image._from_uri(data["image1"]),
|
||||
large_image=Image._from_uri(data["image2"]),
|
||||
previews=previews,
|
||||
uid=data["legacy_attachment_id"],
|
||||
)
|
||||
|
||||
|
@@ -447,11 +447,13 @@ def test_share_with_video_subattachment():
|
||||
uid="2222",
|
||||
duration=datetime.timedelta(seconds=24, microseconds=469000),
|
||||
preview_url="https://video-arn2-1.xx.fbcdn.net/v/t42.9040-2/vid.mp4",
|
||||
medium_image=fbchat.Image(
|
||||
url="https://scontent-arn2-1.xx.fbcdn.net/v/t15.5256-10/p180x540/1.jpg",
|
||||
width=960,
|
||||
height=540,
|
||||
),
|
||||
previews={
|
||||
fbchat.Image(
|
||||
url="https://scontent-arn2-1.xx.fbcdn.net/v/t15.5256-10/p180x540/1.jpg",
|
||||
width=960,
|
||||
height=540,
|
||||
)
|
||||
},
|
||||
)
|
||||
],
|
||||
uid="deadbeef123",
|
||||
|
@@ -33,17 +33,19 @@ def test_imageattachment_from_list():
|
||||
uid="1234",
|
||||
width=2833,
|
||||
height=1367,
|
||||
thumbnail=fbchat.Image(
|
||||
url="https://scontent-arn2-1.xx.fbcdn.net/v/s261x260/1.jpg"
|
||||
),
|
||||
preview=fbchat.Image(
|
||||
url="https://scontent-arn2-1.xx.fbcdn.net/v/2.jpg", width=960, height=463
|
||||
),
|
||||
large_preview=fbchat.Image(
|
||||
url="https://scontent-arn2-1.xx.fbcdn.net/v/s2048x2048/3.jpg",
|
||||
width=2048,
|
||||
height=988,
|
||||
),
|
||||
previews={
|
||||
fbchat.Image(url="https://scontent-arn2-1.xx.fbcdn.net/v/s261x260/1.jpg"),
|
||||
fbchat.Image(
|
||||
url="https://scontent-arn2-1.xx.fbcdn.net/v/2.jpg",
|
||||
width=960,
|
||||
height=463,
|
||||
),
|
||||
fbchat.Image(
|
||||
url="https://scontent-arn2-1.xx.fbcdn.net/v/s2048x2048/3.jpg",
|
||||
width=2048,
|
||||
height=988,
|
||||
),
|
||||
},
|
||||
) == ImageAttachment._from_list({"node": data})
|
||||
|
||||
|
||||
@@ -71,19 +73,21 @@ def test_videoattachment_from_list():
|
||||
uid="1234",
|
||||
width=640,
|
||||
height=368,
|
||||
small_image=fbchat.Image(
|
||||
url="https://scontent-arn2-1.xx.fbcdn.net/v/t15.3394-10/p261x260/1.jpg"
|
||||
),
|
||||
medium_image=fbchat.Image(
|
||||
url="https://scontent-arn2-1.xx.fbcdn.net/v/t15.3394-10/2.jpg",
|
||||
width=640,
|
||||
height=368,
|
||||
),
|
||||
large_image=fbchat.Image(
|
||||
url="https://scontent-arn2-1.xx.fbcdn.net/v/t15.3394-10/3.jpg",
|
||||
width=640,
|
||||
height=368,
|
||||
),
|
||||
previews={
|
||||
fbchat.Image(
|
||||
url="https://scontent-arn2-1.xx.fbcdn.net/v/t15.3394-10/p261x260/1.jpg"
|
||||
),
|
||||
fbchat.Image(
|
||||
url="https://scontent-arn2-1.xx.fbcdn.net/v/t15.3394-10/2.jpg",
|
||||
width=640,
|
||||
height=368,
|
||||
),
|
||||
fbchat.Image(
|
||||
url="https://scontent-arn2-1.xx.fbcdn.net/v/t15.3394-10/3.jpg",
|
||||
width=640,
|
||||
height=368,
|
||||
),
|
||||
},
|
||||
) == VideoAttachment._from_list({"node": data})
|
||||
|
||||
|
||||
@@ -152,11 +156,11 @@ def test_graphql_to_attachment_image1():
|
||||
"width": 128,
|
||||
},
|
||||
"large_preview": {
|
||||
"uri": "https://scontent-arn2-1.xx.fbcdn.net/v/2.png",
|
||||
"uri": "https://scontent-arn2-1.xx.fbcdn.net/v/1.png",
|
||||
"height": 128,
|
||||
"width": 128,
|
||||
},
|
||||
"thumbnail": {"uri": "https://scontent-arn2-1.xx.fbcdn.net/v/p50x50/3.png"},
|
||||
"thumbnail": {"uri": "https://scontent-arn2-1.xx.fbcdn.net/v/p50x50/2.png"},
|
||||
"photo_encodings": [],
|
||||
"legacy_attachment_id": "1234",
|
||||
"original_dimensions": {"x": 128, "y": 128},
|
||||
@@ -170,15 +174,14 @@ def test_graphql_to_attachment_image1():
|
||||
width=None,
|
||||
height=None,
|
||||
is_animated=False,
|
||||
thumbnail=fbchat.Image(
|
||||
url="https://scontent-arn2-1.xx.fbcdn.net/v/p50x50/3.png"
|
||||
),
|
||||
preview=fbchat.Image(
|
||||
url="https://scontent-arn2-1.xx.fbcdn.net/v/1.png", width=128, height=128
|
||||
),
|
||||
large_preview=fbchat.Image(
|
||||
url="https://scontent-arn2-1.xx.fbcdn.net/v/2.png", width=128, height=128
|
||||
),
|
||||
previews={
|
||||
fbchat.Image(url="https://scontent-arn2-1.xx.fbcdn.net/v/p50x50/2.png"),
|
||||
fbchat.Image(
|
||||
url="https://scontent-arn2-1.xx.fbcdn.net/v/1.png",
|
||||
width=128,
|
||||
height=128,
|
||||
),
|
||||
},
|
||||
) == graphql_to_attachment(data)
|
||||
|
||||
|
||||
@@ -207,12 +210,9 @@ def test_graphql_to_attachment_image2():
|
||||
width=None,
|
||||
height=None,
|
||||
is_animated=True,
|
||||
preview=fbchat.Image(
|
||||
url="https://cdn.fbsbx.com/v/1.gif", width=128, height=128
|
||||
),
|
||||
animated_preview=fbchat.Image(
|
||||
url="https://cdn.fbsbx.com/v/1.gif", width=128, height=128
|
||||
),
|
||||
previews={
|
||||
fbchat.Image(url="https://cdn.fbsbx.com/v/1.gif", width=128, height=128)
|
||||
},
|
||||
) == graphql_to_attachment(data)
|
||||
|
||||
|
||||
@@ -249,19 +249,23 @@ def test_graphql_to_attachment_video():
|
||||
height=None,
|
||||
duration=datetime.timedelta(seconds=6),
|
||||
preview_url="https://video-arn2-1.xx.fbcdn.net/v/video-4321.mp4",
|
||||
small_image=fbchat.Image(
|
||||
url="https://scontent-arn2-1.xx.fbcdn.net/v/s168x128/1.jpg",
|
||||
width=168,
|
||||
height=96,
|
||||
),
|
||||
medium_image=fbchat.Image(
|
||||
url="https://scontent-arn2-1.xx.fbcdn.net/v/p261x260/3.jpg",
|
||||
width=452,
|
||||
height=260,
|
||||
),
|
||||
large_image=fbchat.Image(
|
||||
url="https://scontent-arn2-1.xx.fbcdn.net/v/2.jpg", width=640, height=368
|
||||
),
|
||||
previews={
|
||||
fbchat.Image(
|
||||
url="https://scontent-arn2-1.xx.fbcdn.net/v/s168x128/1.jpg",
|
||||
width=168,
|
||||
height=96,
|
||||
),
|
||||
fbchat.Image(
|
||||
url="https://scontent-arn2-1.xx.fbcdn.net/v/p261x260/3.jpg",
|
||||
width=452,
|
||||
height=260,
|
||||
),
|
||||
fbchat.Image(
|
||||
url="https://scontent-arn2-1.xx.fbcdn.net/v/2.jpg",
|
||||
width=640,
|
||||
height=368,
|
||||
),
|
||||
},
|
||||
) == graphql_to_attachment(data)
|
||||
|
||||
|
||||
@@ -346,9 +350,11 @@ def test_graphql_to_subattachment_video():
|
||||
uid="1234",
|
||||
duration=datetime.timedelta(seconds=24, microseconds=469000),
|
||||
preview_url="https://video-arn2-1.xx.fbcdn.net/v/t42.9040-2/vid.mp4",
|
||||
medium_image=fbchat.Image(
|
||||
url="https://scontent-arn2-1.xx.fbcdn.net/v/t15.5256-10/p180x540/1.jpg",
|
||||
width=960,
|
||||
height=540,
|
||||
),
|
||||
previews={
|
||||
fbchat.Image(
|
||||
url="https://scontent-arn2-1.xx.fbcdn.net/v/t15.5256-10/p180x540/1.jpg",
|
||||
width=960,
|
||||
height=540,
|
||||
)
|
||||
},
|
||||
) == graphql_to_subattachment(data)
|
||||
|
Reference in New Issue
Block a user