def direct_thread_by_participants(self, user_ids: List[int]) -> DirectThread: """ Get direct thread by participants Parameters ---------- user_ids: List[int] List of unique identifier of Users id Returns ------- DirectThread An object of DirectThread """ recipient_users = dumps([int(uid) for uid in user_ids]) result = self.private_request("direct_v2/threads/get_by_participants/", params={ "recipient_users": recipient_users, "seq_id": 2580572, "limit": 20 }) if 'thread' not in result: raise DirectThreadNotFound( f'Thread not found by recipient_users={recipient_users}', user_ids=user_ids, **self.last_json) return extract_direct_thread(result['thread'])
def direct_thread(self, thread_id: int, amount: int = 20) -> DirectThread: """ Get all the information about a Direct Message thread Parameters ---------- thread_id: int Unique identifier of a Direct Message thread amount: int, optional Maximum number of media to return, default is 20 Returns ------- DirectThread An object of DirectThread """ assert self.user_id, "Login required" params = { "visual_message_return_type": "unseen", "direction": "older", "seq_id": "40065", # 59663 "limit": "20", } cursor = None items = [] while True: if cursor: params["cursor"] = cursor try: result = self.private_request( f"direct_v2/threads/{thread_id}/", params=params) except ClientNotFoundError as e: raise DirectThreadNotFound(e, thread_id=thread_id, **self.last_json) thread = result["thread"] for item in thread["items"]: items.append(item) cursor = thread.get("oldest_cursor") if not cursor or (amount and len(items) >= amount): break if amount: items = items[:amount] thread["items"] = items return extract_direct_thread(thread)
def direct_media(self, thread_id: int, amount: int = 20) -> List[Media]: """ Get all the media from a thread Parameters ---------- thread_id: int Unique identifier of a Direct Message thread amount: int, optional Maximum number of media to return, default is 20 Returns ------- List[Media] A list of objects of Media """ assert self.user_id, "Login required" params = {"limit": 20, "media_type": "photos_and_videos"} max_timestamp = None items = [] while True: if max_timestamp: params["max_timestamp"] = max_timestamp try: result = self.private_request( f"direct_v2/threads/{thread_id}/media/", params=params) except ClientNotFoundError as e: raise DirectThreadNotFound(e, thread_id=thread_id, **self.last_json) for item in result["items"]: media = item.get("media") items.append(extract_direct_media(media)) max_timestamp = item.get("timestamp") more_available = result.get("more_available") if not more_available or (amount and len(items) >= amount): break if amount: items = items[:amount] return items