-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader_profile.py
109 lines (80 loc) · 3.66 KB
/
reader_profile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#GoycwKdjBJ7oPPSnBIOV498hMHCivpiU6BjQ5YbZ
#47299473(simon) 35278460(danai) 2191982532(jeremy)
import requests
import os
API_KEY_GOOGLE_SCHOLAR = "GoycwKdjBJ7oPPSnBIOV498hMHCivpiU6BjQ5YbZ"
def retrieve_and_merge_abstracts(api_key, author_id):
# Define the Semantic Scholar API endpoint for author's detailed papers
api_url = f"https://api.semanticscholar.org/graph/v1/author/{author_id}/papers"
# Define the API parameters to specify the fields you want
params = {
"fields": "abstract",
"limit": 100 # Limit to the most recent 100 papers (adjust as needed)
}
# Set up the request headers with the API key
headers = {
"x-api-key": api_key
}
try:
# Make the API request to retrieve abstracts of the author's papers
response = requests.get(api_url, params=params, headers=headers)
if response.status_code == 200:
# Parse the API response to extract paper abstracts
data = response.json()
papers = data.get("data", [])
# Create a list to store abstracts
abstracts = []
# Save abstracts to the list
for i, paper in enumerate(papers):
abstract = paper.get("abstract")
if abstract:
abstracts.append(f"Abstract {i+1}:\n{abstract}\n\n")
# Merge abstracts into a single text file
merged_abstracts_filename = "scientist_abstracts.txt"
with open(merged_abstracts_filename, "w", encoding="utf-8") as merged_file:
merged_file.writelines(abstracts)
print("Abstracts retrieved and merged successfully.")
else:
print(f"Error: Unable to fetch papers. Status Code: {response.status_code}")
except Exception as e:
print(f"An error occurred: {str(e)}")
def fetch_abstract(author_id): #why you do this:fetch abstract from a given id
# Define the Semantic Scholar API endpoint for author's detailed papers
api_url = f"https://api.semanticscholar.org/graph/v1/author/{author_id}/papers"
# Define the API parameters to specify the fields you want
params = {
"fields": "abstract",
"limit": 100 # Limit to the most recent 100 papers (adjust as needed)
}
# Set up the request headers with the API key
headers = {
"x-api-key": API_KEY_GOOGLE_SCHOLAR
}
try:
# Make the API request to retrieve abstracts of the author's papers
response = requests.get(api_url, params=params, headers=headers)
if response.status_code == 200:
# Parse the API response to extract paper abstracts
data = response.json()
papers = data.get("data", [])
# Create a list to store abstracts
abstracts = []
# Save abstracts to the list
for i, paper in enumerate(papers):
abstract = paper.get("abstract")
if abstract:
abstracts.append(f"Abstract {i+1}:\n{abstract}\n\n")
return '\n'.join(abstracts)
else:
return f"Error: Unable to fetch papers. Status Code: {response.status_code}"
except Exception as e:
return f"An error occurred: {str(e)}"
def fetch_profiles(ids): #fetch abstracts when user selects several ids; for the ask measure
return [fetch_abstract(id) for id in ids]
def main():
# Example usage
api_key = "GoycwKdjBJ7oPPSnBIOV498hMHCivpiU6BjQ5YbZ"
author_id = "2191982532" # Replace with the actual author's ID
# Retrieve and merge abstracts for the scientist's papers
retrieve_and_merge_abstracts(api_key, author_id)
if __name__ == '__main__': main()