Page 1 of 1

AMC uTorrent keeplink solution

Posted: 17 Jan 2014, 07:39
by Digital God
I've found some solution how to use keeplink mode with uTorrent. I'm not good with Groovy, so maybe someone can optimize my code.
The solution is based on uTorrent API, so first you need to enable WebUI in uTorrent.

class to work with uTorrent API

Code: Select all

// uTorrent class
class Utorrent {
	def host = "localhost"
	def port = 80
	def id, password
	def token, cookie, lg
	
	protected void fetchToken() {
		lg.fine("Fetching uTorrent token...")
		String tok = get("/gui/token.html", [:], true)
		def m = tok =~ /<div .*>(.+)<\/div>/
		token = m[0][1]
	}

	protected void fetchCookie(URLConnection conn) {
		def headerName = null
		for (int i=1; (headerName = conn.getHeaderFieldKey(i)) != null; i++) {
			if (headerName.equalsIgnoreCase("Set-Cookie")) {
				String cook = conn.getHeaderField(i)
				cookie = cook.substring(0, cook.indexOf(";"))
			}
		}
	}
	
	protected String get(def cmd, def args, def isTok = false) {
		if(token == null && !isTok)
			fetchToken()
		def url = "http://$host:$port$cmd?" + (token != null ? "token=" + java.net.URLEncoder.encode(token, "UTF-8") : "")
		args.each {k, v ->
			url += "&" + java.net.URLEncoder.encode(k) + "=" + java.net.URLEncoder.encode(v)
		}
		def conn = new URL(url).openConnection()
		if(id != "" || password != "")
			conn.setRequestProperty("Authorization", "Basic " + "$id:$password".getBytes().encodeBase64().toString())
		if(!isTok && cookie != "")
			conn.setRequestProperty("Cookie", cookie.toString())
		else
			fetchCookie(conn)			
		def out = new ByteArrayOutputStream()
		out << conn.getInputStream()
		return out.toString()
	}
	
	protected void stop(String hash) {
		get("/gui/", ["action": "stop", "hash": hash])
	}
	
	protected void start(String hash) {
		// first rechech
		get("/gui/", ["action": "recheck", "hash": hash])
		// than start
		get("/gui/", ["action": "start", "hash": hash])
	}
}
variable defenition

Code: Select all

// define connection to uTorrent
def t_host = "localhost"
def t_port = tryQuietly{ ut_port } ?: 8080
def t_usr = tryQuietly{ ut_usr } ?: "admin"
def t_pwd = tryQuietly{ ut_pwd } ?: "passwd"
def ut = new Utorrent(host: t_host, port: t_port, id: t_usr, password: t_pwd, lg: _log)
You need to add
"ut_hash=%I" "ut_port=8080" "ut_usr=admin" "ut_pwd=pwd"
to "Run programm" in uTorrent.

Two questions remained to be resolved:
1. We cannot move file when it is seeded by uTorrent
2. There is a problem with start seeding over symlink (Error: Files missing from job. Please recheck). Even if you recheck and then start seeding - you'll get the same error.

My solution is following:
1. Stop seeding
2. Move file and make symlink
3. Force torrent recheck and start seeding
4. Start seeding while checking

It remains to determine where to place ut.start and ut.stop commands in main AMC code.

Re: AMC uTorrent keeplink solution

Posted: 03 Feb 2014, 09:35
by tsoet
Interesting. This code is meant to be appended to amc.groovy, then?