Skip to content

Commit

Permalink
Merge pull request #38 from theslavicbear/development
Browse files Browse the repository at this point in the history
Closes #37
  • Loading branch information
theslavicbear authored Dec 17, 2019
2 parents d0b8648 + fb1b238 commit 1fff60e
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 13 deletions.
39 changes: 26 additions & 13 deletions Ebook-Publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
import sys
from Site import *
import urllib.parse
try:
from ebooklib import epub
except:
print('Warning: No epub filetype support')
#try:
from EpubMaker import epub as epub
#from ebooklib import epub
#except:
# print('Warning: No epub filetype support')
import argparse
import os
import threading
Expand All @@ -29,11 +30,22 @@

#function for making text files
def MakeText(site):
published=open(wd+site.title+'.txt', 'w')
published.write(site.title+'\n')
published.write('by '+site.author+'\n\n')
published.write(site.story)
published.close()
if type(site) is not Nhentai.Nhentai:
published=open(wd+site.title+'.txt', 'w')
published.write(site.title+'\n')
published.write('by '+site.author+'\n\n')
published.write(site.story)
published.close()
else:
if site.hasimages == True:
if not os.path.exists(wd+site.title):
os.makedirs(wd+site.title)
i = 1
zeros = '0' * (len(str(len(site.images)))-1)
for url in site.images:
with open(wd+site.title+'/'+zeros+str(i)+'.jpg', 'wb') as myimg:
myimg.write(GetImage(url))
i=i+1

def GetImage(url):
req = urllib.request.Request(url, headers={'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)'})
Expand All @@ -45,7 +57,7 @@ def MakeEpub(site):
book=epub.EpubBook()
book.set_identifier(site.url)
titlepage=epub.EpubHtml(title='Title Page', file_name='Title.xhtml', lang='en')
titlepage.content='<h1>'+site.title+'</h1><h3>by '+site.author+'</h3><br /><a href=\'url\'>'+site.url+'<a>'
titlepage.content='<h1>'+site.title+'</h1><h3>by '+site.author+'</h3><br /><a href=\'url\'>'+site.url+'</a>'
#add summary information
try:
titlepage.content+='<br /><p>'+site.summary+'</p>'
Expand All @@ -59,7 +71,7 @@ def MakeEpub(site):
c=[]

if type(site) is not Literotica.Literotica and type(site) is not Nhentai.Nhentai:
toc=()
toc=[]
for i in range(len(site.rawstoryhtml)):
c.append(epub.EpubHtml(title=site.chapters[i], file_name='Chapter '+str(i+1)+'.xhtml', lang='en'))
if type(site) is Chyoa.Chyoa:
Expand All @@ -69,7 +81,7 @@ def MakeEpub(site):
else:
c[i].content='<h2>\n'+site.chapters[i]+'\n</h2>\n'+site.rawstoryhtml[i].prettify()
book.add_item(c[i])
toc=toc+(c[i],)
toc.append(c[i])

book.toc=toc
book.spine.append('nav')
Expand All @@ -85,13 +97,14 @@ def MakeEpub(site):
c.append(epub.EpubHtml(title=site.title, file_name='Story.xhtml', lang='en'))
c[0].content=site.storyhtml
book.add_item(c[0])
#print(site.title)
#more ebooklib space magic
book.add_item(epub.EpubNcx())
book.add_item(epub.EpubNav())
#book.spine.append('nav')
for i in c:
book.spine.append(i)
epub.write_epub(wd+site.title+'.epub', book, {})
epub.write_epub(wd+site.title+'.epub', book)

if type(site) is Chyoa.Chyoa or type(site) is Nhentai.Nhentai:
if site.hasimages == True:
Expand Down
1 change: 1 addition & 0 deletions EpubMaker/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pass
105 changes: 105 additions & 0 deletions EpubMaker/epub.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
from zipfile import ZipFile
#import datetime
version = 'Epub-Maker 0.1'
MIMETYPE = 'application/epub+zip'
container = '''<container xmlns="urn:oasis:names:tc:opendocument:xmlns:container" version="1.0">
<rootfiles>
<rootfile media-type="application/oebps-package+xml" full-path="EPUB/content.opf"/>
</rootfiles>
</container>'''

class EpubBook():

def __init__(self):
self.item_list = []
self.spine = [] #spine defines what goes in content.opf. 'nav' is the TOC page
#self.authors = []
self.toc = []
self.author = ''

def set_identifier(self, identifier):
self.identifier=identifier

def add_item(self, item):
self.item_list.append(item)

def set_title(self, title):
self.title = title

def set_language(self, lang):
self.language=lang

def add_author(self, author):
self.author = author



class EpubHtml:

def __init__(self, title='Title', file_name='file_name.xhtml', lang='lang'):
self.title = title
self.file_name = file_name
self.lang = lang
self.content=''

#These classes do nothing, but maintain compatibility with my current implementation
class EpubNcx:
def __init__(self):
pass

class EpubNav:
def __init__(self):
pass

