summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzyp <zyp@localhost>2006-05-20 19:44:19 +0200
committerzyp <zyp@localhost>2006-05-20 19:44:19 +0200
commitd91dd17992c9bbf522cdbe3b57c4493bb44e6721 (patch)
tree1163aefbcb2e9452285bca808c0e3299e030db75
parent5e354503c2e7af39ab12e530c37e2794bc9494a9 (diff)
[project @ zyp-20060520174419-44a3f3da2d7a3cad]
[project @ 39] Added support for renaming files.
-rw-r--r--README4
-rw-r--r--anidb.py44
-rw-r--r--pyanidb.conf1
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