Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

I've recently set up an old MacBook Pro to use as our media computer in the lounge room. This Mac replaced the ageing Odroid XU4 home server I've had since 2016. That meant that the BitTorrent client had to now run on Mac and I chose Transmission for that purpose.

Previously I was using Deluge as the torrent client, and that had a plugin readily available for unzipping/unraring downloaded files. Unfortunately Transmission seems a little less mature in this regard so I had to come up with my own solution.

The end goal was to have any torrents with RAR files automatically decompressed to one central location. That location would still be in the Downloads folder, but not in the torrent directory itself.
txtunrar.jpg


Transmission has an option to run a script upon torrent download completion. So that was my way to implement this requirement. My script relies on the unrar command to be available, so there is a little bit of extra work required if you want to use it. First you need to install Homebrew and after that's done, type in the following (and press enter after) into the Terminal.app...
 Terminal
brew install unrar


Below are the contents of the script. It's a Bash script and should be saved in the home directory i.e. in Finder you can open the 'Home' folder from the Go menu, Home option. You can save the script below on your desktop and then drag it over to Home. Make sure the name of the file is txunrar.sh.
 ~/txunrar.sh
#!/bin/sh
if [ -z "$TR_TORRENT_DIR" ]; then
TR_TORRENT_DIR="/nodir"
fi
if [ -z "$TR_TORRENT_NAME" ]; then
TR_TORRENT_NAME="nofile"
fi
EXTRACT_DIR=~/Downloads/Expanded
LOG_FILE="$EXTRACT_DIR/expand.log"
TORRENT_PATH="$TR_TORRENT_DIR/$TR_TORRENT_NAME"
# create the extraction directory if it doesn't exist
if [ ! -d "$EXTRACT_DIR" ]; then
mkdir -p "$EXTRACT_DIR"
fi
function log() {
echo "`date` - $TORRENT_PATH - $1" >> "$LOG_FILE"
}
log "Extracing torrent"
# torrent is a single file
if [ -f "$TORRENT_PATH" ]; then
TORRENT_EXT="${TR_TORRENT_NAME##*.}"
if [ "rar" == "$TORRENT_EXT" ]; then
unrar e -y "$TORRENT_PATH" "$EXTRACT_DIR" > /dev/null
else
log "Error: Torrent is not a RAR file"
fi
# torrent is a directory
elif [ -d "$TORRENT_PATH" ]; then
RAR_FILES=`find "$TORRENT_PATH" -name "*.rar"`
if [ ! -z "$RAR_FILES" ]; then
find "$TORRENT_PATH" -name "*.rar" -exec unrar e -y {} "$EXTRACT_DIR" \; > /dev/null
else
log "Error: Torrent does not have a RAR file"
fi
# torrent is not a file or directory
else
log "Error: Torrent is not a file or directory"
fi
if [ ! $? -eq 0 ]; then
log "Error: Extraction of torrent failed"
fi
log "Finished processing torrent"




Then in Transmission, go to the preferences Transfers > Management > When download completes option. It's possible to pick a script to run, which I set to txunrar.sh in the home directory for the logged in user.
txunrar.png

Now whenever Transmission completes a torrent, it will run the script, which will check to see if the torrent is a RAR file or if it contains any RAR files inside it. The contents of the RAR files will be extracted into the Downloads > Extracted folder in your Home folder..

There is also a log file created and every time a torrent is processed, log entries are written to it. If anything goes wrong you will see it in the log file. This log file is called expand.log and is found in the Downloads > Extracted folder in your Home folder.
 expand.log
Tue 26 May 2020 22:11:50 AEST - /Users/mediauser/Downloads/Blog Pack - Extracing torrent
Tue 26 May 2020 22:11:50 AEST - /Users/mediauser/Downloads/Blog Pack - Error: Torrent does not have a RAR file
Tue 26 May 2020 22:11:50 AEST - /Users/mediauser/Downloads/Blog Pack - Finished processing torrent


You can learn more about Transmission scripts from its wiki page.

-i

Skip down to comments...
A quick disclaimer...

Although I put in a great effort into researching all the topics I cover, mistakes can happen. If you spot something out of place, please do let me know.

All content and opinions expressed on this Blog are my own and do not represent the opinions of my employer (Oracle). Use of any information contained in this blog post/article is subject to this disclaimer.
 
comments powered by Disqus
Other posts you may like...