From 9b4e753a79a15d717902242482e09b0d411f0f2a Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Wed, 12 Sep 2018 17:48:35 +0200 Subject: [PATCH] Added graphql methods for extensible attachments --- fbchat/graphql.py | 69 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 66 insertions(+), 3 deletions(-) diff --git a/fbchat/graphql.py b/fbchat/graphql.py index f023a16..72cfb13 100644 --- a/fbchat/graphql.py +++ b/fbchat/graphql.py @@ -128,9 +128,72 @@ def graphql_to_attachment(a): uid=a.get('legacy_attachment_id') ) +def graphql_to_extensible_attachment(a): + story = a.get('story_attachment') + if story: + _type = story['target']['__typename'] + if _type == 'MessageLocation': + latitude, longitude = get_url_parameter(get_url_parameter(story['url'], 'u'), 'where1').split(", ") + return LocationAttachment( + uid=int(story['deduplication_key']), + latitude=float(latitude), + longitude=float(longitude), + image=story['media']['image']['uri'], + url=story['url'], + ) + elif _type == 'MessageLiveLocation': + return 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, + image=story['media']['image']['uri'] if story.get('media') else None, + url=story['url'], + 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'], + ) + elif _type in ['ExternalUrl', 'Story']: + return 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'], + title=story['title_with_entities']['text'], + description=story['description']['text'], + 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, + attachments=[graphql_to_subattachment(attachment) for attachment in story.get('subattachments')] + ) + + +def graphql_to_subattachment(a): + _type = a['target']['__typename'] + if _type == 'Video': + return VideoAttachment( + duration=a['media'].get('playable_duration_in_ms'), + preview_url=a['media'].get('playable_url'), + small_image=a['media'].get('image'), + medium_image=a['media'].get('image'), + large_image=a['media'].get('image'), + uid=a['target'].get('video_id'), + ) + +def graphql_to_live_location(a): + return LiveLocationAttachment( + uid=a['id'], + latitude=a['coordinate']['latitude'] / (10 ** 8) if not a.get('stopReason') else None, + longitude=a['coordinate']['longitude'] / (10 ** 8) if not a.get('stopReason') else None, + name=a.get('locationTitle'), + expiration_time=a['expirationTime'], + is_expired=bool(a.get('stopReason')), + ) + def graphql_to_poll(a): rtn = Poll( - title=a.get('title') if a.get('title') else a.get("text"), + title=a.get('title') if a.get('title') else a.get('text'), options=[graphql_to_poll_option(m) for m in a.get('options')] ) rtn.uid = int(a["id"]) @@ -212,8 +275,8 @@ def graphql_to_message(message): rtn.reactions = {str(r['user']['id']):MessageReaction(r['reaction']) for r in message.get('message_reactions')} if message.get('blob_attachments') is not None: rtn.attachments = [graphql_to_attachment(attachment) for attachment in message['blob_attachments']] - # TODO: This is still missing parsing: - # message.get('extensible_attachment') + if message.get('extensible_attachment') is not None: + rtn.attachments.append(graphql_to_extensible_attachment(message['extensible_attachment'])) return rtn def graphql_to_user(user):