Move attachment and subattachment dispatching to _file.py

This commit is contained in:
Mads Marquart
2019-03-10 19:26:01 +01:00
parent 0b99238676
commit 53856a3622
5 changed files with 28 additions and 28 deletions

View File

@@ -50,7 +50,7 @@ class ShareAttachment(Attachment):
@classmethod @classmethod
def _from_graphql(cls, data): def _from_graphql(cls, data):
from . import _graphql from . import _file
url = data.get("url") url = data.get("url")
rtn = cls( rtn = cls(
@@ -68,7 +68,7 @@ class ShareAttachment(Attachment):
else None, else None,
source=data["source"].get("text"), source=data["source"].get("text"),
attachments=[ attachments=[
_graphql.graphql_to_subattachment(attachment) _file.graphql_to_subattachment(attachment)
for attachment in data.get("subattachments") for attachment in data.get("subattachments")
], ],
) )

View File

@@ -8,6 +8,7 @@ from random import choice
from bs4 import BeautifulSoup as bs from bs4 import BeautifulSoup as bs
from mimetypes import guess_type from mimetypes import guess_type
from collections import OrderedDict from collections import OrderedDict
from . import _file
from ._util import * from ._util import *
from .models import * from .models import *
from .graphql import * from .graphql import *
@@ -3057,7 +3058,7 @@ class Client(object):
if mercury.get("blob_attachment"): if mercury.get("blob_attachment"):
image_metadata = a.get("imageMetadata", {}) image_metadata = a.get("imageMetadata", {})
attach_type = mercury["blob_attachment"]["__typename"] attach_type = mercury["blob_attachment"]["__typename"]
attachment = graphql_to_attachment( attachment = _file.graphql_to_attachment(
mercury["blob_attachment"] mercury["blob_attachment"]
) )

View File

@@ -251,3 +251,25 @@ class VideoAttachment(Attachment):
medium_image=media.get("image"), medium_image=media.get("image"),
uid=data["target"].get("video_id"), 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

View File

@@ -3,6 +3,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import json import json
import re import re
from . import _file
from .models import * from .models import *
from ._util import * from ._util import *
@@ -27,20 +28,6 @@ class ConcatJSONDecoder(json.JSONDecoder):
# End shameless copy # 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): def graphql_to_extensible_attachment(a):
story = a.get("story_attachment") story = a.get("story_attachment")
if not story: if not story:
@@ -61,14 +48,6 @@ def graphql_to_extensible_attachment(a):
return None 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): def graphql_to_quick_reply(q, is_response=False):
data = dict() data = dict()
_type = q.get("content_type").lower() _type = q.get("content_type").lower()
@@ -122,7 +101,7 @@ def graphql_to_message(message):
} }
if message.get("blob_attachments") is not None: if message.get("blob_attachments") is not None:
rtn.attachments = [ rtn.attachments = [
graphql_to_attachment(attachment) _file.graphql_to_attachment(attachment)
for attachment in message["blob_attachments"] for attachment in message["blob_attachments"]
] ]
if message.get("platform_xmd_encoded"): if message.get("platform_xmd_encoded"):

View File

@@ -8,9 +8,7 @@ from ._graphql import (
FLAGS, FLAGS,
WHITESPACE, WHITESPACE,
ConcatJSONDecoder, ConcatJSONDecoder,
graphql_to_attachment,
graphql_to_extensible_attachment, graphql_to_extensible_attachment,
graphql_to_subattachment,
graphql_to_quick_reply, graphql_to_quick_reply,
graphql_to_message, graphql_to_message,
graphql_to_thread, graphql_to_thread,