From 3440039610a9fa7c42d5a81a60231c4baa23e984 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Thu, 7 Mar 2019 19:07:00 +0100 Subject: [PATCH] Move graphql_to_sticker -> Sticker._from_graphql --- fbchat/_client.py | 4 +++- fbchat/_graphql.py | 23 +---------------------- fbchat/_sticker.py | 21 +++++++++++++++++++++ fbchat/graphql.py | 1 - 4 files changed, 25 insertions(+), 24 deletions(-) diff --git a/fbchat/_client.py b/fbchat/_client.py index f5f30be..7c514c9 100644 --- a/fbchat/_client.py +++ b/fbchat/_client.py @@ -3077,7 +3077,9 @@ class Client(object): attachments.append(attachment) elif mercury.get("sticker_attachment"): - sticker = graphql_to_sticker(mercury["sticker_attachment"]) + sticker = Sticker._from_graphql( + mercury["sticker_attachment"] + ) elif mercury.get("extensible_attachment"): attachment = graphql_to_extensible_attachment( diff --git a/fbchat/_graphql.py b/fbchat/_graphql.py index 160993c..9692d7a 100644 --- a/fbchat/_graphql.py +++ b/fbchat/_graphql.py @@ -60,27 +60,6 @@ def get_customization_info(thread): return rtn -def graphql_to_sticker(s): - if not s: - return None - sticker = Sticker(uid=s["id"]) - if s.get("pack"): - sticker.pack = s["pack"].get("id") - if s.get("sprite_image"): - sticker.is_animated = True - sticker.medium_sprite_image = s["sprite_image"].get("uri") - sticker.large_sprite_image = s["sprite_image_2x"].get("uri") - sticker.frames_per_row = s.get("frames_per_row") - sticker.frames_per_col = s.get("frames_per_column") - sticker.frame_rate = s.get("frame_rate") - sticker.url = s.get("url") - sticker.width = s.get("width") - sticker.height = s.get("height") - if s.get("label"): - sticker.label = s["label"] - return sticker - - def graphql_to_attachment(a): _type = a["__typename"] if _type in ["MessageImage", "MessageAnimatedImage"]: @@ -360,7 +339,7 @@ def graphql_to_message(message): for m in message.get("message").get("ranges", []) ], emoji_size=get_emojisize_from_tags(message.get("tags_list")), - sticker=graphql_to_sticker(message.get("sticker")), + sticker=Sticker._from_graphql(message.get("sticker")), ) rtn.uid = str(message.get("message_id")) rtn.author = str(message.get("message_sender").get("id")) diff --git a/fbchat/_sticker.py b/fbchat/_sticker.py index e90655d..db8b35f 100644 --- a/fbchat/_sticker.py +++ b/fbchat/_sticker.py @@ -37,3 +37,24 @@ class Sticker(Attachment): def __init__(self, uid=None): super(Sticker, self).__init__(uid=uid) + + @classmethod + def _from_graphql(cls, data): + if not data: + return None + self = cls(uid=data["id"]) + if data.get("pack"): + self.pack = data["pack"].get("id") + if data.get("sprite_image"): + self.is_animated = True + self.medium_sprite_image = data["sprite_image"].get("uri") + self.large_sprite_image = data["sprite_image_2x"].get("uri") + self.frames_per_row = data.get("frames_per_row") + self.frames_per_col = data.get("frames_per_column") + self.frame_rate = data.get("frame_rate") + self.url = data.get("url") + self.width = data.get("width") + self.height = data.get("height") + if data.get("label"): + self.label = data["label"] + return self diff --git a/fbchat/graphql.py b/fbchat/graphql.py index 00f46e4..1f5dc85 100644 --- a/fbchat/graphql.py +++ b/fbchat/graphql.py @@ -9,7 +9,6 @@ from ._graphql import ( WHITESPACE, ConcatJSONDecoder, get_customization_info, - graphql_to_sticker, graphql_to_attachment, graphql_to_extensible_attachment, graphql_to_subattachment,