-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
95 lines (79 loc) · 3.05 KB
/
main.py
File metadata and controls
95 lines (79 loc) · 3.05 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
#!/usr/bin/python3
import ui
import os, re, sys, requests
home = os.path.expanduser('~')
def updater():
print("Checking for updates...")
ver = float(open(f"{home}/BookTUI/.version", "r").read().strip().replace("\n", ""))
response= requests.get('https://raw.githubusercontent.com/UnityTheCoder/BookTUI/main/.version')
crv = float(response.text.strip().replace("\n", ""))
if ver != crv:
os.system(f"rm -rf {home}/BookTUI; git clone https://github.com/UnityTheCoder/BookTUI.git {home}/BookTUI; chmod +x /usr/bin/booktui")
print("Updated!")
exit()
print("Updates not found!")
def retrieveTitle(path):
result = os.popen(f"pdfinfo {path} 2>/dev/null | grep Title: | sed 's/Title:[ ]*//'")
return result.read().replace(r"\n", "\n")
def retrieveETitle(path):
result = os.popen(f"epub2txt --meta {path} 2>/dev/null | grep Title: | sed 's/Title:[ ]*//'")
return result.read().replace(r"\n", "\n")
def retrieveEContent(path):
result = os.popen(f"epub2txt {path}")
return result.read().replace(r"\n", "\n")
def splitIntoCharpsE(textx):
charps = []
text = ""
for line in textx.strip().split("\n"):
text += line + r"\n"
if r"[0m" in text:
#[1mLadle Rat Rotten Hut [0m
charps.append(text.replace("[1m", "").replace("[0m", "").replace("[3m", ""))
text = ""
return charps
def retrieveContent(path):
result = os.popen(f"pdftotext {path} -")
return result.read().replace(r"\n", "\n")
def splitIntoCharps(text):
charps = []
charpsz = []
charpsz = text.split("\n\n")
for ch in charpsz:
if ch == '':
pass
elif ch == '•':
pass
elif "page" in ch.lower():
pass
elif ch in charps:
pass
elif len(ch) < 4:
pass
else:
charps.append(ch)
return charps
if __name__ == "__main__":
if len(sys.argv) == 1:
print("Usage: booktui [OPTIONAL: update] file.pdf [OPTIONAL: page_number]\nKEYS: \n -> = Next page\n <- = Previous page\n Q = exit\n :search = Search a word in text\n N = next found word")
sys.exit(1)
else:
file = sys.argv[1]
page = 0
if len(sys.argv) == 3:
page = int(sys.argv[2])
else:
if os.path.exists(f"{home}/.config/booktui/{file.replace('.pdf', '.conf').replace('.epub','.conf')}"):
page = int(open(f"{home}/.config/booktui/{file.replace('.pdf', '.conf').replace('.epub','.conf')}", "r").read()) + 1
if sys.argv[1] == "update":
updater()
exit()
if file.endswith(".pdf"):
title = retrieveTitle(file)
content = retrieveContent(file)
book = splitIntoCharps(content)
ui.reader(book, title, file, page)
elif file.endswith(".epub"):
title = retrieveETitle(file)
content = retrieveEContent(file)
book = splitIntoCharpsE(content)
ui.reader(book, title, file, page)