Withdraw pending profile invitations
Tags: #linkedin #invitation #pending #naas_drivers #content #automation
Last update: 2023-05-29 (Created: 2022-09-22)
Description: This notebook allows users to view and manage pending profile invitations sent through 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 time
from datetime import datetime
from dateutil.relativedelta import relativedelta
import json
import requests
# Credentials
LI_AT = (
naas.secret.get("LINKEDIN_LI_AT")
or "AQFAzQN_PLPR4wAAAXc-FCKmgiMit5FLdY1af3-2AAXc-FCKmgiMit5FLdY1AAXc-FCKmgiMit5FLdY1"
)
JSESSIONID = naas.secret.get("LINKEDIN_JSESSIONID") or "ajax:8379907400220XXXXX"
# Invitations pending limit in month(s)
LIMIT = 3
For information on changing this setting, please check https://crontab.guru/ for information on the required CRON syntax
# the default settings below will make the notebook run everyday at 14:00 on Friday
SCHEDULER_CRON = "0 14 * * 5"
df_invitations_sent = linkedin.connect(LI_AT, JSESSIONID).invitation.get_sent()
print("Pending invitations:", len(df_invitations_sent))
df_invitations_sent.head(1)
def out_of_limit(df):
# Get limit date
date_limit = datetime.today() - relativedelta(months=LIMIT)
# Create time diff columns
df.loc[:, "TIME_DIFF"] = 0
df.loc[pd.to_datetime(df["SENT_AT"]) < date_limit, "TIME_DIFF"] = 1
# Filter on limit exceed
df = df[df["TIME_DIFF"] == 1]
return df.reset_index(drop=True)
df_withdraw = out_of_limit(df_invitations_sent)
print("Invitations to withdraw:", len(df_withdraw))
df_withdraw.head(1)
def withdraw_invitation(invitation_id):
LinkedIn = linkedin.connect(LI_AT, JSESSIONID)
cookies = LinkedIn.cookies
headers = LinkedIn.headers
payload = {
"inviteActionType": "ACTOR_WITHDRAW",
"inviteActionData": [
{
"entityUrn": f"urn:li:fs_relInvitation:{invitation_id}",
"genericInvitation": False,
"genericInvitationType": "CONNECTION",
}
],
}
req_url = "https://www.linkedin.com/voyager/api/relationships/invitations?action=closeInvitations"
res = requests.post(
req_url, data=json.dumps(payload), cookies=cookies, headers=headers
)
res.raise_for_status()
res_json = res.json()
return res_json
def withdraw_invitations(df):
for index, row in df.iterrows():
fullname = row["FULLNAME"]
invitation_id = row["INVITATION_ID"]
print(f"➡️ Withdrawing from invitations pending:", fullname)
# Get distance with profile
try:
withdraw_invitation(invitation_id)
except Exception as e:
print("❌ Withdraw not available", e)
time.sleep(3)
return df
withdraw_invitations(df_withdraw)
naas.scheduler.add(cron=SCHEDULER_CRON)
# to de-schedule this notebook, simply run the following command:
# naas.scheduler.delete()
Last modified 1mo ago