From 39be12b7e20e841a4c0d6581bc6d4dbc4c47cc7e Mon Sep 17 00:00:00 2001 From: Nil MALHOMME Date: Thu, 8 Jun 2023 14:26:51 +0200 Subject: [PATCH 1/4] Fixed the naming of the get_my_notes it was unclear the method returns all the notes from all the user's friends Created two new methods get_note_by_user that requires the username it returns a NoteResponse object and get_note_content_by_user that requires the username and returns the NoteRequest object, that only contains the text and uuid field. Fixed the get_all_notes that was returning a json text now it will return an array of NoteResponse --- instagrapi/mixins/note.py | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/instagrapi/mixins/note.py b/instagrapi/mixins/note.py index c0128ab6..3d9e4dcb 100644 --- a/instagrapi/mixins/note.py +++ b/instagrapi/mixins/note.py @@ -1,20 +1,51 @@ import uuid from instagrapi.types import NoteRequest +from instagrapi.extractors import extract_note +import json class NoteMixin: - def get_my_notes(self): + def get_all_notes(self): """ - Get your personal notes + Get all notes from a user's friends and their note """ headers = self.base_headers headers["X-IG-Client-Endpoint"] = "DirectInboxFragment:direct_inbox" - return self.private_request("notes/get_notes/", headers=headers) + response = self.private_request("notes/get_notes/", headers=headers) + #print(response) + notes_data = response["items"] + notes = [] + for note_data in notes_data: + note = extract_note(note_data) + notes.append(note) + return notes + + def get_note_by_user(self, notes: [], username: str): + """ + Search notes by user and return the corresponding NoteResponse object. + If the user is not found, return None. + """ + for note_data in notes: + user_data = note_data.user + if user_data and user_data.username == username: + return note_data + return None + + def get_note_content_by_user(self, notes: [], username: str): + """ + Search notes by user and return the corresponding note text content. + If the user is not found, return None. + """ + for note_data in notes: + user_data = note_data.user + if user_data and user_data.username == username: + return note_data.text + return None def delete_note(self, note_id: int): """ - Delete one of your personal notes + Delete your personal notes It uses note_id to delete a note Use get_my_notes() to get note_id """ From 37e4d8c8fcf98f6aeef88d12443320cd2683caed Mon Sep 17 00:00:00 2001 From: Nil MALHOMME Date: Thu, 8 Jun 2023 14:28:45 +0200 Subject: [PATCH 2/4] Added an extract_note that returns a NoteResponse and extract_note_content method that returns a NoteRequest --- instagrapi/extractors.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/instagrapi/extractors.py b/instagrapi/extractors.py index 7213e86d..f6fec727 100644 --- a/instagrapi/extractors.py +++ b/instagrapi/extractors.py @@ -18,6 +18,7 @@ Media, MediaOembed, NoteRequest, + NoteResponse, ReplyMessage, Resource, Story, @@ -441,7 +442,23 @@ def extract_track(data): return Track(**data) -def extract_note(data): +def extract_note_content (data): data["text"] = data.get("text") or None data["uuid"] = data.get("uuid") or None return NoteRequest(**data) + + +def extract_note(data): + data["uuid"] = data.get("id") or None + data["text"] = data.get("text") or None + data["user_id"] = data.get("user_id") or None + data["user"] = data.get("user") or None + data["audience"] = data.get("audience") or 0 + data["created_at"] = data.get("created_at") or 0 + data["expires_at"] = data.get("expires_at") or 0 + data["is_emoji_only"] = data.get("is_emoji_only") or False + data["has_translation"] = data.get("has_translation") or False + data["note_style"] = data.get("note_style") or 0 + data["is_unseen"] = data.get("is_unseen") or False + data["can_reply"] = data.get("can_reply") or False + return NoteResponse(**data) From c800bdafd7e4d5bde007ac234d8388ec01bfcd40 Mon Sep 17 00:00:00 2001 From: Nil MALHOMME Date: Thu, 8 Jun 2023 14:29:36 +0200 Subject: [PATCH 3/4] Fixed some BaseModel declarations that were not named according to the response content --- instagrapi/types.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/instagrapi/types.py b/instagrapi/types.py index ef964922..12f1bb51 100644 --- a/instagrapi/types.py +++ b/instagrapi/types.py @@ -453,7 +453,7 @@ class Track(BaseModel): class NoteResponse(BaseModel): - id: str + uuid: str text: str user_id: int user: UserShort @@ -463,7 +463,7 @@ class NoteResponse(BaseModel): is_emoji_only: bool has_translation: bool note_style: int - status: str + #status: str class NoteRequest(BaseModel): From 7f6e95e02f95e8562bdec51efaebdc6825fd55c6 Mon Sep 17 00:00:00 2001 From: Nil MALHOMME Date: Thu, 8 Jun 2023 14:36:16 +0200 Subject: [PATCH 4/4] Updated wiki --- docs/usage-guide/notes.md | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/docs/usage-guide/notes.md b/docs/usage-guide/notes.md index d144d151..a2c22c95 100644 --- a/docs/usage-guide/notes.md +++ b/docs/usage-guide/notes.md @@ -1,15 +1,17 @@ -# Notes *WIP* +# Notes -| Method | Return | Description -| ----------------------------------------------------------------------- | --------------- | ---------------------------------- -| get_my_notes() | dict | get your current notes -| send_note(note_content: str, audience: int = 0) | None | Post a note -| delete_note(note_id: int, audience: int = 0) | dict (status ok) | Delete a note +| Method | Return | Description +|--------------------------------------------------------------|------------------| ---------------------------------- +| get_all_notes() | NoteResponse | get all the note that the user can see +| get_note_by_user(note: NoteResponse[], username: str) | NoteResponse | get a note by a specified username +| get_note_content_by_user(note: NoteResponse[], username: str | NoteRequest | get a note content by a specified username +| send_note(note_content: str, audience: int = 0) | None | Post a note +| delete_note(note_id: int, audience: int = 0) | dict (status ok) | Delete a note Example: ``` python ->>> cl.get_my_notes() +>>> cl.get_all_notes() >>> cl.delete_note(17887679456798301) @@ -27,6 +29,6 @@ The note should not exceed 60 characters. The rate in between Notes requests sho Common arguments: -* `note_id` - get it from the get_my_notes() +* `note_id` - get it from the note.uuid if the NoteResponse BaseModel is used * `note_content` - Content of the note * `audience` - Who can see the note **(0 = Everyone, 1 = Close Friends only)**