From 919c84fb5a4dec176a666c78f739d3bab4109421 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Fri, 24 Mar 2023 22:24:26 +0000 Subject: [PATCH] my.instagram: better unification of like messages/reactions --- my/instagram/android.py | 9 ++++++--- my/instagram/gdpr.py | 11 ++++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/my/instagram/android.py b/my/instagram/android.py index 709cfe0..8e62363 100644 --- a/my/instagram/android.py +++ b/my/instagram/android.py @@ -102,7 +102,7 @@ def _parse_message(j: Json) -> Optional[_Message]: tid = j['thread_key']['thread_id'] uid = j['user_id'] created = datetime.fromtimestamp(int(j['timestamp']) / 1_000_000) - text: str + text: Optional[str] = None if t == 'text': text = j['text'] elif t == 'reel_share': @@ -110,11 +110,14 @@ def _parse_message(j: Json) -> Optional[_Message]: # the problem is that the links are deliberately expired by instagram.. text = j['reel_share']['text'] elif t == 'action_log': - # something like "X liked message" -- hardly useful? - return None + # for likes this ends up as 'Liked a message' or reactions + # which isn't super useful by itself perhaps, but matches GDPR so lets us unify threads better + text = j['action_log']['description'] else: raise MessageError(id, f"{t} isn't handled yet") + assert text is not None, j + return _Message( id=id, created=created, diff --git a/my/instagram/gdpr.py b/my/instagram/gdpr.py index 62c9f1f..b9f8780 100644 --- a/my/instagram/gdpr.py +++ b/my/instagram/gdpr.py @@ -140,6 +140,9 @@ def _entities() -> Iterator[Res[Union[User, _Message]]]: content = None if 'content' in jm: content = _decode(jm['content']) + if content.endswith(' to your message '): + # ugh. for some reason these contain an extra space and that messes up message merging.. + content = content.strip() else: share = jm.get('share') photos = jm.get('photos') @@ -149,9 +152,11 @@ def _entities() -> Iterator[Res[Union[User, _Message]]]: content = str(cc) if content is None: - # not sure what it means.. perhaps likes or something? - logger.warning(f'content is None: {jm}') - continue + # this happens e.g. on reel shares.. + # not sure what we can do properly, GPDR has literally no other info in this case + # on android in this case at the moment we have as content '' + # so for consistency let's do that too + content = '' timestamp_ms = jm['timestamp_ms'] sender_name = _decode(jm['sender_name'])