Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_company_updates and get_profile_updates: optimize use of max_results #227

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions linkedin_api/linkedin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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",
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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.

Expand All @@ -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",
Expand All @@ -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
Expand All @@ -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(
Expand Down