-
Notifications
You must be signed in to change notification settings - Fork 461
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(LinkedIn): Add Send connections to Google Sheets spreadsheet
- Loading branch information
1 parent
bfd8eac
commit 47a08a3
Showing
1 changed file
with
285 additions
and
0 deletions.
There are no files selected for viewing
285 changes: 285 additions & 0 deletions
285
LinkedIn/LinkedIn_Send_connections_to_Google_Sheets_spreadsheet.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,285 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "65ca55c0-b9d7-4464-b5fb-ce86bd16580c", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"<img width=\"10%\" alt=\"Naas\" src=\"https://landen.imgix.net/jtci2pxwjczr/assets/5ice39g4.png?w=160\"/>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "00222c10-8104-400a-8b95-b98789a88212", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"# LinkedIn - Send connections to Google Sheets spreadsheet" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "8c5010fc-06ad-4b73-97a6-8ffff1d39c31", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Tags:** #linkedin #google #sheets #spreadsheet #connections #python" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "7b024496-dc90-44bc-8637-3215b26eac6c", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Author:** [Florent Ravenel](https://www.linkedin.com/in/florent-ravenel/)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "bd311d19-791d-4a97-9e03-a7fdf3b93889", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Last update:** 2023-10-16 (Created: 2023-10-16)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "a0607b4d-98c6-4f13-a76e-be03206ef7d9", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**Description:** This notebook will show how to send LinkedIn connections to a Google Sheets spreadsheet. It is usefull for organizations to keep track of their contacts and build relationships." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "136a7649-4bfb-41e9-9a5a-41d264fd9f7e", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"**References:**\n- [LinkedIn API Documentation](https://docs.microsoft.com/en-us/linkedin/shared/references/v2/profile/profile-views?context=linkedin/consumer/context)\n- [Google Sheets API Documentation](https://developers.google.com/sheets/api/reference/rest/)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "67544296-7ab2-43b4-a589-2e86573d3849", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"## Input" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "b9317d5e-3cca-4e31-8cb2-4a8e5d7a58d0", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### Import libraries" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "385e374a-3ee4-42eb-a98f-ec43c38c1a31", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": "import requests\nimport json\nimport pandas as pd\nimport gspread\nfrom oauth2client.service_account import ServiceAccountCredentials", | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "b28454c5-5966-4a54-8614-7d1a4aad80bb", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### Setup variables\n- `access_token`: Access token to access LinkedIn API. [Link to procedure](https://docs.microsoft.com/en-us/linkedin/shared/authentication/authorization-code-flow?context=linkedin/consumer/context)\n- `google_credentials`: Credentials to access Google Sheets API. [Link to procedure](https://gspread.readthedocs.io/en/latest/oauth2.html)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "f1907404-4b00-4166-a2e6-c2ace779f9ee", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": "access_token = \"<access_token>\"\ngoogle_credentials = {\n \"type\": \"service_account\",\n \"project_id\": \"<project_id>\",\n \"private_key_id\": \"<private_key_id>\",\n \"private_key\": \"<private_key>\",\n \"client_email\": \"<client_email>\",\n \"client_id\": \"<client_id>\",\n \"auth_uri\": \"https://accounts.google.com/o/oauth2/auth\",\n \"token_uri\": \"https://oauth2.googleapis.com/token\",\n \"auth_provider_x509_cert_url\": \"https://www.googleapis.com/oauth2/v1/certs\",\n \"client_x509_cert_url\": \"<client_x509_cert_url>\",\n}", | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "f59e059d-4869-4776-b765-c2ee21c0d1a7", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"## Model" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "77a51f92-0f94-4fe3-90bb-034d47746138", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### Get LinkedIn connections" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "673ebdf6-2fce-4683-bb7a-b1edfaa527c7", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"Long description of the function: This function will get the connections from the LinkedIn API and store them in a Pandas DataFrame." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "43585a0c-a8c2-4b40-b575-ce7ab3935b72", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": "def get_connections():\n headers = {\"Authorization\": \"Bearer \" + access_token}\n url = \"https://api.linkedin.com/v2/connections\"\n response = requests.get(url, headers=headers)\n response_json = json.loads(response.text)\n connections = response_json[\"elements\"]\n df = pd.DataFrame(connections)\n return df", | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "a7e83147-180c-4685-abba-c595a721623b", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### Send connections to Google Sheets" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "efe5852c-c1cd-4578-a144-6d7e950906fd", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"Long description of the function: This function will send the connections from the Pandas DataFrame to a Google Sheets spreadsheet." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "50cbda1a-77dd-47d1-8d38-710dbfbf9c82", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": "def send_connections_to_google_sheets(df):\n scope = [\n \"https://spreadsheets.google.com/feeds\",\n \"https://www.googleapis.com/auth/drive\",\n ]\n credentials = ServiceAccountCredentials.from_json_keyfile_dict(\n google_credentials, scope\n )\n gc = gspread.authorize(credentials)\n spreadsheet = gc.open(\"LinkedIn Connections\")\n worksheet = spreadsheet.get_worksheet(0)\n worksheet.clear()\n worksheet.update([df.columns.values.tolist()] + df.values.tolist())", | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "746dbc4e-a908-4d44-b1f0-0f339e58f825", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"## Output" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "6cfe6bab-88a1-4449-a2ab-94c07a3906ce", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"### Display result" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "ca7d57cb-5d0b-471d-a6da-1137f3f2151f", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": "connections_df = get_connections()\nsend_connections_to_google_sheets(connections_df)", | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "6a2a06f8-bdc0-45fe-9546-5a7ff6399df0", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
" " | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.6" | ||
}, | ||
"widgets": { | ||
"application/vnd.jupyter.widget-state+json": { | ||
"state": {}, | ||
"version_major": 2, | ||
"version_minor": 0 | ||
} | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |