mirror of
https://github.com/serialexperiments0815/OpenLibFetch.git
synced 2026-07-12 15:32:24 +00:00
Add files via upload
This commit is contained in:
commit
3d875f9b33
3 changed files with 205 additions and 0 deletions
126
Library.py
Normal file
126
Library.py
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
import tkinter as tk
|
||||
from tkinter import ttk
|
||||
from buttonfade import buttonfade
|
||||
#from Requests import search, image
|
||||
from request_api import request_api
|
||||
import requests
|
||||
from io import BytesIO
|
||||
from PIL import Image, ImageTk
|
||||
|
||||
rq = request_api()
|
||||
|
||||
class BookSelection():
|
||||
|
||||
def __init__(self):
|
||||
self.x = 0
|
||||
#self.imageisbn = None
|
||||
|
||||
def on_searchbutton(self):
|
||||
self.x = 0
|
||||
searchbar_input = searchbar.get()
|
||||
rq.search(searchbar_input)
|
||||
self.image_frame()
|
||||
self.text_configuration()
|
||||
for number in range(len(rq.return_isbn())):
|
||||
tk.Label(frame_two, text=f"{rq.return_isbn(number)}").grid(row=number, column=0, pady=10, padx=20, sticky="ew")
|
||||
|
||||
def text_configuration(self):
|
||||
try:
|
||||
resulttext.config(text=(
|
||||
f"{rq.Booktitle}" + "\n"
|
||||
+ f"{rq.Bookauthor}" + "\n"
|
||||
+ f"{rq.Bookisbn[self.x]}" + "\n"
|
||||
+ f"{rq.Bookdate[self.x]}" + "\n"
|
||||
)
|
||||
)
|
||||
except UnboundLocalError:
|
||||
self.x = len(rq.return_isbn())
|
||||
imageisbn = (rq.return_isbn(self.x))
|
||||
print(imageisbn)
|
||||
self.text_configuration()
|
||||
self.image_frame(imageisbn)
|
||||
|
||||
except IndexError:
|
||||
self.x = 0
|
||||
imageisbn = (rq.return_isbn(self.x))
|
||||
self.text_configuration()
|
||||
self.image_frame(imageisbn)
|
||||
|
||||
def image_frame(self, imageisbn=None):
|
||||
global photo
|
||||
imager = rq.out_image(imageisbn)
|
||||
response = requests.get(imager)
|
||||
img_data = BytesIO(response.content)
|
||||
|
||||
photo = Image.open(img_data)
|
||||
|
||||
img = ImageTk.PhotoImage(photo)
|
||||
|
||||
imagelabel.config(image=img)
|
||||
|
||||
imagelabel.image = img
|
||||
|
||||
def on_leftright_button(self, direction):
|
||||
internal_direction = direction
|
||||
if internal_direction == "left":
|
||||
self.x += 1
|
||||
imageisbn = (rq.return_isbn(self.x))
|
||||
else:
|
||||
self.x -= 1
|
||||
imageisbn = (rq.return_isbn(self.x))
|
||||
self.image_frame(imageisbn)
|
||||
self.text_configuration()
|
||||
|
||||
if __name__ == "__main__":
|
||||
BS = BookSelection()
|
||||
|
||||
|
||||
main = tk.Tk()
|
||||
main.geometry("600x600")
|
||||
main.minsize(600, 800)
|
||||
main.maxsize(600, 800)
|
||||
main.title("Book Finder")
|
||||
|
||||
main.rowconfigure(0, weight=1, minsize=50)
|
||||
main.rowconfigure(1, weight=1, minsize=660)
|
||||
main.rowconfigure(2, weight=1, minsize=90)
|
||||
|
||||
main.columnconfigure(0, weight=1, minsize=200)
|
||||
main.columnconfigure(1, weight=1, minsize=200)
|
||||
main.columnconfigure(2, weight=1, minsize=200)
|
||||
|
||||
|
||||
searchbar = buttonfade(main, placeholder="Search here", justify="center")
|
||||
searchbar.grid(row=0, column=0, columnspan=3, sticky="nesw")
|
||||
|
||||
resulttext = tk.Label(main, text="", wraplength=600)
|
||||
resulttext.grid(row=2, column=0, columnspan=2, sticky="nesw")
|
||||
|
||||
imagelabel = tk.Label(main)
|
||||
imagelabel.grid(row=1, column=0, columnspan=2, sticky="nesw")
|
||||
|
||||
|
||||
searchbutton = tk.Button(main, text="Search", command=BS.on_searchbutton)
|
||||
searchbutton.grid(row=2, column=2, sticky="nsew", pady=(40,0))
|
||||
buttonleft = tk.Button(main, text="forward", command=lambda: BS.on_leftright_button("left"))
|
||||
buttonleft.grid(row=2, column=2, sticky="nsew", padx=(100, 0), pady=(0,50))
|
||||
buttonright = tk.Button(main, text="back", command=lambda: BS.on_leftright_button("right"))
|
||||
buttonright.grid(row=2, column=2, sticky="nswe", padx=(0, 100), pady=(0,50))
|
||||
|
||||
|
||||
|
||||
canvas = tk.Canvas(main)
|
||||
canvas.grid(row=1, column=2, sticky="nsew")
|
||||
|
||||
#scrollbar = tk.Scrollbar(main, orient=tk.VERTICAL, command=canvas.yview)
|
||||
#scrollbar.grid(row=1, column=2, sticky="nsw")
|
||||
#
|
||||
#canvas.configure(yscrollcommand=scrollbar.set)
|
||||
#canvas.bind('<Configure>', lambda e: canvas.configure(scrollregion = canvas.bbox("all")))
|
||||
#
|
||||
frame_two = tk.Frame(canvas)
|
||||
frame_two.grid(sticky="nsew")
|
||||
|
||||
canvas.create_window((0, 0), window=frame_two, anchor="nw")
|
||||
|
||||
main.mainloop()
|
||||
20
buttonfade.py
Normal file
20
buttonfade.py
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import tkinter as tk
|
||||
|
||||
class buttonfade(tk.Entry):
|
||||
def __init__(self, master=None, placeholder="Search Here", *args, **kwargs):
|
||||
super().__init__(master, *args, **kwargs)
|
||||
self.placeholder = placeholder
|
||||
self.insert(0, self.placeholder)
|
||||
self.config(fg="gray")
|
||||
self.bind('<FocusIn>', self.on_focus_in)
|
||||
self.bind('<FocusOut>', self.on_focus_out)
|
||||
|
||||
def on_focus_in(self, event):
|
||||
if self.get() == self.placeholder:
|
||||
self.delete(0, tk.END)
|
||||
self.config(fg="black")
|
||||
|
||||
def on_focus_out(self, event):
|
||||
if self.get() == "":
|
||||
self.insert(0, self.placeholder)
|
||||
self.config(fg="gray")
|
||||
59
request_api.py
Normal file
59
request_api.py
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
import requests as rq
|
||||
|
||||
|
||||
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')
|
||||
|
||||
|
||||
def return_title(self, x):
|
||||
return self.Booktitle[x]
|
||||
|
||||
def return_author(self, x):
|
||||
return self.Bookauthor[x]
|
||||
|
||||
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]
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
Loading…
Reference in a new issue