Advertisement
gabalese

sortncx.py (py3 + lxml)

Jul 30th, 2012
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 0 0
  1. #! /usr/bin/env python3
  2.  
  3. from lxml import etree as ET
  4. import zipfile as ZIP
  5. import sys
  6. import os
  7.  
  8. namespaces = {  "opf":"http://www.idpf.org/2007/opf",
  9.         "dc":"http://purl.org/dc/elements/1.1/",
  10.         "ncx":"http://www.daisy.org/z3986/2005/ncx/"
  11.         }
  12.  
  13. info = {}
  14.  
  15. def sortncx(file):
  16.     ncx = parseNCX(file)
  17.     navpoints = ncx.xpath(".//ncx:navPoint",namespaces=namespaces)
  18.  
  19.     counter = 1
  20.     for item in navpoints:
  21.         item.set("playOrder",str(counter))
  22.         counter += 1
  23.     else:
  24.         counter = 1
  25.  
  26.     ncx = ET.ElementTree(ncx)
  27.  
  28.     return ET.tostring(ncx)
  29.  
  30. def parseInfo(file):
  31.     global info
  32.     try:
  33.         f = ZIP.ZipFile(file).read("META-INF/container.xml")
  34.     except KeyError:
  35.         print("The %s file is not a valid OCF." % str(file))
  36.     try:
  37.         m = ET.fromstring(f)
  38.         info["path_to_opf"] = m[0][0].get("full-path")
  39.         root_folder = os.path.dirname(info["path_to_opf"])
  40.     except:
  41.         pass
  42.     opf = ET.fromstring(ZIP.ZipFile(file).read(info["path_to_opf"]))
  43.    
  44.     id = opf.xpath("//opf:spine",namespaces=namespaces)[0].get("toc")
  45.     expr = "//*[@id='%s']" % id
  46.     info["ncx_name"] = opf.xpath(expr)[0].get("href")
  47.     info["path_to_ncx"] = root_folder + "/" + info["ncx_name"]
  48.     info.pop("ncx_name")
  49.  
  50.     return info
  51.  
  52. def parseNCX(file):
  53.  
  54.     ncx = {}
  55.     ncx = ET.fromstring(ZIP.ZipFile(file).read(parseInfo(file)["path_to_ncx"]))
  56.    
  57.     return ncx
  58.  
  59. if __name__ == '__main__':
  60.  
  61.     info = parseInfo(sys.argv[1])
  62.     sorted_ncx = sortncx(sys.argv[1])
  63.     epub = ZIP.ZipFile(sys.argv[1],"a")
  64.     epub.writestr(info["path_to_ncx"],sorted_ncx)
  65.     epub.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement