Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: LinkedIn - Send like to post #2104

Merged
merged 3 commits into from
Aug 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
331 changes: 331 additions & 0 deletions LinkedIn/LinkedIn_Send_like_to_post.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,331 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "2954e3e8-1b11-4946-858b-478d5878e639",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"<img width=\"10%\" alt=\"Naas\" src=\"https://landen.imgix.net/jtci2pxwjczr/assets/5ice39g4.png?w=160\"/>"
]
},
{
"cell_type": "markdown",
"id": "f8bc3806-ec06-4c6e-a405-76bf9eebe6d5",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"# LinkedIn - Send like to post"
]
},
{
"cell_type": "markdown",
"id": "945e3c7f-cdcd-4976-974c-29fabebb4805",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Tags:** #linkedin #socialmedia #like #post #python #api"
]
},
{
"cell_type": "markdown",
"id": "c1dd5362-7198-428a-a177-6d2b84e73834",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Author:** [Florent Ravenel](https://www.linkedin.com/in/florent-ravenel)"
]
},
{
"cell_type": "markdown",
"id": "5396fb5f-e300-4b3a-8760-7cc3178a5035",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Last update:** 2023-08-02 (Created: 2023-08-02)"
]
},
{
"cell_type": "markdown",
"id": "d8e8d954-bfb2-4c90-84fd-3c0b0edb0ed5",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Description:** This notebook will show how to send a like to a post published on LinkedIn."
]
},
{
"cell_type": "markdown",
"id": "811898cf-c185-468f-8e29-fc3e9d65e626",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"\n",
"<div class=\"alert alert-info\" role=\"info\" style=\"margin: 10px\">\n",
"<b>Disclaimer:</b><br>\n",
"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.\n",
"\n",
"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.\n",
"<br>\n",
"</div>"
]
},
{
"cell_type": "markdown",
"id": "40a7d355-df4e-4e28-ac9f-102bb85c3512",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Input"
]
},
{
"cell_type": "markdown",
"id": "be7128ea-5322-438b-abaa-bc131017f379",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Import libraries"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9c3dc68e-ef62-48b2-be36-3f973eddfa45",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"from naas_drivers import linkedin\n",
"import requests\n",
"import naas"
]
},
{
"cell_type": "markdown",
"id": "a5df344a-3e1e-4c85-a342-3a8ab164f6ff",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Setup variables\n",
"**Mandatory**\n",
"\n",
"[Learn how to get your cookies on LinkedIn](https://www.notion.so/LinkedIn-driver-Get-your-cookies-d20a8e7e508e42af8a5b52e33f3dba75)\n",
"- `li_at`: Cookie used to authenticate Members and API clients\n",
"- `JSESSIONID`: Cookie used for Cross Site Request Forgery (CSRF) protection and URL signature validation\n",
"\n",
"**Optional**\n",
"\n",
"- `post_url`: This variable represents the post URL\n",
"- `reaction_type`: This variable represents the type of reaction to sent to the post. It could be \"LIKE\", \"PRAISE\", \"APPRECIATION\", \"EMPATHY\", \"INTEREST\", \"ENTERTAINMENT\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ee606036-6453-461f-9ba4-300000de1d08",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"# Mandatory\n",
"li_at = naas.secret.get(\"LINKEDIN_LI_AT\") or \"YOUR_LINKEDIN_LI_AT\" #example: AQFAzQN_PLPR4wAAAXc-FCKmgiMit5FLdY1af3-2\n",
"JSESSIONID = naas.secret.get(\"LINKEDIN_JSESSIONID\") or \"YOUR_LINKEDIN_JSESSIONID\" #example: ajax:8379907400220387585\n",
"post_url = \"\"\n",
"\n",
"# Optional\n",
"reaction_type = \"LIKE\" # \"PRAISE\", \"APPRECIATION\", \"EMPATHY\", \"INTEREST\", \"ENTERTAINMENT\""
]
},
{
"cell_type": "markdown",
"id": "9109bdf6-dddf-4eb6-aad2-5f27375546bc",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Model"
]
},
{
"cell_type": "markdown",
"id": "245c3589-746b-4db6-8271-2bcd57d1f0d6",
"metadata": {},
"source": [
"### Connect to LinkedIn"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1755794e-2230-4279-910e-b89f54ec3027",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"LK = linkedin.connect(li_at, JSESSIONID)\n",
"cookies = LK.cookies\n",
"headers = LK.headers"
]
},
{
"cell_type": "markdown",
"id": "b3a34294-4613-4d59-af98-7f34e47f1f97",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Send like to post"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6cadd18a-5dce-4d6f-8915-edfa0d72697d",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"def send_like(\n",
" cookies,\n",
" headers,\n",
" post_url,\n",
" reaction_like\n",
"):\n",
" # Check post URL\n",
" response = None\n",
" if not post_url.startswith(\"https://www.linkedin.com/\"):\n",
" print(\"🛑 Post URL not valid! Please update it in the input section.\")\n",
" return response\n",
" # Parse url to get activity id\n",
" if \":activity:\" in post_url:\n",
" activity_tag = \":activity:\"\n",
" tag_end = \"?\"\n",
" elif \"-activity-\" in post_url:\n",
" activity_tag = \"-activity-\"\n",
" tag_end = \"-\"\n",
" activity_id = post_url.split(activity_tag)[-1].split(tag_end)[0]\n",
" \n",
" # Send like\n",
" url = f\"https://www.linkedin.com/voyager/api/voyagerSocialDashReactions?threadUrn=urn%3Ali%3Aactivity%3A{activity_id}\"\n",
" payload = {\"reactionType\": reaction_like}\n",
" response = requests.post(url, headers=headers, cookies=cookies, json=payload)\n",
" return response\n",
"\n",
"res = send_like(\n",
" cookies,\n",
" headers,\n",
" post_url,\n",
" reaction_like\n",
")"
]
},
{
"cell_type": "markdown",
"id": "4a1385da-9332-44d8-ae79-908f36aeb3a5",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Output"
]
},
{
"cell_type": "markdown",
"id": "00782b2b-9ffd-4c84-9ec5-73626668a0cc",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Display result"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "96eeb451-7be1-48cd-a664-f8399eddaae6",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"if res:\n",
" if res.status_code == 201:\n",
" print(\"👍 Like sent to:\", post_url)\n",
" else:\n",
" print(res.json())"
]
},
{
"cell_type": "markdown",
"id": "2f8fbfa5-b9fc-4cdc-8474-7b05dfa1d751",
"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
}
Loading