Skip to content

Commit

Permalink
Add loop to ADS server query
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffjennings authored Feb 12, 2025
1 parent 7b354dd commit 5ce41d1
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions repo_stats/citation_metrics.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import time
from datetime import datetime, timedelta, timezone
from urllib.parse import urlencode

Expand Down Expand Up @@ -63,21 +64,27 @@ def get_citations(self, bib, metric):
}
)

response = requests.get(
f"https://api.adsabs.harvard.edu/v1/search/query?{encoded_query}",
headers={
"Authorization": "Bearer " + self.token,
"Content-type": "application/json",
},
)
if response.status_code == 200:
result = response.json()["response"]

new_cites.extend(result["docs"])
end, start = result["numFound"], result["start"] + len(result["docs"])

else:
raise Exception(f"Query failed -- return code {response.status_code}")
query_tries = 0
while True:
response = requests.get(
f"https://api.adsabs.harvard.edu/v1/search/query?{encoded_query}",
headers={
"Authorization": "Bearer " + self.token,
"Content-type": "application/json",
},
)

if response.status_code == 200:
break
else:
if query_tries == 3:
raise Exception(f"Query failed after 3 attempts -- return code {response.status_code}")
time.sleep(300)
query_tries += 1

result = response.json()["response"]
new_cites.extend(result["docs"])
end, start = result["numFound"], result["start"] + len(result["docs"])

all_cites = update_cache(cache_file, old_cites, new_cites)

Expand Down

0 comments on commit 5ce41d1

Please sign in to comment.