diff --git a/fbchat/_attachment.py b/fbchat/_attachment.py index 62f5ea0..265301e 100644 --- a/fbchat/_attachment.py +++ b/fbchat/_attachment.py @@ -50,7 +50,7 @@ class ShareAttachment(Attachment): @classmethod def _from_graphql(cls, data): - from . import _graphql + from . import _file url = data.get("url") rtn = cls( @@ -68,7 +68,7 @@ class ShareAttachment(Attachment): else None, source=data["source"].get("text"), attachments=[ - _graphql.graphql_to_subattachment(attachment) + _file.graphql_to_subattachment(attachment) for attachment in data.get("subattachments") ], ) diff --git a/fbchat/_client.py b/fbchat/_client.py index a8ca034..168392e 100644 --- a/fbchat/_client.py +++ b/fbchat/_client.py @@ -8,6 +8,7 @@ from random import choice from bs4 import BeautifulSoup as bs from mimetypes import guess_type from collections import OrderedDict +from . import _file from ._util import * from .models import * from .graphql import * @@ -3057,7 +3058,7 @@ class Client(object): if mercury.get("blob_attachment"): image_metadata = a.get("imageMetadata", {}) attach_type = mercury["blob_attachment"]["__typename"] - attachment = graphql_to_attachment( + attachment = _file.graphql_to_attachment( mercury["blob_attachment"] ) diff --git a/fbchat/_file.py b/fbchat/_file.py index c9049aa..00c8a32 100644 --- a/fbchat/_file.py +++ b/fbchat/_file.py @@ -251,3 +251,25 @@ class VideoAttachment(Attachment): medium_image=media.get("image"), uid=data["target"].get("video_id"), ) + + +def graphql_to_attachment(data): + _type = data["__typename"] + if _type in ["MessageImage", "MessageAnimatedImage"]: + return ImageAttachment._from_graphql(data) + elif _type == "MessageVideo": + return VideoAttachment._from_graphql(data) + elif _type == "MessageAudio": + return AudioAttachment._from_graphql(data) + elif _type == "MessageFile": + return FileAttachment._from_graphql(data) + + return Attachment(uid=data.get("legacy_attachment_id")) + + +def graphql_to_subattachment(data): + _type = data["target"]["__typename"] + if _type == "Video": + return VideoAttachment._from_subattachment(data) + + return None diff --git a/fbchat/_graphql.py b/fbchat/_graphql.py index a23865a..1004f68 100644 --- a/fbchat/_graphql.py +++ b/fbchat/_graphql.py @@ -3,6 +3,7 @@ from __future__ import unicode_literals import json import re +from . import _file from .models import * from ._util import * @@ -27,20 +28,6 @@ class ConcatJSONDecoder(json.JSONDecoder): # End shameless copy -def graphql_to_attachment(a): - _type = a["__typename"] - if _type in ["MessageImage", "MessageAnimatedImage"]: - return ImageAttachment._from_graphql(a) - elif _type == "MessageVideo": - return VideoAttachment._from_graphql(a) - elif _type == "MessageAudio": - return AudioAttachment._from_graphql(a) - elif _type == "MessageFile": - return FileAttachment._from_graphql(a) - else: - return Attachment(uid=a.get("legacy_attachment_id")) - - def graphql_to_extensible_attachment(a): story = a.get("story_attachment") if not story: @@ -61,14 +48,6 @@ def graphql_to_extensible_attachment(a): return None -def graphql_to_subattachment(data): - _type = data["target"]["__typename"] - if _type == "Video": - return VideoAttachment._from_subattachment(data) - - return None - - def graphql_to_quick_reply(q, is_response=False): data = dict() _type = q.get("content_type").lower() @@ -122,7 +101,7 @@ def graphql_to_message(message): } if message.get("blob_attachments") is not None: rtn.attachments = [ - graphql_to_attachment(attachment) + _file.graphql_to_attachment(attachment) for attachment in message["blob_attachments"] ] if message.get("platform_xmd_encoded"): diff --git a/fbchat/graphql.py b/fbchat/graphql.py index e35f623..06e416e 100644 --- a/fbchat/graphql.py +++ b/fbchat/graphql.py @@ -8,9 +8,7 @@ from ._graphql import ( FLAGS, WHITESPACE, ConcatJSONDecoder, - graphql_to_attachment, graphql_to_extensible_attachment, - graphql_to_subattachment, graphql_to_quick_reply, graphql_to_message, graphql_to_thread,