From 5ce41d1b8df108c6000bdf36688e469fb7d7e5c8 Mon Sep 17 00:00:00 2001 From: Jeff Jennings Date: Tue, 11 Feb 2025 21:22:34 -0500 Subject: [PATCH] Add loop to ADS server query --- repo_stats/citation_metrics.py | 37 ++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/repo_stats/citation_metrics.py b/repo_stats/citation_metrics.py index 0306f31..3119235 100644 --- a/repo_stats/citation_metrics.py +++ b/repo_stats/citation_metrics.py @@ -1,4 +1,5 @@ import os +import time from datetime import datetime, timedelta, timezone from urllib.parse import urlencode @@ -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)