All pastes #749354 Raw Edit

deluge-shell

public python v1 · immutable
#749354 ·published 2007-10-25 17:17 UTC
rendered paste body
#!/usr/bin/env pythonimport deluge.ui.client as clientimport deluge.common as commonimport readlineimport sysstatus_keys = ["state",			"save_path",			"tracker",			"next_announce",			"name",			"total_size",			"progress",			"num_seeds",			"total_seeds",			"num_peers",			"total_peers",			"eta",			"download_payload_rate",			"upload_payload_rate",			"ratio",			"distributed_copies",			"num_pieces",			"piece_length",			"piece_info",			"total_done",		]def add_torrent(cmd):	"""Add a torrent."""	def show_usage():		print "Usage: add [-p <save-location>;] <torrent-file>; [<torrent-file>; ...]"		print "       (Note that a ';' must follow a path)"		print ""	if len(cmd) < 2:		show_usage()		return	save_path = None	readpath = False	if cmd[1] == '-p':		if len(cmd) < 4:			show_usage()			return			del cmd[1]		readpath = True	else:		configs = client.get_config()		save_path = configs['download_location']	command = " ".join(cmd[1:])	paths = command.split(';')	if readpath:		save_path = paths[0].strip()   # Perhaps verify that the path exists?		client.set_config({'download_location': save_path})		del paths[0]	if not save_path:		print "There's no save-path specified. You must specify a path to save the downloaded files.\n"		return	for iter in range(0, len(paths)):		paths[iter] = paths[iter].strip()		if len(paths[iter]) == 0:			del paths[iter]		try:		client.add_torrent_file(paths)	except Exception, msg:		print "*** Error:", str(msg), "\n"def show_state(state):	return common.TORRENT_STATE[state]def show_info(torrent, brief):	"""Show information about a torrent."""	state = client.get_torrent_status(torrent, status_keys)	print "*** ID:", torrent	print "*** Name:", state['name']	print "*** Path:", state['save_path']	print "*** Completed:", common.fsize(state['total_done']) + "/" + common.fsize(state['total_size'])	print "*** Status:", show_state(state['state'])	if state['state'] in [3, 4, 5, 6]:		print "*** Download Speed:", common.fspeed(state['download_payload_rate'])		print "*** Upload Speed:", common.fspeed(state['upload_payload_rate'])		if state['state'] in [3, 4]:			print "*** ETA", "%s" % deluge.common.ftime(state['eta'])	if not brief:		print "*** Seeders:", "%s (%s)" % (state['num_seeds'], state['total_seeds'])		print "*** Peers:", "%s (%s)" % (state['num_peers'], state['total_peers'])		print "*** Share Ratio:", "%.1f" % state['ratio']		print "*** Availability:", "%.1f" % state['distributed_copies']	print ""def info_torrents(cmd):	"""Show information about the torrents."""	torrents = client.get_session_state()	for tor in torrents:		if len(cmd) < 2:			show_info(tor, True)		elif cmd[1] in tor:			show_info(tor, False)def exit(cmd):	"""Terminate."""	sys.exit(0)def show_help(cmd):	"""Show help."""	print "Available commands:"	for cmd, action, help in commands:		print "\t" + cmd + ": " + helpdef remove_torrent(cmd):	"""Remove a torrent."""	if len(cmd) < 2:		print "Usage: rm <torrent-id> [<torrent-id> ...]"		print "       Use 'list' to see the list of torrents."		print ""		return	try:		client.remove_torrent(cmd[1:])	except Exception, msg:		print "*** Error:", str(msg), "\n"commands = (('add', add_torrent, 'Add a torrent'),	('exit', exit, 'Terminate'),	('help', show_help, 'Show help about a command, or generic help'),	('info', info_torrents, 'Show information about the torrents'),	('quit', exit, 'Terminate'),	('rm', remove_torrent, 'Remove a torrent'),)client.set_core_uri("http://localhost:58846")print "Welcome to deluge-shell. Type 'help' to see a list of available commands."while True:	print "> ",	input = sys.stdin.readline()	if len(input) == 0:	break	input = input.strip().split(" ")	print ""	cmd = input[0]	found = False	for command, action, help in commands:		if command != cmd:			continue		action(input)		found = True		break	if not found:		print "Invalid command!"		show_help([])print "Thanks."