-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathOpenbooksDownloader.py
223 lines (191 loc) · 8.16 KB
/
OpenbooksDownloader.py
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
###
#
# IMPORTS SECTION
#
###
import requests, json, textwrap, html, os, isbnlib, statistics, subprocess, time, ebooklib
from difflib import SequenceMatcher
from ebooklib import epub
from sys import platform
import xml.etree.ElementTree as ET
if platform == "win32":
executable = "openbooks.exe"
else:
executable = "./openbooks_linux"
lang = "en"
###
#
# FUNCTION DEFINITIONS SECTION
#
###
def convertToISBN13(isbn):
if(isbnlib.is_isbn10(isbn)):
return isbnlib.to_isbn13(isbn)
else:
return isbn
def similar(a, b):
return SequenceMatcher(None, a, b).ratio()
def clearScreen():
os.system('cls' if os.name == 'nt' else 'clear')
def generateOverallMatch(fuzzyMatches):
titleWeight = 1.2
authorWeight = 1
descWeight = 0.7
totalWeights = titleWeight + authorWeight + descWeight
confidence = (((fuzzyMatches[0] * titleWeight) + (fuzzyMatches[1] * authorWeight) + (fuzzyMatches[2] * descWeight)) / totalWeights)
return "{:.2%}".format(confidence)
def search(targetTitle, targetAuthors):
global executable
print("\n\nRequesting", targetTitle, "from IRCHighway.")
openbooksSearchReturn = subprocess.Popen([executable, "cli", "search", '"' + targetTitle + " " + targetAuthors + '"'], stdout=subprocess.PIPE)
return openbooksSearchReturn.stdout.read().decode().split("Results location: ",1)[1].strip()
def scrapeSearchResults(openbooksSearchFile):
validBooks = []
for line in open(openbooksSearchFile, "r").read().split("\n"):
if ".epub" in line:
validBooks.append(line)
os.remove(openbooksSearchFile)
return validBooks
def downloadEbook(downloadCommand):
global executable
openbooksDownloadReturn = subprocess.Popen([executable, "cli", "download", downloadCommand], stdout=subprocess.PIPE)
return openbooksDownloadReturn.stdout.read().decode().split("File location: ",1)[1].strip()
def fallBackToNextBook():
global epubFileName, validBooks, downloadCommand
if epubFileName.strip() != "":
os.remove(epubFileName)
validBooks.remove(downloadCommand)
if len(validBooks) == 0:
print("Could not find any valid books that matched the criteria...")
exit()
downloadCommand = min(validBooks, key=len)
clearScreen()
def checkEmbeddedLanguage(book):
if lang in book.get_metadata('DC', 'language')[0][0] or book.get_metadata('DC', 'language')[0][0].startswith(lang + "-"):
return True
else:
return False
def prepareAndRunFuzzyMatching(targetTitle, bookTitle, targetAuthors, bookAuthors, targetDesc, bookDesc):
fuzzyMatches = []
fuzzyMatches.append(similar(targetTitle, bookTitle))
fuzzyMatches.append(similar(targetAuthors, bookAuthors))
fuzzyMatches.append(similar(targetDesc, bookDesc))
return generateOverallMatch(fuzzyMatches)
def getTitle(data):
return data["volumeInfo"]["title"]
def getDesc(data):
return html.unescape(data["searchInfo"]["textSnippet"])
def getAuthors(data, separator=", "):
return separator.join(data["volumeInfo"]["authors"])
def getPageCount(data):
return data["volumeInfo"]["pageCount"]
def getLanguage(data):
return data["volumeInfo"]["language"]
def getISBN(data):
return convertToISBN13(data["volumeInfo"]["industryIdentifiers"][0]['identifier'])
def checkForSimilarISBNS(isbn):
request = requests.get("https://www.librarything.com/api/thingISBN/" + isbn)
root = ET.ElementTree(ET.fromstring(request.content.decode())).getroot()
relatedIsbns = []
for child in root:
relatedIsbns.append(child.text)
return relatedIsbns
###
#
# START OF MAIN PROGRAM
#
###
searchInput = input("Enter search: ")
targetISBN = -1
response = requests.get("https://www.googleapis.com/books/v1/volumes?q=" + searchInput)
info = json.loads(response.content.decode())
if info['totalItems'] == 0:
print("No book results with query...")
exit()
clearScreen()
for volume_info in info['items']:
try:
print("\nTitle:", getTitle(volume_info))
print("\nSummary:\n")
print(textwrap.fill(getDesc(volume_info), width=65))
print("\nAuthor(s):", getAuthors(volume_info))
print("\nPage count:", getPageCount(volume_info))
print("\nLanguage:", getLanguage(volume_info))
print("\n***")
except:
pass
else:
isCorrectBook = input("\n\nIs this your book? (y/n):")
if isCorrectBook.lower().startswith("y"):
targetISBN = getISBN(volume_info)
targetTitle = getTitle(volume_info)
targetAuthors = getAuthors(volume_info)
targetDesc = getDesc(volume_info)
break
else:
clearScreen()
if targetISBN == -1:
clearScreen()
print("No book results remaining...")
exit()
openbooksSearchFile = search(targetTitle, targetAuthors)
clearScreen()
validBooks = scrapeSearchResults(openbooksSearchFile)
if len(validBooks) == 0:
print("No results for this book that we could download...")
exit()
downloadCommand = min(validBooks, key=len)
while True:
try:
epubFileName = downloadEbook(downloadCommand)
book = epub.read_epub(epubFileName)
except:
#print("\n\nFAILED TO DOWNLOAD, FALLING BACK TO NEXT BOOK.")
fallBackToNextBook()
else:
try:
isbn = convertToISBN13(isbnlib.canonical(isbnlib.get_isbnlike(str(book.get_metadata('DC', 'identifier')))[0]))
if isbn.strip() == "":
raise
except:
#print("\n\nEXTRACTING METADATA FAILED, FALLING BACK TO NEXT BOOK.")
fallBackToNextBook()
else:
if checkEmbeddedLanguage(book):
similarISBNS = checkForSimilarISBNS(isbn)
if isbn == targetISBN or targetISBN in similarISBNS or isbnlib.to_isbn10(targetISBN) in similarISBNS:
break
else:
response = requests.get("https://www.googleapis.com/books/v1/volumes?q=isbn:" + isbn)
info = json.loads(response.content.decode())
if info['totalItems'] != 0:
volume_info = info["items"][0]
if getLanguage(volume_info) == lang:
matchPercentage = prepareAndRunFuzzyMatching(targetTitle, getTitle(volume_info), targetAuthors, getAuthors(volume_info, " "), targetDesc, getDesc(volume_info))
print("\n\n***")
print("\nTitle:", getTitle(volume_info))
print("\nSummary:\n")
print(textwrap.fill(getDesc(volume_info), width=65))
print("\nAuthor(s):", getAuthors(volume_info))
print("\nPage count:", getPageCount(volume_info))
print("\nLanguage:", getLanguage(volume_info))
print("\n***")
correctBookQuery = input("This book is only a", matchPercentage, "match, does this still seem like the correct book? (y/n)")
if correctBookQuery.lower().startswith("y"):
break
else:
fallBackToNextBook()
else:
#print("\n\nBOOK DOES NOT MEET REQUIRED LANGUAGE, FALLING BACK TO NEXT BOOK.")
fallBackToNextBook()
else:
#print("\n\nMETADATA QUERY FAILED, FALLING BACK TO NEXT BOOK.")
fallBackToNextBook()
else:
#print("\n\nBOOK DOES NOT MEET REQUIRED LANGUAGE, FALLING BACK TO NEXT BOOK.")
fallBackToNextBook()
clearScreen()
print("Book successfully downloaded!")
addToCalibre = input("\n\nAdd this book to calibre? (y/n):")
if addToCalibre.lower().startswith("y"):
subprocess.run(["calibredb", "add", epubFileName])