83 lines
2.5 KiB
Python
83 lines
2.5 KiB
Python
import os
|
|
import requests
|
|
|
|
ELASTICSEARCH_URL = os.environ.get("ELASTICSEARCH_URL", "https://project.nes.uni-due.de/elastic")
|
|
ELASTICSEARCH_API_KEY = os.environ.get("ELASTICSEARCH_API_KEY", "am1wWXBaMEJEVWJoZkE5MnBaUlk6TWlvbW1LRlMzNjdDUHgzYmVkUmI5UQ==")
|
|
|
|
|
|
def number_of_existing_authors() -> int:
|
|
headers = {
|
|
"Authorization": f"ApiKey {ELASTICSEARCH_API_KEY}",
|
|
"Content-Type": "application/json",
|
|
}
|
|
response = requests.post(
|
|
f"{ELASTICSEARCH_URL}/openalex-authors/_count",
|
|
json={"query": {"match_all": {}}},
|
|
headers=headers,
|
|
)
|
|
response.raise_for_status()
|
|
return response.json()["count"]
|
|
|
|
def number_of_existing_authors_with_min_works(min_number_of_works: int) -> int:
|
|
headers = {
|
|
"Authorization": f"ApiKey {ELASTICSEARCH_API_KEY}",
|
|
"Content-Type": "application/json",
|
|
}
|
|
response = requests.post(
|
|
f"{ELASTICSEARCH_URL}/openalex-authors/_count",
|
|
json={"query": {"range": {"works_count": {"gte": min_number_of_works}}}},
|
|
headers=headers,
|
|
)
|
|
response.raise_for_status()
|
|
return response.json()["count"]
|
|
|
|
def get_all_author_ids_from_specific_university(university_name: str) -> list[str]:
|
|
headers = {
|
|
"Authorization": f"ApiKey {ELASTICSEARCH_API_KEY}",
|
|
"Content-Type": "application/json",
|
|
}
|
|
|
|
query = {
|
|
"nested": {
|
|
"path": "affiliations",
|
|
"query": {
|
|
"match_phrase": {
|
|
"affiliations.institution.display_name": university_name
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
author_ids = []
|
|
search_after = None
|
|
|
|
while True:
|
|
payload = {
|
|
"query": query,
|
|
"_source": ["last_known_institutions.display_name"],
|
|
"size": 10000,
|
|
"sort": [{"id": "asc"}],
|
|
}
|
|
if search_after is not None:
|
|
payload["search_after"] = search_after
|
|
|
|
response = requests.post(
|
|
f"{ELASTICSEARCH_URL}/openalex-authors/_search",
|
|
json=payload,
|
|
headers=headers,
|
|
)
|
|
response.raise_for_status()
|
|
hits = response.json()["hits"]["hits"]
|
|
if not hits:
|
|
break
|
|
|
|
for hit in hits:
|
|
last_known = hit["_source"].get("last_known_institutions") or []
|
|
last_known_names = [institution.get("display_name") for institution in last_known]
|
|
if university_name in last_known_names:
|
|
author_ids.append(hit["_id"])
|
|
|
|
search_after = hits[-1]["sort"]
|
|
|
|
return author_ids
|