Spaces:
Running
Running
Update linkedin_follower_stats.py
Browse files- linkedin_follower_stats.py +27 -15
linkedin_follower_stats.py
CHANGED
@@ -196,26 +196,38 @@ def fetch_monthly_follower_gains(session, org_urn, api_rest_base):
|
|
196 |
# )
|
197 |
# logging.info(f"Fetching monthly follower gains from URL: {url}")
|
198 |
|
199 |
-
|
|
|
|
|
200 |
|
201 |
-
|
202 |
-
|
203 |
-
start_ms = int(twelve_months_ago.timestamp() * 1000)
|
204 |
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
)
|
213 |
|
214 |
results = []
|
|
|
|
|
|
|
215 |
try:
|
216 |
-
|
217 |
-
|
218 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
219 |
|
220 |
elements = data.get('elements', [])
|
221 |
if not elements:
|
|
|
196 |
# )
|
197 |
# logging.info(f"Fetching monthly follower gains from URL: {url}")
|
198 |
|
199 |
+
now_utc = datetime.now(timezone.utc)
|
200 |
+
start_of_reporting_period = (now_utc - timedelta(days=365)).replace(day=1, hour=0, minute=0, second=0, microsecond=0)
|
201 |
+
start_ms = int(start_of_reporting_period.timestamp() * 1000)
|
202 |
|
203 |
+
base_url = f"{api_rest_base_url}/organizationalEntityFollowerStatistics"
|
204 |
+
time_intervals_value = f"(timeRange:(start:{start_ms}),timeGranularityType:MONTH)"
|
|
|
205 |
|
206 |
+
api_params = {
|
207 |
+
"q": "organizationalEntity",
|
208 |
+
"organizationalEntity": org_urn,
|
209 |
+
"timeIntervals": time_intervals_value
|
210 |
+
}
|
211 |
+
|
212 |
+
logging.info(f"Preparing to fetch monthly follower gains for {org_urn}.")
|
213 |
+
logging.debug(f"API Parameters for monthly gains: {json.dumps(api_params)}")
|
214 |
|
215 |
results = []
|
216 |
+
request_url_for_logging = "Not constructed"
|
217 |
+
response_obj = None # To store response for logging in broader exception blocks
|
218 |
+
|
219 |
try:
|
220 |
+
req = requests.Request('GET', base_url, params=api_params)
|
221 |
+
prepared_req = session.prepare_request(req)
|
222 |
+
request_url_for_logging = prepared_req.url
|
223 |
+
|
224 |
+
logging.info(f"Requesting monthly follower gains from URL: {request_url_for_logging}")
|
225 |
+
logging.debug(f"Request Headers for monthly gains: {json.dumps(dict(prepared_req.headers), indent=2)}")
|
226 |
+
|
227 |
+
response_obj = session.send(prepared_req, timeout=30) # Added timeout
|
228 |
+
response_obj.raise_for_status()
|
229 |
+
data = response_obj.json()
|
230 |
+
|
231 |
|
232 |
elements = data.get('elements', [])
|
233 |
if not elements:
|