Comment on page
Send event invitations post engagements
Tags: #linkedin #events #invitations #naas_drivers #snippet
Last update: 2023-05-29 (Created: 2022-07-08)
Description: This notebook allows users to send event invitations and post engagements on LinkedIn.
Disclaimer:
This code is in no way affiliated with, authorized, maintained, sponsored or endorsed by Linkedin or any of its affiliates or subsidiaries. It uses an independent and unofficial API. Use at your own risk.
This project violates Linkedin's User Agreement Section 8.2, and because of this, Linkedin may (and will) temporarily or permanently ban your account. We are not responsible for your account being banned.
from naas_drivers import linkedin
import pandas as pd
import naas
import requests
import time
# Credentials
LI_AT = (
naas.secret.get("LI_AT") or "ENTER_YOUR_COOKIE_LI_AT_HERE"
) # EXAMPLE: AQFAzQN_PLPR4wAAAXc-FCKmgiMit5FLdY1af3-2
JSESSIONID = (
naas.secret.get("JSESSIONID") or "ENTER_YOUR_COOKIE_JSESSIONID_HERE"
) # EXAMPLE: ajax:8379907400220387585
# Event URL
EVENT_URL = "ENTER_YOUR_EVENT_URL_HERE" # EXAMPLE: https://www.linkedin.com/events/XXXXXXXXXXXX/
# Post URL
POST_URL = (
"ENTER_YOUR_POST_URL_HERE" # EXAMPLE: https://www.linkedin.com/posts/XXXXXXXXXXXX/
)
df_attendees = linkedin.connect(LI_AT, JSESSIONID).event.get_guests(EVENT_URL)
print("✅ Attendees fetched:", len(df_attendees))
df_attendees.head(1)
def get_engagements(post_url):
# Get engagements
df_likes = linkedin.connect(LI_AT, JSESSIONID).post.get_likes(post_url)
df_comments = linkedin.connect(LI_AT, JSESSIONID).post.get_comments(post_url)
# Concat
df = (
pd.concat([df_likes, df_comments])
.drop_duplicates("PROFILE_ID", keep="last")
.reset_index(drop=True)
)
return df
df_engagements = get_engagements(POST_URL)
print("✅ Engagements fetched:", len(df_engagements))
df_engagements.head(1)
def get_new_invitations(df_attendees, df_engagements):
attendees_list = df_attendees.PROFILE_ID.unique()
df = df_engagements[~df_engagements.PROFILE_ID.isin(attendees_list)]
return df
df_new_invitations = get_new_invitations(df_attendees, df_engagements)
print("✅ New invitations fetched:", len(df_new_invitations))
df_new_invitations.head(1)
LinkedIn = linkedin.connect(LI_AT, JSESSIONID)
cookies = LinkedIn.cookies
headers = LinkedIn.headers
def send_invitations(profile_id, event_url):
event_id = event_url.split("/events/")[-1].split("/")[0]
payload = {
"invitations": [
{
"emberEntityName": "growth/invitation/norm-invitation",
"invitee": {
"com.linkedin.voyager.growth.invitation.GenericInvitee": {
"inviteeUrn": f"urn:li:fs_miniProfile:{profile_id}"
}
},
"trackingId": "4T5tesDXQDqC9ArO15TLag==",
"inviterUrn": f"urn:li:fs_professionalEvent:{event_id}",
}
],
"defaultCountryCode": "",
}
req_url = f"https://www.linkedin.com/voyager/api/growth/normInvitations?action=batchCreate"
res = requests.post(req_url, cookies=cookies, headers=headers, json=payload)
res.raise_for_status()
return res
def send_invitation(df):
# Check if new invitations to perform
if len(df) == 0:
print("🤙 No new invitations to send")
return df
# Loop
for index, row in df.iterrows():
profile = row["FULLNAME"]
profile_id = row["PROFILE_ID"]
print(f"➡️ Checking :", profile, profile_id)
# Get distance with profile
try:
send_invitations(profile_id, EVENT_URL)
print(index, "- 🙌 Invitation successfully sent")
except Exception as e:
print("❌ Invitation not sent", e)
time.sleep(3)
send_invitation(df_new_invitations)
Last modified 3mo ago