forked from COMP491-Team-Education/Demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_online.py
52 lines (46 loc) · 1.77 KB
/
plugin_online.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
import requests
def get_books_info(partial_title, limit=10):
base_url = "https://openlibrary.org/search.json"
params = {
"title": partial_title,
"fields": "key,title,author_name,cover_i",
"limit": limit
}
response = requests.get(base_url, params=params)
if response.status_code == 200:
data = response.json()
if data["numFound"] > 0:
books = []
for book in data["docs"]:
info = {
"title": book.get("title", "N/A"),
"author": book.get("author_name", ["N/A"])[0] if book.get("author_name") else "N/A",
"cover": f"https://covers.openlibrary.org/b/id/{book['cover_i']}-M.jpg" if book.get("cover_i") else None
}
books.append(info)
return books
else:
return "No books found with that title."
else:
return f"Error: {response.status_code}"
def get_books_by_author(author_name, limit=10):
base_url = "https://openlibrary.org/search.json"
params = {
"author": author_name,
"fields": "key,title,author_name,cover_i",
"limit": limit
}
response = requests.get(base_url, params=params)
if response.status_code == 200:
data = response.json()
books = []
for book in data["docs"]:
book_info = {
"title": book.get("title", "N/A"),
"author": book.get("author_name", ["N/A"])[0] if book.get("author_name") else "N/A",
"cover": f"http://covers.openlibrary.org/b/id/{book['cover_i']}-M.jpg" if book.get("cover_i") else None
}
books.append(book_info)
return books
else:
return f"Error: {response.status_code}"