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