This commit is contained in:
@@ -1,26 +1,183 @@
|
||||
import requests, json
|
||||
import helpers
|
||||
|
||||
AUTHOR_OPEN_ALEXID = helpers.get_all_author_ids_from_specific_university("University of Duisburg-Essen")
|
||||
#AUTHOR_OPEN_ALEXID = helpers.get_all_author_ids_from_specific_university("University of Duisburg-Essen")
|
||||
AUTHOR_OPEN_ALEXID = ["A5007170803"]
|
||||
INCLUDE_WORKS_METRICS = True
|
||||
PREPARE_DATA = False
|
||||
|
||||
ES_HEADERS = {
|
||||
"Authorization": f"ApiKey {helpers.ELASTICSEARCH_API_KEY}",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
||||
AUTHOR_SOURCE_FIELDS = [
|
||||
"display_name",
|
||||
"last_known_institutions",
|
||||
"counts_by_year",
|
||||
"summary_stats",
|
||||
"works_count",
|
||||
"cited_by_count",
|
||||
"topics",
|
||||
"affiliations",
|
||||
"x_concepts",
|
||||
]
|
||||
|
||||
def write_data_to_file(author_id, data):
|
||||
with open(f"data/{author_id}.json", "w") as f:
|
||||
json.dump(data, f)
|
||||
|
||||
def fetch_author_data(author_id):
|
||||
payload = {
|
||||
"query": {
|
||||
"query_string": {
|
||||
"default_field": "_id",
|
||||
"query": author_id,
|
||||
}
|
||||
},
|
||||
"_source": AUTHOR_SOURCE_FIELDS,
|
||||
}
|
||||
response = requests.post(
|
||||
f"{helpers.ELASTICSEARCH_URL}/openalex-authors/_search",
|
||||
json=payload,
|
||||
headers=ES_HEADERS,
|
||||
)
|
||||
response.raise_for_status()
|
||||
|
||||
hits = response.json()["hits"]["hits"]
|
||||
if len(hits) == 0:
|
||||
return None
|
||||
return hits[0]["_source"]
|
||||
|
||||
def get_works_metrics(author_id):
|
||||
author_url = f"https://openalex.org/{author_id}"
|
||||
|
||||
def authorship_query(extra_conditions):
|
||||
conditions = [{"term": {"authorships.author.id": author_url}}]
|
||||
conditions.extend(extra_conditions)
|
||||
return {
|
||||
"nested": {
|
||||
"path": "authorships",
|
||||
"query": {"bool": {"must": conditions}},
|
||||
}
|
||||
}
|
||||
|
||||
def count_works(query):
|
||||
response = requests.post(
|
||||
f"{helpers.ELASTICSEARCH_URL}/openalex-works*/_count",
|
||||
json={"query": query},
|
||||
headers=ES_HEADERS,
|
||||
)
|
||||
response.raise_for_status()
|
||||
return response.json()["count"]
|
||||
|
||||
total = count_works(authorship_query([]))
|
||||
if total == 0:
|
||||
return None
|
||||
|
||||
first_author = count_works(
|
||||
authorship_query([{"term": {"authorships.author_position": "first"}}])
|
||||
)
|
||||
corresponding = count_works(
|
||||
authorship_query([{"term": {"authorships.is_corresponding": True}}])
|
||||
)
|
||||
|
||||
response = requests.post(
|
||||
f"{helpers.ELASTICSEARCH_URL}/openalex-works*/_search",
|
||||
json={
|
||||
"query": authorship_query([]),
|
||||
"_source": ["title", "publication_year", "cited_by_count"],
|
||||
"size": 1,
|
||||
"sort": [{"cited_by_count": {"order": "desc", "missing": "_last"}}],
|
||||
},
|
||||
headers=ES_HEADERS,
|
||||
)
|
||||
response.raise_for_status()
|
||||
hits = response.json()["hits"]["hits"]
|
||||
most_cited_work = None
|
||||
if hits:
|
||||
most_cited_work = hits[0]["_source"]
|
||||
|
||||
return {
|
||||
"works_total": total,
|
||||
"first_author_share": round(first_author / total, 2),
|
||||
"corresponding_author_share": round(corresponding / total, 2),
|
||||
"most_cited_work": most_cited_work,
|
||||
}
|
||||
|
||||
def prepare_author_data(raw):
|
||||
counts_by_year = raw.get("counts_by_year") or []
|
||||
|
||||
years = sorted(entry["year"] for entry in counts_by_year)
|
||||
career = None
|
||||
if years:
|
||||
career = {"first_year": years[0], "last_year": years[-1]}
|
||||
|
||||
topics = sorted(raw.get("topics") or [], key=lambda t: t.get("count", 0), reverse=True)
|
||||
top_topics = []
|
||||
for topic in topics[:4]:
|
||||
top_topics.append({"display_name": topic.get("display_name"), "count": topic.get("count")})
|
||||
|
||||
affiliations = []
|
||||
for affiliation in raw.get("affiliations") or []:
|
||||
institution = affiliation.get("institution") or {}
|
||||
affiliations.append({
|
||||
"display_name": institution.get("display_name"),
|
||||
"type": institution.get("type"),
|
||||
})
|
||||
|
||||
last_known = []
|
||||
for institution in raw.get("last_known_institutions") or []:
|
||||
last_known.append(institution.get("display_name"))
|
||||
|
||||
x_concepts = raw.get("x_concepts") or []
|
||||
domain = None
|
||||
if x_concepts:
|
||||
top_concept = max(x_concepts, key=lambda concept: concept.get("score", 0))
|
||||
domain = top_concept.get("display_name")
|
||||
|
||||
summary = raw.get("summary_stats") or {}
|
||||
last_five_years = sorted(counts_by_year, key=lambda entry: entry["year"], reverse=True)[:5]
|
||||
|
||||
return {
|
||||
"display_name": raw.get("display_name"),
|
||||
"last_known_institutions": last_known,
|
||||
"career": career,
|
||||
"summary_stats": {
|
||||
"h_index": summary.get("h_index"),
|
||||
"i10_index": summary.get("i10_index"),
|
||||
"2yr_mean_citedness": summary.get("2yr_mean_citedness"),
|
||||
},
|
||||
"works_count": raw.get("works_count"),
|
||||
"cited_by_count": raw.get("cited_by_count"),
|
||||
"topics": top_topics,
|
||||
"affiliations": affiliations,
|
||||
"domain": domain,
|
||||
"counts_by_year_last_5": last_five_years,
|
||||
}
|
||||
|
||||
def main():
|
||||
if len(AUTHOR_OPEN_ALEXID) == 0:
|
||||
print("Keine Autoren-IDs gefunden. Abbruch!")
|
||||
raise SystemExit(1)
|
||||
|
||||
|
||||
for i in range(100):
|
||||
author_id = AUTHOR_OPEN_ALEXID[i]
|
||||
print(f"Rufe Daten für Autor: {author_id} ab...")
|
||||
data_URL = f"http://localhost:8000/api/inse-service/persons/search?author_id={author_id}&include=topics_full,topic_share,summary_stats,ids,last_known_institutions,sources,counts_by_year,works_api_url,dates,cited_by_count_boost"
|
||||
author_data = requests.get(data_URL).json()
|
||||
print(f"Schreibe Daten für Autor: {author_data['results'][0]['display_name']}...")
|
||||
author_data = fetch_author_data(author_id)
|
||||
if author_data is None:
|
||||
print(f"Keine Daten für {author_id} gefunden... überspringe.\n")
|
||||
continue
|
||||
|
||||
if PREPARE_DATA:
|
||||
author_data = prepare_author_data(author_data)
|
||||
if INCLUDE_WORKS_METRICS:
|
||||
author_data["works_metrics"] = get_works_metrics(author_id)
|
||||
|
||||
print(f"Schreibe Daten für Autor: {author_data['display_name']}...")
|
||||
write_data_to_file(author_id, author_data)
|
||||
print("Nächster Autor...\n")
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user