Links
Comment on page

Send profile followers by email

Tags: #linkedin #network #followers #naas_drivers #content #snippet #dataframe
Author: Florent Ravenel​
Last update: 2023-05-29 (Created: 2022-06-02)
Description: This notebook allows users to send emails to their LinkedIn profile followers.
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.

Input

Import library

from naas_drivers import linkedin, emailbuilder
import naas
import pandas as pd
from datetime import datetime

Setup LinkedIn

# LinkedIn cookies
LI_AT = "YOUR_COOKIE_LI_AT" # EXAMPLE AQFAzQN_PLPR4wAAAXc-FCKmgiMit5FLdY1af3-2
JSESSIONID = "YOUR_COOKIE_JSESSIONID" # EXAMPLE ajax:8379907400220387585

Setup Outputs

Create CSV to store your followers. PS: This CSV could be used in others LinkedIn templates.
csv_output = f"LINKEDIN_FOLLOWERS.csv"

Setup Naas notification

EMAIL_TO = "ENTER_RECIPIENT_EMAIL_HERE" # you will receive weekly summary at this email
EMAIL_FROM = "ENTER_SENDER_EMAIL_HERE" # summary will have this email as sender. Only available for your naas email
EMAIL_SUBJECT = "LinkedIn Followers Export" # subject of your email

Setup Naas scheduler

Schedule your notebook with the naas scheduler feature
# the default settings below will make the notebook run at 08:00 on the 1st of every month
# for information on changing this setting, please check https://crontab.guru/ for information on the required CRON syntax
naas.scheduler.add(cron="0 8 1 * *")
​
# to de-schedule this notebook, simply run the following command:
# naas.scheduler.delete()

Model

Get followers from CSV

def read_csv(file_path):
try:
df = pd.read_csv(file_path)
except FileNotFoundError as e:
# Empty dataframe returned
return pd.DataFrame()
return df
​
​
df_followers = read_csv(csv_output)
df_followers

Update new followers

If CSV is empty, we will get all your followers
def update_last_posts(df, csv_output):
# Init output
df_new = pd.DataFrame()
​
# Check if dataframe init is empty
if len(df) > 0:
# If dataframe not empty, get last followers
profiles = df.PROFILE_ID.unique()
start = 0
count = 100
while True:
tmp_new = linkedin.connect(LI_AT, JSESSIONID).network.get_followers(
start=start, limit=count
)
# Check if existing profile in each call
tmp_df = tmp_new[tmp_new.PROFILE_ID.isin(profiles)]
if len(tmp_df) > 0:
break
​
# Get more profile
df_new = pd.concat([df_new, tmp_new])
start += count
else:
# If dataframe empty, get all followers
df_new = linkedin.connect(LI_AT, JSESSIONID).network.get_followers(limit=-1)
​
# Concat
print(f"-> New followers fetched: {len(df_new)}.")
df_update = pd.concat([df, df_new])
​
# Cleaning to remove duplicates if needed
if len(df_update) > 0:
df_update = df_update.drop_duplicates("PROFILE_ID", keep="first")
​
# Add extract date
df_update["DATE_EXTRACT"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
​
# Save dataframe in CSV
df_update.to_csv(csv_output, index=False)
​
# Return all followers
print(f"Total followers fetched: {len(df_update)}.")
return df_update.reset_index(drop=True)
​
​
df_update = update_last_posts(df_followers, csv_output)
df_update

Share output with naas

# Share output with naas
csv_link = naas.asset.add(csv_output)
​
# -> Uncomment the line below to remove your asset
# naas.asset.delete(csv_output)

Create email content

def email_content(csv_link):
content = {
"header_naas": (
"<a href='https://www.naas.ai/'>"
"<img align='center' width='30%' target='_blank' style='border-radius:5px;'"
"src='https://landen.imgix.net/jtci2pxwjczr/assets/5ice39g4.png?w=160'"
"alt='Please provide more information.'/>"
"</a>"
),
"txt_0": emailbuilder.text(
f"Hi there,<br><br>"
f"Here's your LinkedIn followers report as of {datetime.now().strftime('%Y-%m-%d')}.<br><br>"
),
"button": emailbuilder.button(csv_link, "Download CSV"),
"signature": "Naas Team",
"footer": emailbuilder.footer_company(naas=True),
}
email_content = emailbuilder.generate(display="iframe", **content)
return email_content
​
​
email_content = email_content(csv_link)

Output

Send followers by email

# sends the email
naas.notification.send(
email_to=EMAIL_TO,
subject=EMAIL_SUBJECT,
html=email_content,
email_from=EMAIL_FROM,
files=[csv_output],
)