Skip to content

Commit 6d14f8e

Browse files
authored
Only do 5 calls to the openalex API at a time (#36)
1 parent 49c6472 commit 6d14f8e

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/bibx/clients/openalex.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111

1212
logger = logging.getLogger(__name__)
1313

14-
MAX_WORKS_PER_PAGE = 200
15-
MAX_IDS_PER_REQUEST = 80
14+
_MAX_WORKS_PER_PAGE = 200
15+
_MAX_IDS_PER_REQUEST = 80
16+
_MAX_CONNECTIONS = 5
1617

1718

1819
class AuthorPosition(Enum):
@@ -145,17 +146,17 @@ def list_recent_articles(self, query: str, limit: int = 600) -> list[Work]:
145146
"cited_by_count:>1",
146147
]
147148
)
148-
pages = (limit // MAX_WORKS_PER_PAGE) + 1
149+
pages = (limit // _MAX_WORKS_PER_PAGE) + 1
149150
results: list[Work] = []
150-
with ThreadPoolExecutor(max_workers=min(pages, 25)) as executor:
151+
with ThreadPoolExecutor(max_workers=min(pages, _MAX_CONNECTIONS)) as executor:
151152
futures = [
152153
executor.submit(
153154
self._fetch_works,
154155
{
155156
"select": select,
156157
"filter": filter_,
157158
"sort": "publication_year:desc",
158-
"per_page": MAX_WORKS_PER_PAGE,
159+
"per_page": _MAX_WORKS_PER_PAGE,
159160
"page": page,
160161
},
161162
)
@@ -175,17 +176,17 @@ def list_articles_by_openalex_id(self, ids: list[str]) -> list[Work]:
175176
return []
176177
select = ",".join(Work.model_fields.keys())
177178
results: list[Work] = []
178-
with ThreadPoolExecutor(max_workers=5) as executor:
179+
with ThreadPoolExecutor(max_workers=_MAX_CONNECTIONS) as executor:
179180
futures = [
180181
executor.submit(
181182
self._fetch_works,
182183
{
183184
"select": select,
184185
"filter": f"ids.openalex:{'|'.join(ids)},type:types/article",
185-
"per_page": MAX_IDS_PER_REQUEST,
186+
"per_page": _MAX_IDS_PER_REQUEST,
186187
},
187188
)
188-
for ids in chunks(ids, MAX_IDS_PER_REQUEST)
189+
for ids in chunks(ids, _MAX_IDS_PER_REQUEST)
189190
]
190191
for future in as_completed(futures):
191192
work_response = future.result()

0 commit comments

Comments
 (0)