Skip to content
This repository was archived by the owner on Dec 12, 2023. It is now read-only.

Commit 4130230

Browse files
committed
get_all_conversations: add helper
1 parent 4e9f69f commit 4130230

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

linkedin_messaging/api_objects.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,11 @@ class Picture:
8989
@dataclass_json(letter_case=LetterCase.CAMEL, undefined=Undefined.EXCLUDE)
9090
@dataclass
9191
class MiniProfile:
92-
first_name: str
93-
last_name: str
94-
occupation: str
9592
entity_urn: URN
9693
public_identifier: str
97-
tracking_id: str
98-
94+
first_name: Optional[str] = None
95+
last_name: Optional[str] = None
96+
occupation: Optional[str] = None
9997
memorialized: bool = False
10098
picture: Optional[Picture] = None
10199

@@ -197,10 +195,11 @@ class ThirdPartyMedia:
197195
@dataclass_json(letter_case=LetterCase.CAMEL)
198196
@dataclass
199197
class MessageCustomContent:
200-
third_party_media: ThirdPartyMedia = field(
198+
third_party_media: Optional[ThirdPartyMedia] = field(
201199
metadata=config(
202200
field_name="com.linkedin.voyager.messaging.shared.ThirdPartyMedia"
203-
)
201+
),
202+
default=None,
204203
)
205204

206205

linkedin_messaging/linkedin.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from datetime import datetime
77
from typing import (
88
Any,
9+
AsyncGenerator,
910
Awaitable,
1011
Callable,
1112
DefaultDict,
@@ -19,6 +20,7 @@
1920
from bs4 import BeautifulSoup
2021

2122
from .api_objects import (
23+
Conversation,
2224
ConversationResponse,
2325
ConversationsResponse,
2426
MessageAttachmentCreate,
@@ -219,6 +221,25 @@ async def get_conversations(
219221
conversations_resp = await self._get("/messaging/conversations", params=params)
220222
return ConversationsResponse.from_json(await conversations_resp.text())
221223

224+
async def get_all_conversations(self) -> AsyncGenerator[Conversation, None]:
225+
"""
226+
A generator of all of the user's conversations using paging.
227+
"""
228+
last_activity_before = datetime.now()
229+
while True:
230+
conversations_response = await self.get_conversations(
231+
last_activity_before=last_activity_before
232+
)
233+
for c in conversations_response.elements:
234+
yield c
235+
236+
# The page size is 20, by default, so if we get less than 20, we are at the
237+
# end of the list so we should stop.
238+
if len(conversations_response.elements) < 20:
239+
break
240+
241+
last_activity_before = conversations_response.elements[-1].last_activity_at
242+
222243
async def get_conversation(
223244
self,
224245
conversation_urn: URN,

0 commit comments

Comments
 (0)