mirror of
https://github.com/nims11/IPod-Shuffle-4g.git
synced 2025-12-08 00:18:01 +09:00
initialization added to Shuffler, check_unicode now uses recursive listdirs to cause minimal renaming, renaming now an optional argument through --rename-unicode, help message updated
This commit is contained in:
parent
3ce4cd7db0
commit
7d7f313034
1 changed files with 45 additions and 32 deletions
71
shuffle.py
71
shuffle.py
|
|
@ -11,6 +11,22 @@ import collections
|
||||||
import errno
|
import errno
|
||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
|
audio_ext = (".mp3", ".m4a", ".m4b", ".m4p", ".aa", ".wav")
|
||||||
|
list_ext = (".pls", ".m3u")
|
||||||
|
def make_dir_if_absent(path):
|
||||||
|
try:
|
||||||
|
os.makedirs(path)
|
||||||
|
except OSError as exc:
|
||||||
|
if exc.errno != errno.EEXIST:
|
||||||
|
raise
|
||||||
|
|
||||||
|
def raises_unicode_error(str):
|
||||||
|
try:
|
||||||
|
str.decode('utf-8').encode('latin-1')
|
||||||
|
return False
|
||||||
|
except UnicodeEncodeError, UnicodeDecodeError:
|
||||||
|
return True
|
||||||
|
|
||||||
class Record(object):
|
class Record(object):
|
||||||
|
|
||||||
def __init__(self, parent):
|
def __init__(self, parent):
|
||||||
|
|
@ -362,6 +378,10 @@ class Shuffler(object):
|
||||||
self.tunessd = None
|
self.tunessd = None
|
||||||
self.voiceover = voiceover
|
self.voiceover = voiceover
|
||||||
|
|
||||||
|
def initialize(self):
|
||||||
|
for dirname in ('iPod_Control/iTunes', 'iPod_Control/Music', 'iPod_Control/Speakable/Playlists', 'iPod_Control/Speakable/Tracks'):
|
||||||
|
make_dir_if_absent(os.path.join(self.path, dirname))
|
||||||
|
|
||||||
def dump_state(self):
|
def dump_state(self):
|
||||||
print "Shuffle DB state"
|
print "Shuffle DB state"
|
||||||
print "Tracks", self.tracks
|
print "Tracks", self.tracks
|
||||||
|
|
@ -399,48 +419,41 @@ class Shuffler(object):
|
||||||
# http://shuffle3db.wikispaces.com/iTunesSD3gen
|
# http://shuffle3db.wikispaces.com/iTunesSD3gen
|
||||||
# Use festival to produce voiceover data
|
# Use festival to produce voiceover data
|
||||||
#
|
#
|
||||||
def make_dir_if_absent(path):
|
|
||||||
try:
|
|
||||||
os.makedirs(path)
|
|
||||||
except OSError as exc:
|
|
||||||
if exc.errno != errno.EEXIST:
|
|
||||||
raise
|
|
||||||
|
|
||||||
def initialize(base_path):
|
|
||||||
for dirname in ('iPod_Control/iTunes', 'iPod_Control/Music', 'iPod_Control/Speakable/Playlists', 'iPod_Control/Speakable/Tracks'):
|
|
||||||
make_dir_if_absent(os.path.join(base_path, dirname))
|
|
||||||
|
|
||||||
def check_unicode(path):
|
def check_unicode(path):
|
||||||
for (dirpath, dirnames, filenames) in os.walk(path):
|
ret_flag = False # True if there is a recognizable file within this level
|
||||||
for dirname in dirnames:
|
print path
|
||||||
try:
|
for item in os.listdir(path):
|
||||||
dirname.decode('utf-8').encode('latin-1')
|
if os.path.isfile(os.path.join(path, item)):
|
||||||
except UnicodeEncodeError, UnicodeDecodeError:
|
if os.path.splitext(item)[1].lower() in audio_ext+list_ext:
|
||||||
src = os.path.join(dirpath, dirname)
|
ret_flag = True
|
||||||
new_name = "".join(["{0:02X}".format(ord(x)) for x in reversed(hashlib.md5(dirname).digest()[:8])])
|
if raises_unicode_error(item):
|
||||||
dest = os.path.join(dirpath, new_name)
|
src = os.path.join(path, item)
|
||||||
|
dest = os.path.join(path,
|
||||||
|
"".join(["{0:02X}".format(ord(x)) for x in reversed(hashlib.md5(item).digest()[:8])])) + os.path.splitext(item)[1].lower()
|
||||||
print 'Renaming %s -> %s' % (src, dest)
|
print 'Renaming %s -> %s' % (src, dest)
|
||||||
os.rename(src, dest)
|
os.rename(src, dest)
|
||||||
dirnames[dirnames.index(dirname)] = new_name
|
else:
|
||||||
for filename in filenames:
|
ret_flag = (check_unicode(os.path.join(path, item)) or ret_flag)
|
||||||
if os.path.splitext(filename)[1].lower() in (".mp3", ".m4a", ".m4b", ".m4p", ".aa", ".wav", ".pls", ".m3u"):
|
if ret_flag and raises_unicode_error(item):
|
||||||
try:
|
src = os.path.join(path, item)
|
||||||
filename.decode('utf-8').encode('latin-1')
|
new_name = "".join(["{0:02X}".format(ord(x)) for x in reversed(hashlib.md5(item).digest()[:8])])
|
||||||
except UnicodeEncodeError, UnicodeDecodeError:
|
dest = os.path.join(path, new_name)
|
||||||
src = os.path.join(dirpath, filename)
|
|
||||||
dest = os.path.join(dirpath,
|
|
||||||
"".join(["{0:02X}".format(ord(x)) for x in reversed(hashlib.md5(filename).digest()[:8])])) + os.path.splitext(filename)[1].lower()
|
|
||||||
print 'Renaming %s -> %s' % (src, dest)
|
print 'Renaming %s -> %s' % (src, dest)
|
||||||
os.rename(src, dest)
|
os.rename(src, dest)
|
||||||
|
return ret_flag
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('--disable-voiceover', action='store_true')
|
parser.add_argument('--disable-voiceover', action='store_true', help='Disable Voiceover Feature')
|
||||||
|
parser.add_argument('--rename-unicode', action='store_true', help='Rename Files Causing Unicode Errors, will do minimal required renaming')
|
||||||
parser.add_argument('path')
|
parser.add_argument('path')
|
||||||
result = parser.parse_args()
|
result = parser.parse_args()
|
||||||
|
|
||||||
|
if result.rename_unicode:
|
||||||
check_unicode(result.path)
|
check_unicode(result.path)
|
||||||
|
|
||||||
initialize(result.path)
|
|
||||||
shuffle = Shuffler(result.path, voiceover=not result.disable_voiceover)
|
shuffle = Shuffler(result.path, voiceover=not result.disable_voiceover)
|
||||||
|
shuffle.initialize()
|
||||||
shuffle.populate()
|
shuffle.populate()
|
||||||
shuffle.write_database()
|
shuffle.write_database()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue