From 789d9d8ca15993ce938c0220e5cc93769c80a3ce Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Thu, 7 Mar 2019 21:22:56 +0100 Subject: [PATCH] Split graphql_to_attachment into smaller methods --- fbchat/_file.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ fbchat/_graphql.py | 38 ++++---------------------------------- 2 files changed, 50 insertions(+), 34 deletions(-) diff --git a/fbchat/_file.py b/fbchat/_file.py index 85b2055..4b858a7 100644 --- a/fbchat/_file.py +++ b/fbchat/_file.py @@ -21,6 +21,15 @@ class FileAttachment(Attachment): # Put here for backwards compatibility, so that the init argument order is preserved uid = attr.ib(None) + @classmethod + def _from_graphql(cls, data): + return cls( + url=data.get("url"), + name=data.get("filename"), + is_malicious=data.get("is_malicious"), + uid=data.get("message_file_fbid"), + ) + @attr.s(cmp=False) class AudioAttachment(Attachment): @@ -38,6 +47,15 @@ class AudioAttachment(Attachment): # Put here for backwards compatibility, so that the init argument order is preserved uid = attr.ib(None) + @classmethod + def _from_graphql(cls, data): + return cls( + filename=data.get("filename"), + url=data.get("playable_url"), + duration=data.get("playable_duration_in_ms"), + audio_type=data.get("audio_type"), + ) + @attr.s(cmp=False, init=False) class ImageAttachment(Attachment): @@ -122,6 +140,21 @@ class ImageAttachment(Attachment): self.animated_preview_width = animated_preview.get("width") self.animated_preview_height = animated_preview.get("height") + @classmethod + def _from_graphql(cls, data): + 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_url=data.get("thumbnail", {}).get("uri"), + preview=data.get("preview") or data.get("preview_image"), + large_preview=data.get("large_preview"), + animated_preview=data.get("animated_image"), + uid=data.get("legacy_attachment_id"), + ) + @attr.s(cmp=False, init=False) class VideoAttachment(Attachment): @@ -195,3 +228,16 @@ class VideoAttachment(Attachment): self.large_image_url = large_image.get("uri") self.large_image_width = large_image.get("width") self.large_image_height = large_image.get("height") + + @classmethod + def _from_graphql(cls, data): + return cls( + width=data.get("original_dimensions", {}).get("width"), + height=data.get("original_dimensions", {}).get("height"), + duration=data.get("playable_duration_in_ms"), + preview_url=data.get("playable_url"), + small_image=data.get("chat_image"), + medium_image=data.get("inbox_image"), + large_image=data.get("large_image"), + uid=data.get("legacy_attachment_id"), + ) diff --git a/fbchat/_graphql.py b/fbchat/_graphql.py index df3c1a6..932c5c8 100644 --- a/fbchat/_graphql.py +++ b/fbchat/_graphql.py @@ -63,43 +63,13 @@ def get_customization_info(thread): def graphql_to_attachment(a): _type = a["__typename"] if _type in ["MessageImage", "MessageAnimatedImage"]: - return ImageAttachment( - original_extension=a.get("original_extension") - or (a["filename"].split("-")[0] if a.get("filename") else None), - width=a.get("original_dimensions", {}).get("width"), - height=a.get("original_dimensions", {}).get("height"), - is_animated=_type == "MessageAnimatedImage", - thumbnail_url=a.get("thumbnail", {}).get("uri"), - preview=a.get("preview") or a.get("preview_image"), - large_preview=a.get("large_preview"), - animated_preview=a.get("animated_image"), - uid=a.get("legacy_attachment_id"), - ) + return ImageAttachment._from_graphql(a) elif _type == "MessageVideo": - return VideoAttachment( - width=a.get("original_dimensions", {}).get("width"), - height=a.get("original_dimensions", {}).get("height"), - duration=a.get("playable_duration_in_ms"), - preview_url=a.get("playable_url"), - small_image=a.get("chat_image"), - medium_image=a.get("inbox_image"), - large_image=a.get("large_image"), - uid=a.get("legacy_attachment_id"), - ) + return VideoAttachment._from_graphql(a) elif _type == "MessageAudio": - return AudioAttachment( - filename=a.get("filename"), - url=a.get("playable_url"), - duration=a.get("playable_duration_in_ms"), - audio_type=a.get("audio_type"), - ) + return AudioAttachment._from_graphql(a) elif _type == "MessageFile": - return FileAttachment( - url=a.get("url"), - name=a.get("filename"), - is_malicious=a.get("is_malicious"), - uid=a.get("message_file_fbid"), - ) + return FileAttachment._from_graphql(a) else: return Attachment(uid=a.get("legacy_attachment_id"))