GuglielmoTor commited on
Commit
9287cc1
·
verified ·
1 Parent(s): 4d12aa9

Update linkedin_follower_stats.py

Browse files
Files changed (1) hide show
  1. 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
- now = datetime.now(timezone.utc)
 
 
 
 
 
199
  twelve_months_ago = now - timedelta(days=365)
200
  twelve_months_ago = twelve_months_ago.replace(day=1)
201
-
202
- start_date = int(twelve_months_ago.timestamp() * 1000)
203
- end_date = int(now.timestamp() * 1000) # You could omit this if letting LinkedIn default
204
-
205
- time_intervals = f"(timeRange:(start:{start_date},end:{end_date}),timeGranularityType:MONTH)"
206
-
207
- url = (
208
- f"{api_rest_base}/organizationalEntityFollowerStatistics"
209
- f"?q=organizationalEntity"
210
- f"&organizationalEntity={org_urn}"
211
- f"&timeIntervals={time_intervals}"
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