Last active
April 11, 2016 00:35
-
-
Save digglife/131f0570ce8485d9d1fe to your computer and use it in GitHub Desktop.
fix language meta of chinese epubs in iBooks Library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from biplist import readPlist,writePlist | |
import re | |
import os.path | |
import xml.etree.ElementTree as et | |
def change_lang_zh(opf): | |
opftree = et.parse(opf) | |
opfroot = opftree.getroot() | |
# dc namespace | |
title_tag = '{http://purl.org/dc/elements/1.1/}title' | |
lang_tag = '{http://purl.org/dc/elements/1.1/}language' | |
epub_title = opfroot[0].find(title_tag) | |
epub_lang = opfroot[0].find(lang_tag) | |
#Remove default namespace for avoiding ns0 markup. | |
et.register_namespace("","http://www.idpf.org/2007/opf") | |
if epub_title is None: | |
print "Corrupted EPUB => " + opf | |
return False | |
#If the title of EPUB contains Chinese chracter and not contains Jap's Kana. | |
if ( re.search(ur"[\u4e00-\u9fa5]+", epub_title.text) | |
and not re.search(ur"[\u3041-\u3096\u30A0-\u30FF]+", epub_title.text) ): | |
# Add lang meta data if not present. | |
if epub_lang is None : | |
print "Language meta for %s is None" % opf | |
et.SubElement(opfroot[0], lang_tag) | |
epub_lang = opfroot[0].find(lang_tag) | |
if ( epub_lang.text == 'en' or epub_lang.text == '' ): | |
print "Changing Language to zh for %s" % opf | |
epub_lang.text = 'zh' | |
opftree.write(opf,encoding='UTF-8') | |
BOOK_LIB_PATH = os.path.join(os.path.expanduser("~"), | |
"Library/Containers/com.apple.BKAgentService/Data/Documents/iBooks/Books/") | |
bkplist = readPlist(BOOK_LIB_PATH + 'Books.plist') | |
zh_epubs = [] | |
for item in bkplist['Books']: | |
if ( not re.match(ur".*pdf$", item['BKDisplayName']) | |
and not 'BKMigrationSourcePath' in item ): | |
for i in os.walk(item['path']): | |
for f in i[2]: | |
if re.match(r'.*\.opf$',f): | |
opf = os.path.join(i[0],f) | |
print "OPF FOUND for " + item['BKDisplayName'] | |
change_lang_zh(opf) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment