From d91dd17992c9bbf522cdbe3b57c4493bb44e6721 Mon Sep 17 00:00:00 2001 From: zyp Date: Sat, 20 May 2006 17:44:19 +0000 Subject: [project @ zyp-20060520174419-44a3f3da2d7a3cad] [project @ 39] Added support for renaming files. --- README | 4 ++++ anidb.py | 44 ++++++++++++++++++++++++++++++++++++++------ pyanidb.conf | 1 + 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/README b/README index 580e242..d20f1e4 100644 --- a/README +++ b/README @@ -25,7 +25,11 @@ options: -r, --recursive Recurse into directories. -s SUFFIX, --suffix=SUFFIX File suffix for recursive matching. + -i, --identify Identify files. -a, --add Add files to mylist. + -n, --rename Rename files. + -f FORMAT, --format=FORMAT + Filename format. Example: diff --git a/anidb.py b/anidb.py index 1fcda29..3cd8cac 100644 --- a/anidb.py +++ b/anidb.py @@ -32,12 +32,21 @@ op.add_option('-i', '--identify', help = 'Identify files.', action = 'store_true', dest = 'identify', default = False) op.add_option('-a', '--add', help = 'Add files to mylist.', action = 'store_true', dest = 'add', default = False) +op.add_option('-n', '--rename', help = 'Rename files.', + action = 'store_true', dest = 'rename', default = False) +op.add_option('-f', '--format', help = 'Filename format.', + action = 'store', dest = 'format', default = config.get('format')) options, args = op.parse_args(sys.argv[1:]) # Defaults. +options.identify = options.identify or options.rename options.login = options.add or options.identify +if not options.suffix: + options.suffix = ['avi', 'ogm', 'mkv'] +if not options.format: + options.format = r'_[%group]_%anime_-_%epno%ver_[%CRC].%suf' if options.login: if not options.username: @@ -91,20 +100,43 @@ for filename, hash in pyanidb.hash.hash_files(files): fid = (size, hash.ed2k()) try: - # Identify + + # Identify. if options.identify: - info = a.get_file(fid, ('gname', 'romaji', 'epno', 'epromaji'), True) + info = a.get_file(fid, ('gtag', 'romaji', 'epno', 'state', 'epromaji', 'crc32', 'filetype'), True) fid = int(info['fid']) - print '[%s] %s - %s - %s' % (info['gname'], info['romaji'], info['epno'], info['epromaji']) - # Adding + print 'Identified: [%s] %s - %s - %s' % (info['gtag'], info['romaji'], info['epno'], info['epromaji']) + + # Renaming. + + if options.rename: + s = options.format + rename_data = { + 'group': info['gtag'], + 'anime': info['romaji'], + 'epno': info['epno'], + 'ver': {0: '', 4: 'v2', 8: 'v3', 16: 'v4', 32: 'v5'}[(int(info['state']) & 0x3c)], + 'crc': info['crc32'], + 'CRC': info['crc32'].upper(), + 'suf': info['filetype']} + for name, value in rename_data.iteritems(): + s = s.replace(r'%' + name, value) + if s[0] == '_': + s = s[1:].replace(' ', '_') + s = s.replace('/', '_') + + print 'Renaming to: %s' % (s) + os.rename(filename, os.path.join(os.path.split(filename)[0], s)) + + # Adding. if options.add: a.add_file(fid, retry = True) - print 'Added file: %s' % (filename) + print 'Added to mylist.' except pyanidb.AniDBUnknownFile: - print 'Unknown file: %s' % (filename) + print 'Unknown file.' # Finished. diff --git a/pyanidb.conf b/pyanidb.conf index fe61771..89807a0 100644 --- a/pyanidb.conf +++ b/pyanidb.conf @@ -5,3 +5,4 @@ username = foo password = password suffix = avi ogm mkv +format = _[%group]_%anime_-_%epno%ver_[%CRC].%suf -- cgit v1.2.3