mirror of
https://github.com/serialexperiments0815/OpenLibFetch.git
synced 2026-07-12 15:32:24 +00:00
67 lines
No EOL
2.5 KiB
Python
67 lines
No EOL
2.5 KiB
Python
import requests as rq
|
|
|
|
# Request_api class made for import in the Library.py file. Seperate for better overview.
|
|
# Class intializes five parameters first to then have them assigned by requests get method.
|
|
# Requests are made to openlibrary, because they support api calls.
|
|
|
|
class request_api():
|
|
|
|
def __init__(self):
|
|
self.Booktitle = None
|
|
self.Bookauthor = None
|
|
self.Bookisbn = None
|
|
self.Bookdate = None
|
|
self.Booktext = None
|
|
|
|
def search(self, input):
|
|
filtered_input = ''
|
|
for x in input:
|
|
|
|
if x == ' ':
|
|
x = '+'
|
|
filtered_input += x
|
|
|
|
Book = rq.get(f"https://openlibrary.org/search.json?q={filtered_input}&fields=key,title,author_name,editions,isbn,number_of_pages_median,publish_date,availability&limit=1")
|
|
Booktext = Book.json()
|
|
Bookinfo = Booktext['docs'][0]
|
|
|
|
self.Booktitle = Bookinfo.get('title')
|
|
self.Bookauthor = Bookinfo.get('author_name')
|
|
self.Bookisbn = Bookinfo.get('isbn')
|
|
self.Bookdate = Bookinfo.get('publish_date')
|
|
|
|
# Return methods implemented for standardized retrieval of selected book properties.
|
|
# Also for easy implementation of the next and previous book buttons while searching.
|
|
def return_title(self, x):
|
|
return self.Booktitle[x]
|
|
|
|
def return_author(self, x):
|
|
return self.Bookauthor[x]
|
|
|
|
# If x (place in gotten book que) is None (after search) return entire list.
|
|
# If IndexError (clicked next past que list) then return to first (zero) book entry in list.
|
|
def return_isbn(self, x = None):
|
|
try:
|
|
if x != None:
|
|
return self.Bookisbn[x]
|
|
if x == None:
|
|
return self.Bookisbn
|
|
except IndexError:
|
|
return self.Bookisbn[0]
|
|
|
|
def return_date(self, x):
|
|
return self.Bookdate[x]
|
|
|
|
# If "Imageisbn = None" (after search) - set imageisbn to bookisbn of 0
|
|
def out_image(self, imageisbn=None):
|
|
if imageisbn is None:
|
|
imageisbn = self.Bookisbn[0]
|
|
image_output = f'https://covers.openlibrary.org/b/isbn/{imageisbn}-L.jpg'
|
|
return image_output
|
|
|
|
# Small-Image functionality - not yet implemented.
|
|
def out_image_small(self, imageisbn=None):
|
|
if imageisbn is None:
|
|
imageisbn = self.Bookisbn[0]
|
|
image_output = f'https://covers.openlibrary.org/b/isbn/{imageisbn}-M.jpg'
|
|
return image_output |