Example #1
0
def createEpub(title, output=None):
    pages = bookLinks(title, recursive=True)
    tmptree = tempfile.mkdtemp(prefix="epub.")
    base = os.path.join(tmptree, title)

    #FIXME: other css might be better suited
    url = 'http://en.wikibooks.org/skins-1.5/common/commonPrint.css'
    saveUrl(url, base + '/stylesheet.css')

    #write html pages and related images
    items = []
    for page in pages:
        items.append(savePage(page, base, bookTitle=title))
        items += saveImages(page, base)

    #write basic epub info and create epub
    content_opf, toc_ncx = epub_files(title, items)
    f = open(base + '/content.opf', 'w')
    f.write(content_opf)
    f.close()
    f = open(base + '/toc.ncx', 'w')
    f.write(toc_ncx)
    f.close()
    container(base)
    tmp_output = os.path.join(tmptree, '%s.epub' % title)
    cmd = u'cd "%s";zip -q0X  "%s" mimetype;zip -qXr9D  "%s" *' % (
        base, tmp_output, tmp_output)
    os.system(cmd.encode('utf-8'))
    if output:
        shutil.copyfile(tmp_output, output)
        shutil.rmtree(tmptree)
Example #2
0
def createEpub(title, output=None):
    pages = bookLinks(title, recursive=True)
    tmptree = tempfile.mkdtemp(prefix="epub.")
    base = os.path.join(tmptree, title)

    #FIXME: other css might be better suited
    url = 'http://en.wikibooks.org/skins-1.5/common/commonPrint.css'
    saveUrl(url, base+'/stylesheet.css')

    #write html pages and related images
    items = []
    for page in pages:
        items.append(savePage(page, base, bookTitle=title))
        items += saveImages(page, base)

    #write basic epub info and create epub
    content_opf, toc_ncx = epub_files(title, items)
    f = open(base+'/content.opf', 'w')
    f.write(content_opf)
    f.close()
    f = open(base+'/toc.ncx', 'w')
    f.write(toc_ncx)
    f.close()
    container(base)
    tmp_output=os.path.join(tmptree, '%s.epub'%title)
    cmd = u'cd "%s";zip -q0X  "%s" mimetype;zip -qXr9D  "%s" *'%(base, tmp_output, tmp_output)
    os.system(cmd.encode('utf-8'))
    if output:
        shutil.copyfile(tmp_output, output)
        shutil.rmtree(tmptree)
Example #3
0
def saveImages(title, base='/tmp/book'):
    #use cache for now
    #from oxlib.net import saveUrl
    images = pageImages(title)
    _images = []
    for image in images:
        f = os.path.join(base, unquote(localImageLink(image).replace('http:__', '')))
        _images.append(f.replace(base+'/', ''))
        if not os.path.exists(f.encode('utf-8')):
            saveUrl(image, f)
    return _images
Example #4
0
def saveImages(title, base='/tmp/book'):
    #use cache for now
    #from oxlib.net import saveUrl
    images = pageImages(title)
    _images = []
    for image in images:
        f = os.path.join(base,
                         unquote(localImageLink(image).replace('http:__', '')))
        _images.append(f.replace(base + '/', ''))
        if not os.path.exists(f.encode('utf-8')):
            saveUrl(image, f)
    return _images