Spaces:
Running
Running
Update Linkedin_Data_API_Calls.py
Browse files- Linkedin_Data_API_Calls.py +40 -4
Linkedin_Data_API_Calls.py
CHANGED
@@ -69,7 +69,45 @@ def _get_sentiment_from_text(text_to_analyze):
|
|
69 |
|
70 |
return {"label": dominant_sentiment_label, "counts": sentiment_counts}
|
71 |
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
# --- Post Retrieval Functions ---
|
74 |
def fetch_linkedin_posts_core(comm_client_id, community_token, org_urn, count):
|
75 |
"""
|
@@ -266,9 +304,7 @@ def fetch_linkedin_posts_core(comm_client_id, community_token, org_urn, count):
|
|
266 |
"published_at_iso": published_at_iso,
|
267 |
"organization_urn": p.get("author", f"urn:li:organization:{org_urn.split(':')[-1]}"),
|
268 |
"is_ad": 'adContext' in p,
|
269 |
-
"media_category": p.get("content"
|
270 |
-
p.get("content",{}).get("com.linkedin.voyager.feed.render.ImageComponent",{}).get("mediaCategory") or \
|
271 |
-
p.get("content",{}).get("com.linkedin.voyager.feed.render.ArticleComponent",{}).get("mediaCategory") or "NONE"
|
272 |
})
|
273 |
logging.info(f"Processed {len(processed_raw_posts)} posts with core data.")
|
274 |
return processed_raw_posts, stats_map, "DefaultOrgName"
|
|
|
69 |
|
70 |
return {"label": dominant_sentiment_label, "counts": sentiment_counts}
|
71 |
|
72 |
+
def get_post_media_category(post_content):
|
73 |
+
"""
|
74 |
+
Determines the media category from the post's content object.
|
75 |
+
Args:
|
76 |
+
post_content (dict or None): The content dictionary of the post.
|
77 |
+
Returns:
|
78 |
+
str: The determined media category (e.g., "Video", "Article", "Document", "Image", "Multi-Image", "NONE").
|
79 |
+
"""
|
80 |
+
if not post_content:
|
81 |
+
return "NONE"
|
82 |
+
|
83 |
+
# 1. Check for specific LinkedIn Video Component (from your original logic)
|
84 |
+
# You might want to refine this if 'mediaCategory' within the video component is more specific
|
85 |
+
if "com.linkedin.voyager.feed.render.LinkedInVideoComponent" in post_content:
|
86 |
+
# video_component_data = post_content.get("com.linkedin.voyager.feed.render.LinkedInVideoComponent", {})
|
87 |
+
# return video_component_data.get("mediaCategory", "Video") # Example if you want to use its specific category
|
88 |
+
return "Video"
|
89 |
+
|
90 |
+
# 2. Check for Article (based on your "old code" and examples)
|
91 |
+
if 'article' in post_content:
|
92 |
+
return "Article"
|
93 |
+
|
94 |
+
# 3. Check for Multi-Image (based on your "old code")
|
95 |
+
if 'multiImage' in post_content:
|
96 |
+
return "Multi-Image"
|
97 |
+
|
98 |
+
# 4. Check for Media (Document or Image - based on your "old code" and examples)
|
99 |
+
if 'media' in post_content:
|
100 |
+
media_item = post_content['media']
|
101 |
+
# Heuristic from your "old code": if 'title' is present, it's likely a Document.
|
102 |
+
if 'title' in media_item:
|
103 |
+
# Example: "content": {"media": {"title": "...", "id": "urn:li:document:..."}}
|
104 |
+
return "Document"
|
105 |
+
# Else, if 'id' is present (and no title was found for Document), assume Image.
|
106 |
+
elif 'id' in media_item:
|
107 |
+
# Example: "content": {"media": {"altText": "", "id": "urn:li:image:..."}}
|
108 |
+
return "Image"
|
109 |
+
|
110 |
+
return "NONE"
|
111 |
# --- Post Retrieval Functions ---
|
112 |
def fetch_linkedin_posts_core(comm_client_id, community_token, org_urn, count):
|
113 |
"""
|
|
|
304 |
"published_at_iso": published_at_iso,
|
305 |
"organization_urn": p.get("author", f"urn:li:organization:{org_urn.split(':')[-1]}"),
|
306 |
"is_ad": 'adContext' in p,
|
307 |
+
"media_category": get_post_media_category(p.get("content")),
|
|
|
|
|
308 |
})
|
309 |
logging.info(f"Processed {len(processed_raw_posts)} posts with core data.")
|
310 |
return processed_raw_posts, stats_map, "DefaultOrgName"
|