Repository navigation
Expand file tree
/
Copy pathLibrary.py
More file actions
198 lines (180 loc) · 6.77 KB
/
Copy pathLibrary.py
File metadata and controls
198 lines (180 loc) · 6.77 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import os
import shutil
import PyPDF2
from gtts import gTTS
from playsound import playsound
import pygame
class Node:
def __init__(self, bid, title, author, status, borrower=None):
self.bid = bid
self.title = title
self.author = author
self.status = status
self.borrower = borrower
self.next = None
class LinkedListLibrary:
def __init__(self):
self.head = None
self.bid_set = set()
self.borrowers = {}
self.pdf_folder = "pdf_books"
if not os.path.exists(self.pdf_folder):
os.makedirs(self.pdf_folder)
def AddBook(self, bid, title, author, status, path):
if bid in self.bid_set:
print(f'book with id {bid} already exists.')
else:
new_node = Node(bid, title, author, status)
if self.head is None:
self.head = new_node
else:
current = self.head
while current.next:
current = current.next
current.next = new_node
self.bid_set.add(bid)
new_node.borrower = None
file_name = f'{bid}.pdf'
new_path = os.path.join(self.pdf_folder, file_name)
shutil.move(path, new_path)
new_node.path = new_path
def display_list(self):
if self.head is None:
print("The list is empty.")
else:
current = self.head
print("Book List:")
print("{:<5} | {:<25} | {:<20} | {:<10}".format("Bid", "Title", "Author", "status (0 - available)"))
print("------------------------------------------------------------------------------------------")
while current:
print("{:<5} | {:<25} | {:<20} | {:<10}".format(current.bid, current.title, current.author, current.status))
current = current.next
def delete_book(self, bid):
if self.head is None:
print("The list is empty.")
else:
current = self.head
prev = None
found = False
while current:
if current.bid == bid:
found = True
break
prev = current
current = current.next
if found:
if prev:
prev.next = current.next
else:
self.head = current.next
print(f"Book with id {bid} has been deleted.")
else:
print(f"Book with id {bid} not found.")
def borrow_book(self, bid, borrower, borrowed_books):
if self.head is None:
print("The list is empty.")
else:
current = self.head
found = False
while current:
if current.bid == bid:
found = True
if current.status == "0":
current.status == "1"
borrowed_books.add_node(current.bid, current.title, borrower)
self.borrowers[current.bid] = borrower
print(f'Book with ID {bid} has been borrowed.')
else:
print(f"Book with ID {bid} is not available")
current = current.next
if not found:
print(f"Book with id {bid} is not found.")
def return_book(self, bid, borrowed_books):
if borrowed_books.head is None:
print("No books are currently borrowed.")
else:
current = borrowed_books.head
prev = None
found = False
while current:
if current.bid == bid:
found = True
break
prev = current
current = current.next
if found:
if prev:
prev.next = current.next
else:
borrowed_books.head = current.next
current = self.head
while current:
if current.bid == bid:
current.status = "0"
break
current = current.next
borrower = self.borrowers.get(bid) # Get borrower information from dictionary
print(f"Book with id {bid} - {current.title} has been returned by {borrower}.") # The minus sign (-) is used to create a space between two parts of a string.
else:
print(f"Book with id {bid} not found in borrowed books.")
def search_book_by_title(self, title):
current = self.head
found_books = []
while current:
if title.lower() in current.title.lower():
found_books.append(current.bid)
current = current.next
return found_books
def print_book_id(self, title):
found_books = self.search_book_by_title(title)
if found_books:
print(f"Books with title '{title}' found with the following IDs:")
for bid in found_books:
print(bid)
else:
print(f"No books found with title '{title}'.")
def read_pdf(self, file_path):
with open(file_path, 'rb') as file:
reader = PyPDF2.PdfReader(file)
num_pages = len(reader.pages)
content = ''
for page_num in range(num_pages):
page = reader.pages[page_num]
content += page.extract_text() # extract: extract Content
return content
def read_book_with_voice(self, bid):
current = self.head
found = False
while current:
if current.bid == bid:
found = True
break
current = current.next
if found:
content = self.read_pdf(current.path) # use path of book
if content:
tts = gTTS(text=content, lang='vi')
tts.save("output.mp3")
pygame.mixer.init()
pygame.mixer.music.load("output.mp3")
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
continue
pygame.mixer.quit()
os.remove("output.mp3")
else:
print(f"Book with id {bid} not found in the library.")
else:
print(f"Book with id {bid} not found in the library.")
class BorrowedLinkedList:
def __init__(self):
self.head = None
def add_node(self, bid, title, borrower):
new_node = Node(bid, title, borrower, None)
if self.head is None:
self.head = new_node
else:
current = self.head
while current.next:
current = current.next
current.next = new_node