Skip to content

Instantly share code, notes, and snippets.

@digglife
Created September 30, 2015 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save digglife/b7a5b29652dfd8061e9b to your computer and use it in GitHub Desktop.
Save digglife/b7a5b29652dfd8061e9b to your computer and use it in GitHub Desktop.
Backup EPUB files in iBooks Library
#!/usr/bin/python
import os
import sys
import zipfile
import getopt
from biplist import readPlist
book_path = os.path.join(os.path.expanduser("~"),
"Library/Containers/com.apple.BKAgentService/Data/Documents/iBooks/Books/")
export_path = os.getcwd()
try:
opts, args = getopt.getopt(sys.argv[1:], "i:o:")
except getopt.GetoptError as e:
print str(e)
sys.exit(2)
for opt, arg in opts:
if opt == '-i':
book_path = arg
elif opt == '-o':
export_path = arg
if ( not os.path.isdir(book_path) or not os.path.isdir(export_path)):
sys.exit(1)
epub_dir = [ os.path.join(book_path, i) for i in os.walk(book_path).next()[1] ]
for d in epub_dir:
bp = readPlist(os.path.join(d, 'iTunesMetadata.plist'))
epub_zip_name = "%s.epub" % bp['itemName']
print "processing {0}".format(epub_zip_name.encode('utf8'))
zipf = zipfile.ZipFile(os.path.join(export_path, epub_zip_name), 'w')
for root, dirs, files in os.walk(d):
for f in files:
filename = os.path.join(root, f)
# relative file path for zip archive.
if root == d:
arcname = f
else:
arcname = os.path.relpath(filename, d)
zipf.write(filename, arcname)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment