Spaces:
Restarting
Restarting
Update linkedin_follower_stats.py
Browse files- linkedin_follower_stats.py +22 -15
linkedin_follower_stats.py
CHANGED
@@ -195,24 +195,30 @@ def fetch_monthly_follower_gains(session, org_urn, api_rest_base):
|
|
195 |
# )
|
196 |
# logging.info(f"Fetching monthly follower gains from URL: {url}")
|
197 |
|
198 |
-
|
|
|
|
|
|
|
|
|
|
|
199 |
twelve_months_ago = now - timedelta(days=365)
|
200 |
twelve_months_ago = twelve_months_ago.replace(day=1)
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
)
|
213 |
|
214 |
results = []
|
215 |
try:
|
|
|
216 |
response = session.get(url)
|
217 |
response.raise_for_status() # Raises an HTTPError for bad responses (4XX or 5XX)
|
218 |
data = response.json()
|
@@ -396,11 +402,12 @@ def get_linkedin_follower_stats(comm_client_id, community_token, org_urn):
|
|
396 |
|
397 |
all_follower_data = []
|
398 |
|
399 |
-
monthly_gains = fetch_monthly_follower_gains(session, org_urn, API_REST_BASE)
|
400 |
-
all_follower_data.extend(monthly_gains)
|
401 |
-
|
402 |
demographics = fetch_follower_demographics(session, org_urn, functions_map, seniorities_map)
|
403 |
all_follower_data.extend(demographics)
|
404 |
|
|
|
|
|
|
|
|
|
405 |
logging.info(f"Successfully compiled {len(all_follower_data)} total follower stat entries for {org_urn}.")
|
406 |
return all_follower_data
|
|
|
195 |
# )
|
196 |
# logging.info(f"Fetching monthly follower gains from URL: {url}")
|
197 |
|
198 |
+
linkedin.headers.update({'LinkedIn-Version': "202502"})
|
199 |
+
# Replace with your LinkedIn organization URN
|
200 |
+
|
201 |
+
now = datetime.now()
|
202 |
+
|
203 |
+
# Subtract 12 months
|
204 |
twelve_months_ago = now - timedelta(days=365)
|
205 |
twelve_months_ago = twelve_months_ago.replace(day=1)
|
206 |
+
|
207 |
+
start_date1 = int(twelve_months_ago.timestamp() * 1000)
|
208 |
+
|
209 |
+
# Build the URL with time interval parameters
|
210 |
+
orgs_url = (
|
211 |
+
"https://api.linkedin.com/rest/organizationalEntityFollowerStatistics"
|
212 |
+
f"?q=organizationalEntity"
|
213 |
+
f"&organizationalEntity={org_urn}"
|
214 |
+
f"&timeIntervals.timeGranularityType=MONTH"
|
215 |
+
f"&timeIntervals.timeRange.start={start_date1}"
|
216 |
+
#f"&timeIntervals.timeRange.end={end_ms}"
|
217 |
)
|
218 |
|
219 |
results = []
|
220 |
try:
|
221 |
+
|
222 |
response = session.get(url)
|
223 |
response.raise_for_status() # Raises an HTTPError for bad responses (4XX or 5XX)
|
224 |
data = response.json()
|
|
|
402 |
|
403 |
all_follower_data = []
|
404 |
|
|
|
|
|
|
|
405 |
demographics = fetch_follower_demographics(session, org_urn, functions_map, seniorities_map)
|
406 |
all_follower_data.extend(demographics)
|
407 |
|
408 |
+
session = create_session(comm_client_id, token=token_dict) #try a new session with base header because follower gains is not working
|
409 |
+
monthly_gains = fetch_monthly_follower_gains(session, org_urn, API_REST_BASE)
|
410 |
+
all_follower_data.extend(monthly_gains)
|
411 |
+
|
412 |
logging.info(f"Successfully compiled {len(all_follower_data)} total follower stat entries for {org_urn}.")
|
413 |
return all_follower_data
|