platypus + screen + ssh + btdownloadheadless = no more local bittorrent clients 4
Like many carefree young programmers, I own a laptop (17” powerbook) which I think is approximately better than sex. At the same time, there are things to which it’s less than perfectly adapted, bittorrent downloads being one of them. To wit:
1. I take my laptop onsite occasionally. At the very least, this means that my downloads are interrupted: at worst, I gank my client’s network connection by accident. (Hasn’t happened yet, but it’d only be a matter of time.)
2. The Mac clients I’ve found seem rather resource hungry: on a single download with Tomato Torrent, my CPU usage hovers around 70%.
3. If I download to my laptop, I still have to transfer it to the shared drive. This may seem a trivial objection, but I’m a lazy man, and I have a desktop full of downloads to prove exactly how lazy.
4. It’s easier to set up throttling on my Linux box. Bittorrent being a greedy protocol, this is important.
So what’s a lazy programmer to do? Write a script, of course.
First step was to set up a script to ssh to my Linux server (magarak). I have ssh keys set up there already, so the command “ssh magarak” doesn’t require a password.
punt_torrent.sh
#!/bin/zsh
scp “$1” @magarak:/data/torrents
f=`basename $1`
ssh magarak “cd /data/incoming;screen -S bittorrent -X screen;screen -S bittorrent -X exec btdownloadcurses \”/data/torrents/$f\””
Run this script with the argument foo.torrent, and all is peaches and cream. Notice the use of screen here: using a screen session on magarak called bittorrent, we first create a new window within it (screen -S bittorrent -X screen) and then screen -S bittorrent -X exec btdownloadcurses /data/torrents/$f to actually start the process. (The first scp command puts the torrent somewhere the linux box can see it.)
The use of screen here makes it easy to add downloads on the fly, and I can detach the screen any time I want to see what’s happening. Torrents go to /data/torrents, and the files themselves go to /data/incoming.
I then tried to get MacOS to use my little script for all torrent files. No dice, however. Shell scripts on MacOS X aren’t fully fledged applications, it seems, and the OS turns up its nose at them. Platypus came to my rescue: this app promotes shell scripts to the first rank, with the somewhat baffling snag that it bumps the arglist by one. This poses only a minor roadbump: change $1 to $2 and we are set:
#!/bin/zsh
scp “$2” @magarak:/data/torrents
f=`basename $2`
ssh magarak “cd /data/incoming;screen -S bittorrent -X screen;screen -S bittorrent -X exec btdownloadcurses \”/data/torrents/$f\””
then
18:26 ~/local/bin % platypus -D punt_torrent.sh punt_torrent
and we have an app that does exactly what I want. The last remaining step is to associate torrent files with it by right-clicking on a torrent, going to “Get Info”, choosing “Open With”, browsing to punt_torrent.app, and clicking “Change All”.
EDIT: it’s probably best to rename the torrents, or you run into nasty quoting problems.
If you liked this, or found it in any way useful, please let me know. I know it sounds pretty simple, but I went down a lot of blind alleys to get this working.

Aw c’mon, describe some of the blind alleys!
One day I’ll get back to hacking on combinatorrent. The whole idea there is to make an efficient torrent client in Haskell. On my dual Athlon MP 1544MHz system, btdownloadmany can still use 70% of one cpu with three or four torrents running.
Well, they’re all a bit dumb, really… i looked into getting download managers for safari and firefox, I looked into greasemonkey, I looked into applescript… nothing really came of any of them. The biggest win was finding platypus - the fact that scripts aren’t considered apps natively irks me a little, to be honest, and I think it’s a pointer to one of the cracks in the Mac/Unix marriage.
btdownloadcurses seems ok cpu-wise, but I imagine you have a much fatter pipe than mine, you lucky European bastard.
What’s the deal with btdownloadmany? And how is combinatorrent going to do it more efficiently?
I used to have no trouble running many bt downloads at ridiculous Swedish speeds (100s kb / s each) on an old 455MHz 200Mb Alpha, so I dunno why your clients suck so many resources.
Nice script, but I just use screen and good old cut and paste - the command line bt* tools understand URLs. They’re chugging away on the old P120 just fine, but that’s not much of an achievement at 500b/s. :-/
I’m a bit neurotic that way. I’d rather spend a day on a script to make it trivial than spend thirty seconds on each torrent. I think I’m starting to get the Mac disease - this odd feeling that if I have to open a terminal to do it, and I do it regularly, that there’s probably a better way.
As to resources, I was using a gui client. It shouldn’t have sucked resources, I suppose, but it did…