From 2ce99a2c44df04dc0c5e35a7943cb2975996ad48 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Thu, 7 Mar 2019 20:50:14 +0100 Subject: [PATCH] Split graphql_to_extensible_attachment into smaller methods --- fbchat/_attachment.py | 38 +++++++++++++++++ fbchat/_graphql.py | 98 +++++++------------------------------------ fbchat/_location.py | 49 ++++++++++++++++++++++ 3 files changed, 103 insertions(+), 82 deletions(-) diff --git a/fbchat/_attachment.py b/fbchat/_attachment.py index 4ee40b2..62f5ea0 100644 --- a/fbchat/_attachment.py +++ b/fbchat/_attachment.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals import attr +from . import _util @attr.s(cmp=False) @@ -46,3 +47,40 @@ class ShareAttachment(Attachment): # Put here for backwards compatibility, so that the init argument order is preserved uid = attr.ib(None) + + @classmethod + def _from_graphql(cls, data): + from . import _graphql + + url = data.get("url") + rtn = cls( + uid=data.get("deduplication_key"), + author=data["target"]["actors"][0]["id"] + if data["target"].get("actors") + else None, + url=url, + original_url=_util.get_url_parameter(url, "u") + if "/l.php?u=" in url + else url, + title=data["title_with_entities"].get("text"), + description=data["description"].get("text") + if data.get("description") + else None, + source=data["source"].get("text"), + attachments=[ + _graphql.graphql_to_subattachment(attachment) + for attachment in data.get("subattachments") + ], + ) + media = data.get("media") + if media and media.get("image"): + image = media["image"] + rtn.image_url = image.get("uri") + rtn.original_image_url = ( + _util.get_url_parameter(rtn.image_url, "url") + if "/safe_image.php" in rtn.image_url + else rtn.image_url + ) + rtn.image_width = image.get("width") + rtn.image_height = image.get("height") + return rtn diff --git a/fbchat/_graphql.py b/fbchat/_graphql.py index ceffba0..df3c1a6 100644 --- a/fbchat/_graphql.py +++ b/fbchat/_graphql.py @@ -106,88 +106,22 @@ def graphql_to_attachment(a): def graphql_to_extensible_attachment(a): story = a.get("story_attachment") - if story: - target = story.get("target") - if target: - _type = target["__typename"] - if _type == "MessageLocation": - url = story.get("url") - address = get_url_parameter(get_url_parameter(url, "u"), "where1") - try: - latitude, longitude = [float(x) for x in address.split(", ")] - address = None - except ValueError: - latitude, longitude = None, None - rtn = LocationAttachment( - uid=int(story["deduplication_key"]), - latitude=latitude, - longitude=longitude, - address=address, - ) - media = story.get("media") - if media and media.get("image"): - image = media["image"] - rtn.image_url = image.get("uri") - rtn.image_width = image.get("width") - rtn.image_height = image.get("height") - rtn.url = url - return rtn - elif _type == "MessageLiveLocation": - rtn = LiveLocationAttachment( - uid=int(story["target"]["live_location_id"]), - latitude=story["target"]["coordinate"]["latitude"] - if story["target"].get("coordinate") - else None, - longitude=story["target"]["coordinate"]["longitude"] - if story["target"].get("coordinate") - else None, - name=story["title_with_entities"]["text"], - expiration_time=story["target"].get("expiration_time"), - is_expired=story["target"].get("is_expired"), - ) - media = story.get("media") - if media and media.get("image"): - image = media["image"] - rtn.image_url = image.get("uri") - rtn.image_width = image.get("width") - rtn.image_height = image.get("height") - rtn.url = story.get("url") - return rtn - elif _type in ["ExternalUrl", "Story"]: - url = story.get("url") - rtn = ShareAttachment( - uid=a.get("legacy_attachment_id"), - author=story["target"]["actors"][0]["id"] - if story["target"].get("actors") - else None, - url=url, - original_url=get_url_parameter(url, "u") - if "/l.php?u=" in url - else url, - title=story["title_with_entities"].get("text"), - description=story["description"].get("text") - if story.get("description") - else None, - source=story["source"].get("text"), - attachments=[ - graphql_to_subattachment(attachment) - for attachment in story.get("subattachments") - ], - ) - media = story.get("media") - if media and media.get("image"): - image = media["image"] - rtn.image_url = image.get("uri") - rtn.original_image_url = ( - get_url_parameter(rtn.image_url, "url") - if "/safe_image.php" in rtn.image_url - else rtn.image_url - ) - rtn.image_width = image.get("width") - rtn.image_height = image.get("height") - return rtn - else: - return UnsentMessage(uid=a.get("legacy_attachment_id")) + if not story: + return None + + target = story.get("target") + if not target: + return UnsentMessage(uid=a.get("legacy_attachment_id")) + + _type = target["__typename"] + if _type == "MessageLocation": + return LocationAttachment._from_graphql(story) + elif _type == "MessageLiveLocation": + return LiveLocationAttachment._from_graphql(story) + elif _type in ["ExternalUrl", "Story"]: + return ShareAttachment._from_graphql(story) + + return None def graphql_to_subattachment(a): diff --git a/fbchat/_location.py b/fbchat/_location.py index af9b56d..23c1351 100644 --- a/fbchat/_location.py +++ b/fbchat/_location.py @@ -3,6 +3,7 @@ from __future__ import unicode_literals import attr from ._attachment import Attachment +from . import _util @attr.s(cmp=False) @@ -30,6 +31,30 @@ class LocationAttachment(Attachment): # Put here for backwards compatibility, so that the init argument order is preserved uid = attr.ib(None) + @classmethod + def _from_graphql(cls, data): + url = data.get("url") + address = _util.get_url_parameter(_util.get_url_parameter(url, "u"), "where1") + try: + latitude, longitude = [float(x) for x in address.split(", ")] + address = None + except ValueError: + latitude, longitude = None, None + rtn = cls( + uid=int(data["deduplication_key"]), + latitude=latitude, + longitude=longitude, + address=address, + ) + media = data.get("media") + if media and media.get("image"): + image = media["image"] + rtn.image_url = image.get("uri") + rtn.image_width = image.get("width") + rtn.image_height = image.get("height") + rtn.url = url + return rtn + @attr.s(cmp=False, init=False) class LiveLocationAttachment(LocationAttachment): @@ -61,3 +86,27 @@ class LiveLocationAttachment(LocationAttachment): expiration_time=data["expirationTime"], is_expired=bool(data.get("stopReason")), ) + + @classmethod + def _from_graphql(cls, data): + target = data["target"] + rtn = cls( + uid=int(target["live_location_id"]), + latitude=target["coordinate"]["latitude"] + if target.get("coordinate") + else None, + longitude=target["coordinate"]["longitude"] + if target.get("coordinate") + else None, + name=data["title_with_entities"]["text"], + expiration_time=target.get("expiration_time"), + is_expired=target.get("is_expired"), + ) + media = data.get("media") + if media and media.get("image"): + image = media["image"] + rtn.image_url = image.get("uri") + rtn.image_width = image.get("width") + rtn.image_height = image.get("height") + rtn.url = data.get("url") + return rtn