summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzyp <zyp@localhost>2006-05-17 01:21:27 +0200
committerzyp <zyp@localhost>2006-05-17 01:21:27 +0200
commita2ebd9bca9c4077dc6642c29d4e6f2d7107c0c24 (patch)
tree43f990d5b73fded2d37159a8e18a8ae4c9c09840
parent8e9e23cddd72d2dd407164f8a7e835fa6c84b4d2 (diff)
[project @ zyp-20060516232127-1ed2efc46068ab87]
[project @ 33] Added optionparser.
-rw-r--r--anidb_add63
-rw-r--r--pyanidb.conf4
-rw-r--r--pyanidb.py103
3 files changed, 106 insertions, 64 deletions
diff --git a/anidb_add b/anidb_add
deleted file mode 100644
index b0d06bd..0000000
--- a/anidb_add
+++ /dev/null
@@ -1,63 +0,0 @@
-#!/usr/bin/env python
-
-import pyanidb, pyanidb.hash
-import ConfigParser, os, sys, getpass, multihash
-
-def auth():
- try:
- c = ConfigParser.ConfigParser()
- c.read(os.path.expanduser('~/.pyanidb.conf'))
- username = c.get('auth', 'username')
- password = c.get('auth', 'password')
- except:
- username = raw_input('Username: ')
- password = getpass.getpass()
- return username, password
-
-username, password = auth()
-
-try:
- a = pyanidb.AniDB(username, password)
- #t = a.ping()
- #if t:
- # print 'AniDB is reachable, %.3fs' % (t)
- #else:
- # print 'AniDB is unreachable.'
- # sys.exit(1)
- a.auth()
- print 'Logged in as user %s.' % (username)
- if a.new_version:
- print 'New version available.'
-
- files = []
- for name in sys.argv[1:]:
- if os.access(name, os.R_OK):
- files.append(name)
- else:
- print 'Invalid file: %s' % (name)
- for filename, hash in pyanidb.hash.hash_files(files):
- hash = hash.ed2k()
- size = os.stat(filename).st_size
- print 'Hashed: ed2k://|file|%s|%d|%s|' % (filename, size, hash)
- try:
- while 1:
- try:
- a.add_hash(size, hash)
- except pyanidb.AniDBTimeout:
- print 'Connection timed out, retrying.'
- continue
- break
- except pyanidb.AniDBUnknownFile:
- print 'Unknown file: %s' % (filename)
- continue
- print 'Added file: %s' % (filename)
- print 'All operations finished.'
-except pyanidb.AniDBUserError:
- print 'Invalid username/password.'
- sys.exit(1)
-except pyanidb.AniDBTimeout:
- print 'Connection timed out.'
- sys.exit(1)
-except pyanidb.AniDBError, e:
- print 'Fatal error:', e
- sys.exit(1)
diff --git a/pyanidb.conf b/pyanidb.conf
index f400006..fe61771 100644
--- a/pyanidb.conf
+++ b/pyanidb.conf
@@ -1,5 +1,7 @@
# Put this under ~/.pyanidb.conf and chmod 600 to prevent other users from reading your password.
-[auth]
+[pyanidb]
username = foo
password = password
+
+suffix = avi ogm mkv
diff --git a/pyanidb.py b/pyanidb.py
new file mode 100644
index 0000000..62258c9
--- /dev/null
+++ b/pyanidb.py
@@ -0,0 +1,103 @@
+#!/usr/bin/env python
+
+import pyanidb, pyanidb.hash
+import ConfigParser, optparse, os, sys, getpass, multihash
+
+# Config.
+
+config = {}
+try:
+ cp = ConfigParser.ConfigParser()
+ cp.read(os.path.expanduser('~/.pyanidb.conf'))
+ for option in cp.options('pyanidb'):
+ config[option] = cp.get('pyanidb', option)
+except:
+ pass
+
+# Options.
+
+op = optparse.OptionParser()
+
+op.add_option('-u', '--username', help = 'AniDB username.',
+ action = 'store', dest = 'username', default = config.get('username'))
+op.add_option('-p', '--password', help = 'AniDB password.',
+ action = 'store', dest = 'password', default = config.get('password'))
+
+op.add_option('-r', '--recursive', help = 'Recurse into directories.',
+ action = 'store_true', dest = 'recursive', default = False)
+op.add_option('-s', '--suffix', help = 'File suffix for recursive matching.',
+ action = 'append', dest = 'suffix', default = config['suffix'].split())
+
+op.add_option('-a', '--add', help = 'Add files to mylist.',
+ action = 'store_true', dest = 'add', default = False)
+
+options, args = op.parse_args(sys.argv[1:])
+
+# Defaults.
+
+options.login = options.add
+
+# Authorization.
+
+if options.login:
+ if not options.username:
+ options.username = raw_input('Username: ')
+ if not options.password:
+ options.passord = getpass.getpass()
+ a = pyanidb.AniDB(options.username, options.password)
+ try:
+ a.auth()
+ print 'Logged in as user %s.' % (options.username)
+ if a.new_version:
+ print 'New version available.'
+ except pyanidb.AniDBUserError:
+ print 'Invalid username/password.'
+ sys.exit(1)
+ except pyanidb.AniDBTimeout:
+ print 'Connection timed out.'
+ sys.exit(1)
+ except pyanidb.AniDBError, e:
+ print 'Fatal error:', e
+ sys.exit(1)
+
+# Input files.
+
+files = []
+for name in args:
+ if not os.access(name, os.R_OK):
+ print 'Invalid file: %s' % (name)
+ elif os.path.isfile(name):
+ files.append(name)
+ elif os.path.isdir(name):
+ if not options.recursive:
+ print 'Is a directory: %s' % (name)
+ else:
+ for root, subdirs, subfiles in os.walk(name):
+ subdirs.sort()
+ subfiles.sort()
+ files += [os.path.join(root, file) for file in subfiles if sum([file.endswith('.' + suffix) for suffix in options.suffix])]
+
+# Hashing.
+
+for filename, hash in pyanidb.hash.hash_files(files):
+ size = os.path.getsize(filename)
+ print 'Hashed: ed2k://|file|%s|%d|%s|' % (filename, size, hash.ed2k())
+
+ # Adding
+
+ if options.add:
+ try:
+ while 1:
+ try:
+ a.add_hash(size, hash.ed2k())
+ except pyanidb.AniDBTimeout:
+ print 'Connection timed out, retrying.'
+ continue
+ break
+ print 'Added file: %s' % (filename)
+ except pyanidb.AniDBUnknownFile:
+ print 'Unknown file: %s' % (filename)
+
+# Finished.
+
+print 'All operations finished.'