From 87084ff53079d93c5a8b2e2043ee5becc062bb1b Mon Sep 17 00:00:00 2001 From: Romain Bignon Date: Sun, 26 Jun 2022 19:37:55 +0200 Subject: [PATCH] get_company_updates and get_profile_updates: optimize use of max_results 1. do not set [] as a default parameter, as it is a global list and if the method is called twice, previous results will be included 2. extend results *before* checking if results limit is reached, it prevents useless API calls 3. return only required results --- linkedin_api/linkedin.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/linkedin_api/linkedin.py b/linkedin_api/linkedin.py index b0d0ef87..8839566d 100644 --- a/linkedin_api/linkedin.py +++ b/linkedin_api/linkedin.py @@ -724,7 +724,7 @@ def get_profile_connections(self, urn_id): return self.search_people(connection_of=urn_id, network_depth="F") def get_company_updates( - self, public_id=None, urn_id=None, max_results=None, results=[] + self, public_id=None, urn_id=None, max_results=None, results=None ): """Fetch company updates (news activity) for a given LinkedIn company. @@ -736,6 +736,9 @@ def get_company_updates( :return: List of company update objects :rtype: list """ + if results is None: + results = [] + params = { "companyUniversalName": {public_id or urn_id}, "q": "companyFeedByUniversalName", @@ -747,6 +750,7 @@ def get_company_updates( res = self._fetch(f"/feed/updates", params=params) data = res.json() + results.extend(data["elements"]) if ( len(data["elements"]) == 0 @@ -756,9 +760,8 @@ def get_company_updates( and len(results) / max_results >= Linkedin._MAX_REPEATED_REQUESTS ) ): - return results + return results[:max_results] - results.extend(data["elements"]) self.logger.debug(f"results grew: {len(results)}") return self.get_company_updates( @@ -769,7 +772,7 @@ def get_company_updates( ) def get_profile_updates( - self, public_id=None, urn_id=None, max_results=None, results=[] + self, public_id=None, urn_id=None, max_results=None, results=None ): """Fetch profile updates (newsfeed activity) for a given LinkedIn profile. @@ -781,6 +784,9 @@ def get_profile_updates( :return: List of profile update objects :rtype: list """ + if results is None: + results = [] + params = { "profileId": {public_id or urn_id}, "q": "memberShareFeed", @@ -792,6 +798,7 @@ def get_profile_updates( res = self._fetch(f"/feed/updates", params=params) data = res.json() + results.extend(data["elements"]) if ( len(data["elements"]) == 0 @@ -801,9 +808,8 @@ def get_profile_updates( and len(results) / max_results >= Linkedin._MAX_REPEATED_REQUESTS ) ): - return results + return results[:max_results] - results.extend(data["elements"]) self.logger.debug(f"results grew: {len(results)}") return self.get_profile_updates(