summaryrefslogtreecommitdiff
path: root/anidb_add
blob: b0d06bdaad942c67dae8243415813d1a4b322146 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/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)