From 491d120c25093d4f7e0cfe898f42cf55f07e82d8 Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Sun, 3 Feb 2019 14:45:10 +0100 Subject: [PATCH 1/2] Fix #388 issue --- fbchat/graphql.py | 54 +++++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/fbchat/graphql.py b/fbchat/graphql.py index 8a22c4f4..3d2baf64 100644 --- a/fbchat/graphql.py +++ b/fbchat/graphql.py @@ -150,10 +150,11 @@ def graphql_to_extensible_attachment(a): latitude=float(latitude), longitude=float(longitude), ) - if story["media"]: - rtn.image_url = story["media"]["image"]["uri"] - rtn.image_width = story["media"]["image"]["width"] - rtn.image_height = story["media"]["image"]["height"] + if story.get("media"): + if story["media"].get("image"): + rtn.image_url = story["media"]["image"]["uri"] + rtn.image_width = story["media"]["image"]["width"] + rtn.image_height = story["media"]["image"]["height"] rtn.url = story["url"] return rtn elif _type == "MessageLiveLocation": @@ -166,19 +167,18 @@ def graphql_to_extensible_attachment(a): if story["target"].get("coordinate") else None, name=story["title_with_entities"]["text"], - expiration_time=story["target"]["expiration_time"] - if story["target"].get("expiration_time") - else None, - is_expired=story["target"]["is_expired"], + expiration_time=story["target"].get("expiration_time"), + is_expired=story["target"].get("is_expired"), ) - if story["media"]: - rtn.image_url = story["media"]["image"]["uri"] - rtn.image_width = story["media"]["image"]["width"] - rtn.image_height = story["media"]["image"]["height"] + if story.get("media"): + if story["media"].get("image"): + rtn.image_url = story["media"]["image"]["uri"] + rtn.image_width = story["media"]["image"]["width"] + rtn.image_height = story["media"]["image"]["height"] rtn.url = story["url"] return rtn elif _type in ["ExternalUrl", "Story"]: - return ShareAttachment( + rtn = ShareAttachment( uid=a.get("legacy_attachment_id"), author=story["target"]["actors"][0]["id"] if story["target"].get("actors") @@ -191,28 +191,22 @@ def graphql_to_extensible_attachment(a): description=story["description"].get("text") if story.get("description") else None, - source=story["source"]["text"], - image_url=story["media"]["image"]["uri"] - if story.get("media") - else None, - original_image_url=( - get_url_parameter(story["media"]["image"]["uri"], "url") - if "/safe_image.php" in story["media"]["image"]["uri"] - else story["media"]["image"]["uri"] - ) - if story.get("media") - else None, - image_width=story["media"]["image"]["width"] - if story.get("media") - else None, - image_height=story["media"]["image"]["height"] - if story.get("media") - else None, + source=story["source"].get("text"), attachments=[ graphql_to_subattachment(attachment) for attachment in story.get("subattachments") ], ) + if story.get("media"): + if story["media"].get("image"): + rtn.image_url = story["media"]["image"]["uri"] + rtn.original_image_url = ( + get_url_parameter(story["media"]["image"]["uri"], "url") + if "/safe_image.php" in story["media"]["image"]["uri"] + else story["media"]["image"]["uri"] + ) + rtn.image_width = story["media"]["image"]["width"] + rtn.image_height = story["media"]["image"]["height"] else: return UnsentMessage(uid=a.get("legacy_attachment_id")) From 56e43aec0e5a4e453f986fb6aab1820f09e48be4 Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Sun, 3 Feb 2019 19:03:43 +0100 Subject: [PATCH 2/2] Apply suggestions and fixes from review --- fbchat/graphql.py | 67 ++++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/fbchat/graphql.py b/fbchat/graphql.py index 3d2baf64..7aea2839 100644 --- a/fbchat/graphql.py +++ b/fbchat/graphql.py @@ -142,20 +142,22 @@ def graphql_to_extensible_attachment(a): if target: _type = target["__typename"] if _type == "MessageLocation": + url = story.get("url") latitude, longitude = get_url_parameter( - get_url_parameter(story["url"], "u"), "where1" + get_url_parameter(url, "u"), "where1" ).split(", ") rtn = LocationAttachment( uid=int(story["deduplication_key"]), latitude=float(latitude), longitude=float(longitude), ) - if story.get("media"): - if story["media"].get("image"): - rtn.image_url = story["media"]["image"]["uri"] - rtn.image_width = story["media"]["image"]["width"] - rtn.image_height = story["media"]["image"]["height"] - rtn.url = story["url"] + 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( @@ -170,23 +172,25 @@ def graphql_to_extensible_attachment(a): expiration_time=story["target"].get("expiration_time"), is_expired=story["target"].get("is_expired"), ) - if story.get("media"): - if story["media"].get("image"): - rtn.image_url = story["media"]["image"]["uri"] - rtn.image_width = story["media"]["image"]["width"] - rtn.image_height = story["media"]["image"]["height"] - rtn.url = story["url"] + 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=story["url"], - original_url=get_url_parameter(story["url"], "u") - if "/l.php?u=" in story["url"] - else story["url"], + 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") @@ -197,16 +201,18 @@ def graphql_to_extensible_attachment(a): for attachment in story.get("subattachments") ], ) - if story.get("media"): - if story["media"].get("image"): - rtn.image_url = story["media"]["image"]["uri"] - rtn.original_image_url = ( - get_url_parameter(story["media"]["image"]["uri"], "url") - if "/safe_image.php" in story["media"]["image"]["uri"] - else story["media"]["image"]["uri"] - ) - rtn.image_width = story["media"]["image"]["width"] - rtn.image_height = story["media"]["image"]["height"] + 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")) @@ -214,10 +220,11 @@ def graphql_to_extensible_attachment(a): def graphql_to_subattachment(a): _type = a["target"]["__typename"] if _type == "Video": + media = a["media"] return VideoAttachment( - duration=a["media"].get("playable_duration_in_ms"), - preview_url=a["media"].get("playable_url"), - medium_image=a["media"].get("image"), + duration=media.get("playable_duration_in_ms"), + preview_url=media.get("playable_url"), + medium_image=media.get("image"), uid=a["target"].get("video_id"), )