-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.py
More file actions
183 lines (131 loc) · 4.56 KB
/
Copy pathreader.py
File metadata and controls
183 lines (131 loc) · 4.56 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
import sys
import os
import curses
import threading
import time
from pypdf import PdfReader
PDF_DIR = os.path.join("downloads", "pdf")
BOOKS_DIR = os.path.join("books")
STATE_FILE = os.path.join("books", "reader_state.txt")
# ================= DIRECTORIES =================
def ensure_directories():
os.makedirs(PDF_DIR, exist_ok=True)
os.makedirs(BOOKS_DIR, exist_ok=True)
# ================= SPINNER =================
def spinner(stop_event):
frames = ["/", "-", "\\", "|"]
i = 0
while not stop_event.is_set():
frame = frames[i % len(frames)]
sys.stdout.write(f"\rimporting pdf {frame}")
sys.stdout.flush()
time.sleep(0.1)
i += 1
sys.stdout.write("\r" + " " * 30 + "\r")
sys.stdout.flush()
# ================= STATE =================
def load_state():
if not os.path.exists(STATE_FILE):
return {}
state = {}
with open(STATE_FILE, "r", encoding="utf-8") as f:
for line in f:
if "=" in line:
k, v = line.strip().split("=", 1)
state[k] = v
return state
def save_state(state):
with open(STATE_FILE, "w", encoding="utf-8") as f:
for k, v in state.items():
f.write(f"{k}={v}\n")
# ================= PDF SELECTION =================
def select_pdf_from_directory():
pdf_files = [f for f in os.listdir(PDF_DIR) if f.lower().endswith(".pdf")]
if not pdf_files:
print(f"No PDF files found in {PDF_DIR}")
sys.exit(1)
print(f"\nAvailable PDFs in {PDF_DIR}:\n")
for idx, filename in enumerate(pdf_files, 1):
print(f"{idx}. {filename}")
while True:
choice = input("\nSelect PDF number: ").strip()
if choice.isdigit():
index = int(choice) - 1
if 0 <= index < len(pdf_files):
return os.path.join(PDF_DIR, pdf_files[index])
print("Invalid selection.")
# ================= CACHE LOADING =================
def cache_path_for(pdf_path):
name = os.path.splitext(os.path.basename(pdf_path))[0]
return os.path.join(BOOKS_DIR, f"{name}.txt")
def load_from_cache(cache_path):
pages = []
with open(cache_path, "r", encoding="utf-8") as f:
content = f.read()
pages = content.split("\n===PAGE_BREAK===\n")
return pages
def save_to_cache(cache_path, pages):
with open(cache_path, "w", encoding="utf-8") as f:
f.write("\n===PAGE_BREAK===\n".join(pages))
def extract_pdf_text(pdf_path):
reader = PdfReader(pdf_path)
pages = []
for page in reader.pages:
text = page.extract_text()
pages.append(text if text else "[No extractable text]")
return pages
def load_pdf_text(pdf_path):
cache_path = cache_path_for(pdf_path)
if os.path.exists(cache_path):
return load_from_cache(cache_path)
stop_event = threading.Event()
spin_thread = threading.Thread(target=spinner, args=(stop_event,))
spin_thread.start()
try:
pages = extract_pdf_text(pdf_path)
save_to_cache(cache_path, pages)
finally:
stop_event.set()
spin_thread.join()
return pages
# ================= DISPLAY =================
def display_page(stdscr, text, page_num, total_pages):
stdscr.clear()
height, width = stdscr.getmaxyx()
header = f"Page {page_num + 1}/{total_pages} | n: next p: prev q: quit"
stdscr.addstr(0, 0, header[:width - 1])
lines = text.split("\n")
for i, line in enumerate(lines):
if i + 2 >= height:
break
stdscr.addstr(i + 2, 0, line[:width - 1])
stdscr.refresh()
# ================= MAIN =================
def main(stdscr, pdf_path):
curses.curs_set(0)
state = load_state()
book_key = os.path.basename(pdf_path)
pages = load_pdf_text(pdf_path)
total_pages = len(pages)
# Restore last page if exists
current_page = int(state.get(book_key, 0))
current_page = min(current_page, total_pages - 1)
while True:
display_page(stdscr, pages[current_page], current_page, total_pages)
key = stdscr.getch()
if key == ord('n') and current_page < total_pages - 1:
current_page += 1
elif key == ord('p') and current_page > 0:
current_page -= 1
elif key == ord('q'):
state[book_key] = str(current_page)
save_state(state)
break
# ================= ENTRY =================
if __name__ == "__main__":
ensure_directories()
if len(sys.argv) == 2:
pdf_path = sys.argv[1]
else:
pdf_path = select_pdf_from_directory()
curses.wrapper(main, pdf_path)