Send posts feed to gsheet

Tags: #linkedin #profile #post #stats #naas_drivers #automation #content #googlesheets
Author: Florent Ravenel
Last update: 2023-05-29 (Created: 2022-03-17)
Description: This notebook automates the process of sending LinkedIn posts to a Google Sheet for easy tracking and analysis.
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.


Import libraries

from naas_drivers import linkedin, gsheet
import naas
import pandas as pd

Setup LinkedIn

# Lindekin cookies
LI_AT = "AQEDARCNSioDe6wmAAABfqF-HR4AAAF-xYqhHlYAtSu7EZZEpFer0UZF-GLuz2DNSz4asOOyCRxPGFjenv37irMObYYgxxxxxxx"
# Linkedin profile url
# Number of posts updated in Gsheet (This avoid to requests the entire database)
LIMIT = 10

Setup your Google Sheet

👉 Get your spreadsheet URL 👉 Share your gsheet with our service account to connect : [email protected] 👉 Create your sheet before sending data into it
# Spreadsheet URL
# Sheet name

Setup Naas

naas.scheduler.add(cron="0 8 * * *")
# -> To delete your scheduler, please uncomment the line below and execute this cell
# naas.scheduler.delete()


Get data from Google Sheet

df_gsheet = gsheet.connect(SPREADSHEET_URL).get(sheet_name=SHEET_NAME)

Get new posts and update last posts stats

def get_new_posts(df_gsheet, key, limit=LIMIT, sleep=False):
posts = []
if len(df_gsheet) > 0:
posts = df_gsheet[key].unique()
df_posts_feed = linkedin.connect(LI_AT, JSESSIONID).profile.get_posts_feed(
PROFILE_URL, limit=-1, sleep=sleep
return df_posts_feed
# Get new
df_posts_feed = linkedin.connect(LI_AT, JSESSIONID).profile.get_posts_feed(
PROFILE_URL, limit=LIMIT, sleep=sleep
df_new = pd.concat([df_posts_feed, df_gsheet]).drop_duplicates(key, keep="first")
return df_new
df_new = get_new_posts(df_gsheet, "POST_URL", limit=LIMIT)


Send to Google Sheet

gsheet.connect(SPREADSHEET_URL).send(df_new, sheet_name=SHEET_NAME, append=False)