Files
Steven Tischer 9bca00fcdb
LocationBasedServices/smartguide/pipeline/head This commit looks good
add Arman/Marcus/Pedro as test authors
2026-06-09 12:44:04 +02:00

181 lines
4.6 KiB
Python

import requests, json
import helpers
AUTHOR_OPEN_ALEXID = helpers.get_all_author_ids_from_specific_university("University of Duisburg-Essen")
#AUTHOR_OPEN_ALEXID = ["A5093075883", "A5047117780", "A5007170803"]
ANZAHL_TOP_TOPICS = 5
WORKS_PAGE_SIZE = 100
ES_HEADERS = {
"Authorization": f"ApiKey {helpers.ELASTICSEARCH_API_KEY}",
"Content-Type": "application/json",
}
WORK_SOURCE_FIELDS = [
"title",
"display_name",
"abstract_inverted_index",
"cited_by_count",
"primary_topic",
]
def write_data_to_file(author_id, data):
with open(f"data2/{author_id}.json", "w") as f:
json.dump(data, f)
def fetch_author_display_name(author_id):
payload = {
"query": {
"query_string": {
"default_field": "_id",
"query": author_id,
}
},
"_source": ["display_name"],
}
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"].get("display_name")
def fetch_author_works(author_id):
author_url = f"https://openalex.org/{author_id}"
authorship_query = {
"nested": {
"path": "authorships",
"query": {
"bool": {
"must": [{"term": {"authorships.author.id": author_url}}],
},
},
}
}
all_works = []
page = 0
while True:
payload = {
"query": authorship_query,
"_source": WORK_SOURCE_FIELDS,
"from": page * WORKS_PAGE_SIZE,
"size": WORKS_PAGE_SIZE,
}
response = requests.post(
f"{helpers.ELASTICSEARCH_URL}/openalex-works*/_search",
json=payload,
headers=ES_HEADERS,
)
response.raise_for_status()
hits = response.json()["hits"]["hits"]
if len(hits) == 0:
break
for hit in hits:
all_works.append(hit["_source"])
page += 1
return all_works
def count_primary_topics(works):
counts = {}
for work in works:
primary_topic = work.get("primary_topic")
if not primary_topic:
continue
name = primary_topic.get("display_name")
if not name:
continue
if name not in counts:
counts[name] = {
"id": primary_topic.get("id"),
"display_name": name,
"count": 0,
}
counts[name]["count"] += 1
topics_sorted = sorted(counts.values(), key=lambda topic: topic["count"], reverse=True)
return topics_sorted[:ANZAHL_TOP_TOPICS]
def reconstruct_abstract(inverted_index):
if not inverted_index:
return None
position_to_word = {}
for word, positions in inverted_index.items():
for position in positions:
position_to_word[position] = word
words_in_order = []
for position in sorted(position_to_word.keys()):
words_in_order.append(position_to_word[position])
return " ".join(words_in_order)
def build_papers(works):
papers = []
for work in works:
name = work.get("title") or work.get("display_name")
abstract = reconstruct_abstract(work.get("abstract_inverted_index"))
papers.append({
"name": name,
"abstract": abstract,
"stats": {
"cited_by_count": work.get("cited_by_count"),
},
})
return papers
def main():
if len(AUTHOR_OPEN_ALEXID) == 0:
print("Keine Autoren-IDs gefunden. Abbruch!")
raise SystemExit(1)
#for author_id in AUTHOR_OPEN_ALEXID:
for i in range(100):
author_id = AUTHOR_OPEN_ALEXID[i % len(AUTHOR_OPEN_ALEXID)]
print(f"Rufe Daten für Autor: {author_id} ab...")
display_name = fetch_author_display_name(author_id)
if display_name is None:
print(f"Keine Daten für {author_id} gefunden... überspringe.\n")
continue
works = fetch_author_works(author_id)
top_topics = count_primary_topics(works)
papers = build_papers(works)
author_data = {
"display_name": display_name,
"topics": top_topics,
"papers": papers,
}
print(f"Schreibe Daten für Autor: {display_name}...")
write_data_to_file(author_id, author_data)
print("Nächster Autor...\n")
if __name__ == "__main__":
main()