Skip to content

Commit

Permalink
Merge pull request #1467 from Yessirskiy/master
Browse files Browse the repository at this point in the history
 General or Primary box for direct_threads()
  • Loading branch information
adw0rd committed Jul 20, 2023
2 parents 217d88a + 64c6454 commit 38af401
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion instagrapi/mixins/direct.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@
from instagrapi.utils import dumps

SELECTED_FILTERS = ("flagged", "unread")
BOXES = ("general", "primary")

try:
from typing import Literal

SELECTED_FILTER = Literal[SELECTED_FILTERS]
BOX = Literal[BOXES]
except ImportError:
# python <= 3.8
SELECTED_FILTER = str
BOX = str


class DirectMixin:
Expand All @@ -41,6 +44,7 @@ def direct_threads(
self,
amount: int = 20,
selected_filter: SELECTED_FILTER = "",
box: BOX = "",
thread_message_limit: Optional[int] = None,
) -> List[DirectThread]:
"""
Expand All @@ -52,6 +56,8 @@ def direct_threads(
Maximum number of media to return, default is 20
selected_filter: str, optional
Filter to apply to threads (flagged or unread)
box: str, optional
Box to gather threads from (primary or general) (business accounts only)
thread_message_limit: int, optional
Thread message limit, deafult is 10
Expand All @@ -65,7 +71,7 @@ def direct_threads(
threads = []
# self.private_request("direct_v2/get_presence/")
while True:
threads_chunk, cursor = self.direct_threads_chunk(selected_filter, thread_message_limit, cursor)
threads_chunk, cursor = self.direct_threads_chunk(selected_filter, box, thread_message_limit, cursor)
for thread in threads_chunk:
threads.append(thread)

Expand All @@ -78,6 +84,7 @@ def direct_threads(
def direct_threads_chunk(
self,
selected_filter: SELECTED_FILTER = "",
box: BOX = "",
thread_message_limit: Optional[int] = None,
cursor: str = None
) -> Tuple[List[DirectThread], str]:
Expand All @@ -90,6 +97,8 @@ def direct_threads_chunk(
Filter to apply to threads (flagged or unread)
thread_message_limit: int, optional
Thread message limit, deafult is 10
box: str, optional
Box to gather threads from (primary or general) (business accounts only)
cursor: str, optional
Cursor from the previous chunk request
Expand All @@ -116,6 +125,15 @@ def direct_threads_chunk(
"selected_filter": selected_filter
}
)
if box:
assert (
box in BOXES
), f'Unsupported box="{box}" {BOXES}'
params.update(
{
"folder" : '1' if box == "general" else '0'
}
)
if thread_message_limit:
params.update({"thread_message_limit": thread_message_limit})
if cursor:
Expand Down

0 comments on commit 38af401

Please sign in to comment.