def write_epub(title, book):
with ZipFile(title, 'w') as Zip:
opf_content = '<?xml version="1.0" encoding="utf-8"?>\n<package unique-identifier="id"\nversion="3.0" xmlns="http://www.idpf.org/2007/opf" prefix="rendition: http://www.idpf.org/vocab/rendition/#">\n <metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">\n <meta content="'+version+'" name="generator"/>\n <dc:identifier id="id">'+book.identifier+'</dc:identifier>\n <dc:title>'+book.title+'</dc:title>\n <dc:language>'+book.language+'</dc:language>\n <dc:creator id="creator">'+book.author+'</dc:creator>\n </metadata>\n <manifest>' #building the content.opf file as we iterate through items
#add default files
Zip.writestr('mimetype', MIMETYPE)
Zip.writestr('META-INF/container.xml', container)
if book.toc != []:
isTOC = True
else:
isTOC = False
#for adding nav.xhtml and toc.xhtml later
i = 0
for item in book.item_list:
if type(item) is not EpubNav and type(item) is not EpubNcx:
Zip.writestr('EPUB/'+item.file_name,'<?xml version="1.0" encoding="utf-8"?>\n <!DOCTYPE html>\n <html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" epub:prefix="z3998: http://www.daisy.org/z3998/2012/vocab/structure/#" lang="'+item.lang+'" xml:lang="'+item.lang+'">\n <head>\n <title>'+item.title+'</title>\n </head>\n <body>'+item.content+'</body>\n </html>''')
opf_content +='<item href="'+item.file_name+'" id="chapter_'+str(i)+'" media-type="application/xhtml+xml"/>\n'
i += 1
elif type(item) is EpubNcx:
opf_content +='<item href="toc.ncx" id="ncx" media-type="application/x-dtbncx+xml"/>\n'
if not isTOC:
Zip.writestr('EPUB/toc.ncx','<?xml version="1.0" encoding="utf-8"?>\n <ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1">\n <head>\n <meta content="'+book.identifier+'" name="dtb:uid"/>\n <meta content="0" name="dtb:depth"/>\n <meta content="0" name="dtb:totalPageCount"/>\n <meta content="0" name="dtb:maxPageNumber"/>\n </head>\n <docTitle>\n <text>'+book.title+'</text>\n </docTitle>\n <navMap/>\n </ncx>') #TODO needs to update for TOC
elif isTOC:
ncxstring = '<?xml version="1.0" encoding="utf-8"?>\n <ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1">\n <head>\n <meta content="'+book.identifier+'" name="dtb:uid"/>\n <meta content="0" name="dtb:depth"/>\n <meta content="0" name="dtb:totalPageCount"/>\n <meta content="0" name="dtb:maxPageNumber"/>\n </head>\n <docTitle>\n <text>'+book.title+'</text>\n </docTitle>\n<navMap>\n'
j = 1
for item in book.toc:
ncxstring += '<navPoint id="chapter_'+str(j)+'">\n<navLabel>\n<text>'+item.title+'</text>\n</navLabel>\n <content src="'+item.file_name+'" />\n</navPoint>'
ncxstring += '</navPoint>\n</navMap>\n</ncx>'
Zip.writestr('EPUB/toc.ncx', ncxstring)
elif type(item) is EpubNav:
opf_content += '<item href="nav.xhtml" id="nav" media-type="application/xhtml+xml" properties="nav"/>\n'
if not isTOC:
Zip.writestr('EPUB/nav.xhtml', '<?xml version="1.0" encoding="utf-8"?>\n <!DOCTYPE html>\n <html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" lang="en" xml:lang="en">\n <head>\n <title>'+book.title+'</title>\n </head>\n <body>\n <nav id="id" role="doc-toc" epub:type="toc">\n <h2>'+book.title+'</h2>\n <ol/>\n </nav>\n </body>\n </html>\n ')
elif isTOC:
navstring = '<?xml version="1.0" encoding="utf-8"?>\n <!DOCTYPE html>\n <html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" lang="en" xml:lang="en">\n <head>\n <title>'+book.title+'</title>\n </head>\n <body>\n <nav id="id" role="doc-toc" epub:type="toc">\n <h2>'+book.title+'</h2>\n <ol>\n'
for item in book.toc:
navstring += '<li>\n<a href="'+item.file_name+'">'+item.title+'</a>\n</li>\n'
navstring += '</ol>\n</nav>\n</body>\n</html>'
Zip.writestr('EPUB/nav.xhtml', navstring)
opf_content +='</manifest>\n<spine toc="ncx">\n'
i = 0
for item in book.spine:
if type(item) is not EpubHtml:
if item == 'nav':
opf_content += '<itemref idref="nav"/>\n'
else:
opf_content += '<itemref idref="chapter_'+str(i)+'"/>\n'
i+=1
opf_content += '</spine>\n</package>'
Zip.writestr('EPUB/content.opf', opf_content)

if __name__ == '__main__':
pass

0 comments on commit 1fff60e

Please sign in to comment.