-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspringer.py
More file actions
32 lines (24 loc) · 1012 Bytes
/
springer.py
File metadata and controls
32 lines (24 loc) · 1012 Bytes
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
import requests, html_wrapper
def query(query: str):
# // Put your API key here
api_key: str = "f8e9e20b32c954ded7e5a527003a7bfd"
# // Make a request to the Springer API
r = requests.get(f"http://api.springernature.com/meta/v2/json?q={query}&api_key={api_key}", headers={
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36"
}, timeout=5)
# // Declare an empty response string
response_str: str = ""
# // Loop through the api results
for doc in r.json()["records"]:
# // Get the data from the api response
url: str = doc["url"][0]["value"]
title: str = doc["title"]
description: str = doc["abstract"][:300] + "..."
# // Add data to html response string
response_str += html_wrapper.search_result(
title,
description,
url
)
# // Return the response string
return response_str