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

Possible Solution to Limit Semantic Scholar Usage #76

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
19 changes: 19 additions & 0 deletions richcontext/scholapi/scholapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,22 @@ class _ScholInfra_SemanticScholar (_ScholInfra):
"""
http://api.semanticscholar.org/
"""
def __init__ (self, parent=None, name="Generic", api_url=None, cgi_url=None):
super(_ScholInfra_SemanticScholar, self).__init__( parent, name, api_url, cgi_url )
self.time = 0
self.tries = 0
self.MAX_QUERIES = 100
self.MAX_ELAPSED_TIME = 5 * 60

def check_limit(self, t):
if self.tries > 100 and (t - self.time) <= self.MAX_ELAPSED_TIME:
print("We are sleeping for {}} secs".format( (t - self.time)))
time.sleep( (t - self.time))
elif self.tries <= 100:
self.tries += 1
elif (t - self.time) > self.MAX_ELAPSED_TIME:
self.time = time.time()
self.tries = 0

def publication_lookup (self, identifier):
"""
Expand All @@ -369,6 +385,9 @@ def publication_lookup (self, identifier):
message = None

t0 = time.time()

self.check_limit(t0)

url = self._get_api_url(identifier)
meta = json.loads(requests.get(url).text)

Expand Down