From 861f17bc4dac009631580b6f884184426995903e Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Sun, 9 Dec 2018 14:55:10 +0100 Subject: [PATCH] Added DeletedMessage attachment --- fbchat/graphql.py | 86 +++++++++++++++++++++++++---------------------- fbchat/models.py | 6 ++++ 2 files changed, 52 insertions(+), 40 deletions(-) diff --git a/fbchat/graphql.py b/fbchat/graphql.py index ac5b859..b6c1ae2 100644 --- a/fbchat/graphql.py +++ b/fbchat/graphql.py @@ -131,47 +131,53 @@ def graphql_to_attachment(a): 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(", ") - rtn = LocationAttachment( - uid=int(story['deduplication_key']), - latitude=float(latitude), - longitude=float(longitude), - ) - 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': - 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']['expiration_time'] if story['target'].get('expiration_time') else None, - is_expired=story['target']['is_expired'], - ) - 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( + target = story.get('target') + if target: + _type = target['__typename'] + if _type == 'MessageLocation': + latitude, longitude = get_url_parameter(get_url_parameter(story['url'], 'u'), 'where1').split(", ") + rtn = LocationAttachment( + uid=int(story['deduplication_key']), + latitude=float(latitude), + longitude=float(longitude), + ) + 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': + 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']['expiration_time'] if story['target'].get('expiration_time') else None, + is_expired=story['target']['is_expired'], + ) + 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( + 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'].get('text'), + description=story['description'].get('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')], + ) + else: + return DeletedMessage( 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'].get('text'), - description=story['description'].get('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')] ) diff --git a/fbchat/models.py b/fbchat/models.py index cc06564..f038b59 100644 --- a/fbchat/models.py +++ b/fbchat/models.py @@ -216,6 +216,12 @@ class Attachment(object): """Represents a Facebook attachment""" self.uid = uid +class DeletedMessage(Attachment): + + def __init__(self, *args, **kwargs): + """Represents a deleted message""" + super(DeletedMessage, self).__init__(*args, **kwargs) + class Sticker(Attachment): #: The sticker-pack's ID pack = None