Links

Update jobtitle country industry from linkedin

Tags: #hubspot #crm #sales #contact #naas_drivers #linkedin #identity #scheduler #naas #automation
Author: Florent Ravenel
Description: This notebook update the jobtitle, country and industry 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:
  • 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",
"jobtitle",
"country",
"industry",
]
hubspot_contacts = hubspot.connect(HS_ACCESS_TOKEN).contacts.get_all(properties_list)
hubspot_contacts

Filter to get "jobtitle", "country", "industry" = "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.jobtitle == "Not Defined")
& (df_to_update.country == "Not Defined")
& (df_to_update.industry == "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 identity from Linkedin

for _, row in df_to_update.iterrows():
linkedinbio = row.linkedinbio
# Get followers
df = linkedin.connect(LI_AT, JSESSIONID).profile.get_identity(linkedinbio)
jobtitle = df.loc[0, "OCCUPATION"]
industry = df.loc[0, "INDUSTRY_NAME"]
country = df.loc[0, "COUNTRY"]
# Get linkedinbio
df_to_update.loc[_, "jobtitle"] = jobtitle
df_to_update.loc[_, "industry"] = industry
df_to_update.loc[_, "country"] = country
df_to_update

Output

Update jobtitle, country, industry in Hubspot

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