LinkedinMonitor / mentions_dashboard.py
GuglielmoTor's picture
Update mentions_dashboard.py
8081391 verified
raw
history blame
3.54 kB
import json
import time
import re
import os
from datetime import datetime
from urllib.parse import quote
from requests_oauthlib import OAuth2Session
from textblob import TextBlob
import matplotlib.pyplot as plt
from sessions import create_session
def extract_text_from_commentary(commentary):
return re.sub(r"{.*?}", "", commentary).strip()
def analyze_sentiment(text):
return TextBlob(text).sentiment.polarity
def generate_mentions_dashboard(comm_client_id, comm_token_dict):
org_urn = "urn:li:organization:19010008"
encoded_urn = quote(org_urn, safe='')
session = create_session(comm_client_id, token=comm_token_dict)
session.headers.update({
"X-Restli-Protocol-Version": "2.0.0"
})
base_url = (
"https://api.linkedin.com/rest/organizationalEntityNotifications"
"?q=criteria"
"&actions=List(COMMENT,SHARE_MENTION)"
f"&organizationalEntity={encoded_urn}"
"&count=20"
)
all_notifications = []
start = 0
while True:
url = f"{base_url}&start={start}"
resp = session.get(url)
if resp.status_code != 200:
break
data = resp.json()
elements = data.get("elements", [])
all_notifications.extend(elements)
if len(elements) < data.get("paging", {}).get("count", 0):
break
start += len(elements)
time.sleep(0.5)
# Extract mentions and their share URNs
mention_shares = [e.get("generatedActivity") for e in all_notifications if e.get("action") == "SHARE_MENTION"]
mention_data = []
for share_urn in mention_shares:
if not share_urn:
continue
encoded_share_urn = quote(share_urn, safe='')
share_url = f"https://api.linkedin.com/rest/posts/{encoded_share_urn}"
response = session.get(share_url)
if response.status_code != 200:
continue
post = response.json()
commentary_raw = post.get("commentary", "")
if not commentary_raw:
continue
commentary = extract_text_from_commentary(commentary_raw)
sentiment = analyze_sentiment(commentary)
timestamp = post.get("createdAt", 0)
dt = datetime.fromtimestamp(timestamp / 1000.0)
mention_data.append({
"date": dt,
"text": commentary,
"sentiment": sentiment
})
# Save HTML
html_parts = ["<h2 style='text-align:center;'>📣 Mentions Sentiment Dashboard</h2>"]
for mention in mention_data:
html_parts.append(f"""
<div style='border:1px solid #ccc; border-radius:10px; padding:15px; margin:10px;'>
<p><strong>Date:</strong> {mention["date"].strftime('%Y-%m-%d')}</p>
<p>{mention["text"]}</p>
<p><strong>Sentiment:</strong> {mention["sentiment"]:.2f}</p>
</div>
""")
html_path = "mentions_dashboard.html"
with open(html_path, "w", encoding="utf-8") as f:
f.write("\n".join(html_parts))
# Plot
if mention_data:
dates = [m["date"] for m in mention_data]
sentiments = [m["sentiment"] for m in mention_data]
plt.figure(figsize=(10, 5))
plt.plot(dates, sentiments, marker='o', linestyle='-', color='blue')
plt.title("Sentiment Over Time")
plt.xlabel("Date")
plt.ylabel("Sentiment")
plt.grid(True)
plt.tight_layout()
plt.savefig("mentions_sentiment_plot.png")
plt.close()
return html_path