Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notes update #1380

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions docs/usage-guide/notes.md
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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)**
19 changes: 18 additions & 1 deletion instagrapi/extractors.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
Media,
MediaOembed,
NoteRequest,
NoteResponse,
ReplyMessage,
Resource,
Story,
Expand Down Expand Up @@ -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)
39 changes: 35 additions & 4 deletions instagrapi/mixins/note.py
Original file line number Diff line number Diff line change
@@ -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
"""
Expand Down
4 changes: 2 additions & 2 deletions instagrapi/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ class Track(BaseModel):


class NoteResponse(BaseModel):
id: str
uuid: str
text: str
user_id: int
user: UserShort
Expand All @@ -463,7 +463,7 @@ class NoteResponse(BaseModel):
is_emoji_only: bool
has_translation: bool
note_style: int
status: str
#status: str


class NoteRequest(BaseModel):
Expand Down