Skip to content

Instantly share code, notes, and snippets.

@Pyppe
Last active August 29, 2015 13:57
Show Gist options
  • Save Pyppe/9446546 to your computer and use it in GitHub Desktop.
Save Pyppe/9446546 to your computer and use it in GitHub Desktop.
Simple scala script for wrapping `transmission-remote`
#!/bin/bash
exec scala -savecompiled "$0" "$@"
!#
// Given all torrents are paused, this script starts the first one
import sys.process._
val host = "127.0.0.1:9091"
val auth = "myuser:mypassword"
def run(cmd: String): String = {
val sb = new StringBuilder
Process(cmd) ! ProcessLogger({out: String => sb.append(out + "\n")})
sb.toString
}
def runTorrentCmd(cmd: String): String =
run(s"transmission-remote $host --auth $auth $cmd")
val lines = runTorrentCmd("--list").split("\n").toList
if (lines.size > 2) {
val torrents = lines.drop(1).dropRight(1)
val stopped = torrents.filter(_.contains(" Stopped "))
println(s"${torrents.size} torrents, ${stopped.size} of them stopped")
if (stopped.size == torrents.size) {
stopped.head.split("([ ]{2,})").map(_.trim).filter(_.nonEmpty) match {
case Array(id, percentDone, downloaded, eta, up, down, ratio, status, name) =>
println(s"Starting torrent $id: $name")
println(runTorrentCmd(s"--torrent $id --start"))
}
}
}
time ./data/scripts/start-paused-torrent.scala
6 torrents, 6 of them stopped
Starting torrent 51: jdk-7u51-linux-x64.tar.gz
127.0.0.1:9091/transmission/rpc/ responded: "success"
real 0m0.278s
user 0m0.236s
sys 0m0.048s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment