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

Commit 17fa1c7

Browse files
committed
examples: add a set of initial examples
1 parent d0cf21d commit 17fa1c7

File tree

6 files changed

+205
-0
lines changed

6 files changed

+205
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
# LinkedIn Messaging API
22

3+
[![Python](https://github.com/sumnerevans/linkedin-messaging-api/actions/workflows/python.yaml/badge.svg)](https://github.com/sumnerevans/linkedin-messaging-api/actions/workflows/python.yaml)
4+
[![Matrix Chat](https://img.shields.io/matrix/linkedin-matrix:nevarro.space?server_fqdn=matrix.nevarro.space)](https://matrix.to/#/#linkedin-matrix:nevarro.space?via=nevarro.space&via=sumnerevans.com)
5+
36
An unofficial API for interacting with LinkedIn Messaging.
47

58
Built using [aiohttp](https://docs.aiohttp.org/en/stable/).
69

10+
## Documentation
11+
12+
See [`examples` directory](./examples).
13+
714
## Credits
815

916
Authentication technique from [@everping](https://github.com/everping) in the

examples/get-conversation.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from pathlib import Path
2+
3+
import asyncio
4+
from linkedin_messaging import LinkedInMessaging, ChallengeException
5+
from linkedin_messaging.api_objects import URN
6+
7+
cookie_path = Path(__file__).parent.joinpath("cookies.pickle")
8+
urn = URN(
9+
"urn:li:fs_conversation:2-OTNkODIyYTEtODFjZS00NTdlLThlYTItYWQyMDg2NTc4YWMyXzAxMA=="
10+
)
11+
12+
13+
async def main():
14+
linkedin = LinkedInMessaging()
15+
if cookie_path.exists():
16+
with open(cookie_path, "rb") as cf:
17+
linkedin = LinkedInMessaging.from_pickle(cf.read())
18+
19+
if not await linkedin.logged_in():
20+
try:
21+
await linkedin.login("EMAIL", "PASSWORD")
22+
except ChallengeException:
23+
await linkedin.enter_2fa(input("2fa code: "))
24+
25+
with open(cookie_path, "wb+") as cf:
26+
cf.write(linkedin.to_pickle())
27+
28+
# Get a list of all of the conversations for the given user.
29+
conversations = await linkedin.get_conversations()
30+
for c in conversations.elements:
31+
print(c)
32+
33+
# Get a specific conversation by URN.
34+
convo_resp = await linkedin.get_conversation(urn)
35+
for element in convo_resp.elements:
36+
print(element)
37+
38+
await linkedin.close()
39+
40+
41+
loop = asyncio.get_event_loop()
42+
asyncio.ensure_future(main())
43+
loop.run_forever()
44+
loop.close()

examples/listen-for-events.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import logging
2+
from pathlib import Path
3+
4+
import asyncio
5+
from linkedin_messaging import LinkedInMessaging, ChallengeException
6+
7+
cookie_path = Path(__file__).parent.joinpath("cookies.pickle")
8+
9+
10+
async def main():
11+
logging.basicConfig()
12+
logging.getLogger().setLevel(logging.DEBUG)
13+
14+
linkedin = LinkedInMessaging()
15+
if cookie_path.exists():
16+
with open(cookie_path, "rb") as cf:
17+
linkedin = LinkedInMessaging.from_pickle(cf.read())
18+
19+
if not await linkedin.logged_in():
20+
try:
21+
await linkedin.login("EMAIL", "PASSWORD")
22+
except ChallengeException:
23+
await linkedin.enter_2fa(input("2fa code: "))
24+
25+
with open(cookie_path, "wb+") as cf:
26+
cf.write(linkedin.to_pickle())
27+
28+
async def on_event(event):
29+
print(event)
30+
31+
linkedin.add_event_listener("event", on_event)
32+
linkedin.add_event_listener("reactionSummary", on_event)
33+
linkedin.add_event_listener("reactionAdded", on_event)
34+
35+
task = asyncio.create_task(linkedin.start_listener())
36+
37+
# wait basically forever
38+
await asyncio.sleep(2 ** 128)
39+
40+
asyncio.gather(task)
41+
42+
await linkedin.close()
43+
44+
45+
loop = asyncio.get_event_loop()
46+
asyncio.ensure_future(main())
47+
loop.run_forever()
48+
loop.close()

examples/pickle.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from pathlib import Path
2+
3+
import asyncio
4+
from linkedin_messaging import LinkedInMessaging, ChallengeException
5+
6+
cookie_path = Path(__file__).parent.joinpath("cookies.pickle")
7+
8+
9+
async def main():
10+
linkedin = LinkedInMessaging()
11+
if cookie_path.exists():
12+
with open(cookie_path, "rb") as cf:
13+
linkedin = LinkedInMessaging.from_pickle(cf.read())
14+
15+
if not await linkedin.logged_in():
16+
try:
17+
await linkedin.login("EMAIL", "PASSWORD")
18+
except ChallengeException:
19+
await linkedin.enter_2fa(input("2fa code: "))
20+
21+
with open(cookie_path, "wb+") as cf:
22+
cf.write(linkedin.to_pickle())
23+
24+
print(await linkedin.get_user_profile())
25+
26+
await linkedin.close()
27+
28+
29+
loop = asyncio.get_event_loop()
30+
asyncio.ensure_future(main())
31+
loop.run_forever()
32+
loop.close()

examples/send-message.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import logging
2+
from pathlib import Path
3+
4+
import asyncio
5+
from linkedin_messaging import LinkedInMessaging, ChallengeException
6+
from linkedin_messaging.api_objects import URN, MessageCreate, AttributedBody
7+
8+
cookie_path = Path(__file__).parent.joinpath("cookies.pickle")
9+
urn = URN(
10+
"urn:li:fs_conversation:2-OTNkODIyYTEtODFjZS00NTdlLThlYTItYWQyMDg2NTc4YWMyXzAxMA=="
11+
)
12+
13+
14+
async def main():
15+
logging.basicConfig()
16+
logging.getLogger().setLevel(logging.DEBUG)
17+
18+
linkedin = LinkedInMessaging()
19+
if cookie_path.exists():
20+
with open(cookie_path, "rb") as cf:
21+
linkedin = LinkedInMessaging.from_pickle(cf.read())
22+
23+
if not await linkedin.logged_in():
24+
try:
25+
await linkedin.login("EMAIL", "PASSWORD")
26+
except ChallengeException:
27+
await linkedin.enter_2fa(input("2fa code: "))
28+
29+
with open(cookie_path, "wb+") as cf:
30+
cf.write(linkedin.to_pickle())
31+
32+
# Send a simple message that has some text.
33+
mc = MessageCreate(AttributedBody("test"))
34+
print(await linkedin.send_message(urn, mc))
35+
36+
# Send a multimedia message.
37+
with open("/path/to/the/cool-pic.jpg", "rb") as f:
38+
attachment = await linkedin.upload_media(f.read(), "cool-pic.jpg", "image/jpeg")
39+
40+
mc = MessageCreate(AttributedBody(), attachments=[attachment])
41+
print(await linkedin.send_message(urn, mc))
42+
43+
await linkedin.close()
44+
45+
46+
loop = asyncio.get_event_loop()
47+
asyncio.ensure_future(main())
48+
loop.run_forever()
49+
loop.close()

examples/simple-2fa.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from pathlib import Path
2+
3+
import asyncio
4+
from linkedin_messaging import LinkedInMessaging, ChallengeException
5+
6+
cookie_path = Path(__file__).parent.joinpath("cookies.pickle")
7+
8+
9+
async def main():
10+
linkedin = LinkedInMessaging()
11+
12+
try:
13+
await linkedin.login("EMAIL", "PASSWORD")
14+
except ChallengeException:
15+
await linkedin.enter_2fa(input("2fa code: "))
16+
17+
print(await linkedin.get_user_profile())
18+
19+
await linkedin.close()
20+
21+
22+
loop = asyncio.get_event_loop()
23+
asyncio.ensure_future(main())
24+
loop.run_forever()
25+
loop.close()

0 commit comments

Comments
 (0)