Links

Update followers from linkedin

Tags: #hubspot #crm #sales #contact #naas_drivers #linkedin #network #scheduler #naas #automation
Author: Florent Ravenel​
Description: This notebook update the LinkedIn followers count for a contact in HubSpot.

Input

Import libraries

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

Setup HubSpot

👉 Starting November 30, 2022, HubSpot API keys no longer enable access to HubSpot APIs, so in Naas version 2.8.3 and above, you need create a private app and use the access token.
# Enter Your Access Token
HS_ACCESS_TOKEN = naas.secret.get("HS_ACCESS_TOKEN") or "YOUR_HS_ACCESS_TOKEN"

Setup LinkedIn

If you are using the Chrome Extension:
  • ​Create a new token​
  • Copy/Paste your token in your extension
  • Login/Logout your LinkedIn account
  • Your secrets "LINKEDIN_LI_AT" and "LINKEDIN_JSESSIONID" will be added directly on your naas everytime you login and logout.
or
If you are not using the Google Chrome Extension, learn how to get your cookies on LinkedIn and set up the values below:
  • 🍪 li_at
  • 🍪 JSESSIONID
# Cookies
LI_AT = naas.secret.get("LINKEDIN_LI_AT") or 'YOUR_COOKIE_LI_AT'
JSESSIONID = naas.secret.get("LINKEDIN_JSESSIONID") or 'YOUR_COOKIE_JSESSIONID'
​
# LinkedIn update limit
LIMIT = 15

Setup Naas

naas.scheduler.add(cron="0 8 * * *")
​
#-> Uncomment the line below (by removing the hashtag) to remove your scheduler
# naas.scheduler.delete()

Model

Get all contacts in HubSpot

properties_list = [
"hs_object_id",
"firstname",
"lastname",
"linkedinbio",
"linkedinconnections",
]
hubspot_contacts = hubspot.connect(HS_ACCESS_TOKEN).contacts.get_all(properties_list)
hubspot_contacts

Filter to get linkedinconnections = "Not Defined" and "linkedinbio" = defined

df_to_update = hubspot_contacts.copy()
​
# Cleaning
df_to_update = df_to_update.fillna("Not Defined")
​
# Filter on "Not defined"
df_to_update = df_to_update[(df_to_update.linkedinbio != "Not Defined") &
(df_to_update.linkedinconnections == "Not Defined")]
​
# Limit to last x contacts
df_to_update = df_to_update.sort_values(by="createdate", ascending=False)[:LIMIT].reset_index(drop=True)
​
df_to_update

Get followers from Linkedin

for _, row in df_to_update.iterrows():
linkedinbio = row.linkedinbio
# Get followers
df = linkedin.connect(LI_AT, JSESSIONID).profile.get_network(linkedinbio)
linkedinconnections = df.loc[0, "FOLLOWERS_COUNT"]
# Get linkedinbio
df_to_update.loc[_, "linkedinconnections"] = linkedinconnections
df_to_update

Output

Update followers in Hubspot

for _, row in df_to_update.iterrows():
# Init data
data = {}
# Get data
hs_object_id = row.hs_object_id
linkedinconnections = row.linkedinconnections
​
# Update LK Bio
if linkedinconnections != None:
data = {"properties": {"linkedinconnections": linkedinconnections}}
hubspot.connect(HS_ACCESS_TOKEN).contacts.patch(hs_object_id, data)