Skip to content

Commit

Permalink
Optimisation for Books app
Browse files Browse the repository at this point in the history
  • Loading branch information
xyl1null committed Jul 22, 2019
1 parent d71df50 commit 002b885
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ Books/

cookies.json
*.log
.DS_Store
30 changes: 28 additions & 2 deletions safaribooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ def log(self, message):
self.logger.info(str(message)) # TODO: "utf-8", "replace"

def out(self, put):
sys.stdout.write("\r" + " " * self.columns + "\r" + str(put, "utf-8", "replace") + "\n")
# sys.stdout.write("\r" + " " * self.columns + "\r" + str(put, "utf-8", "replace") + "\n")
sys.stdout.write("\r" + " " * self.columns + "\r" + str(put) + "\n")

def info(self, message, state=False):
self.log(message)
Expand Down Expand Up @@ -249,7 +250,7 @@ class SafariBooks:
"<dc:language>en-US</dc:language>\n" \
"<dc:date>{7}</dc:date>\n" \
"<dc:identifier id=\"bookid\">{0}</dc:identifier>\n" \
"<meta name=\"cover\" content=\"{8}\"/>\n" \
"<meta name=\"cover\" content=\"cover-image\"/>\n" \
"</metadata>\n" \
"<manifest>\n" \
"<item id=\"ncx\" href=\"toc.ncx\" media-type=\"application/x-dtbncx+xml\" />\n" \
Expand Down Expand Up @@ -893,6 +894,9 @@ def create_content_opf(self):
manifest.append("<item id=\"style_{0:0>2}\" href=\"Styles/Style{0:0>2}.css\" "
"media-type=\"text/css\" />".format(i))

res = self.create_cover()
manifest.append("<item id=\"cover-image\" href=\"{0}\" media-type=\"image/png\" properties=\"cover-image\" />".format(res))

authors = "\n".join("<dc:creator opf:file-as=\"{0}\" opf:role=\"aut\">{0}</dc:creator>".format(
escape(aut["name"])
) for aut in self.book_info["authors"])
Expand Down Expand Up @@ -963,6 +967,28 @@ def create_toc(self):
navmap
)

def create_cover(self):
response = self.requests_provider(self.api_url)
parsed = response.json()
for i in parsed['chapters']:
# very vulnerable as publishers may differ
if 'cover.' or 'Cover.' or 'titlepage.' in i:
cover_url = i
break

if cover_url:
cover_response = self.requests_provider(cover_url)
cover_parsed = cover_response.json()
imgAttrib = cover_parsed['images']
if imgAttrib:
lst2str = "".join(list(map(str, imgAttrib)))
return "Images/" + lst2str.split('/')[-1]
else:
return "Images/" + self.get_default_cover()
else:
return "Images/" + self.get_default_cover()


def create_epub(self):
open(os.path.join(self.BOOK_PATH, "mimetype"), "w").write("application/epub+zip")
meta_info = os.path.join(self.BOOK_PATH, "META-INF")
Expand Down

0 comments on commit 002b885

Please sign in to comment.