Skip to content

Instantly share code, notes, and snippets.

@xvorenda
Forked from sbisbee/transmission-remote-magnet.bash
Last active February 21, 2017 21:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xvorenda/05abe9a7ccb06a046434 to your computer and use it in GitHub Desktop.
Save xvorenda/05abe9a7ccb06a046434 to your computer and use it in GitHub Desktop.
Quick and easy way of making Chrome send bittorrent magnet links to a remote Transmission instance using its API on OSX 10.11.1 using built-in tools.

Quick and easy way of making Chrome send bittorrent magnet links to a remote Transmission instance using its API on OSX, with PHP, Javascript, and OSX's built-in Apache.

Chrome does not handle default protocol applications without a little encouragement. But we're about to take care of that (see Step 6).

Steps:

  1. Install the shell script transmission-remote-magnet: cp ~/src/transmission-remote-magnet.sh /usr/local/bin/transmission-remote-magnet

  2. Install the php file, which is required for link handling to work, and will likely require sudo: sudo cp ~/src/trm.php /Library/WebServer/Documents/

  3. Enable PHP by uncommenting (aka deleting the # before) this line in /private/etc/apache2/httpd.conf with sudo vi or an equivalent: LoadModule php5_module libexec/apache2/libphp5.so

  4. You may also want to lock Apache to the localhost for security - in the file above, change the line Listen 80 to Listen 127.0.0.1:80

  5. Start the built-in webserver: sudo apachectl start

  6. Tell your Chrome to use the php file for the magnet MIME type. Navigate to http://127.0.0.1/trm.php and paste the following string in the navigation box, then hit Enter/Return: javascript:navigator.registerProtocolHandler("magnet","http://127.0.0.1/trm.php?url=%s","TRM")

Right now, when you click a magnet: link in Chrome, you get a readout page - X-Transmission-Session-Id, the full magnet link, and the output from Transmission that hopefully ends with "result":"success"}, and a javascript "Go Back" button. I may or may not change this behavior so that the page doesn't print a readout and auto-returns to the previous page. It was very useful for debugging, though, so I may keep it and style it with some CSS, maybe put the return on a timer... We'll see.

#!/bin/bash
# transmission-remote-magnet
# Usage: transmission-remote-magnet <magnet URL>
#
# Adapted to function with OSX 10.11.1 by XVO <xvo@zerua.org> | 11.28.2015
# Copyright 2014 Sam Bisbee <sam@sbisbee.com>
# Modified from http://blog.flo.cx/2011/02/how-to-open-magnet-links-on-a-remote-transmission-daemon-with-one-click/
MAGNET="$1"
if [ -z "$MAGNET" ]; then
echo "Error: No Magnet Link"
exit 1
fi
echo $MAGNET >> /tmp/trm.log
HOST="your-remote-transmission-ip"
PORT="your-remote-transmission-port"
USER="transmission"
PASS="transmission"
SESSID=$(curl --silent --anyauth --user $USER:$PASS "http://$HOST:$PORT/transmission/rpc" | sed 's/.*<code>//g;s/<\/code>.*//g')
curl --silent --anyauth --user $USER:$PASS --header "$SESSID" "http://$HOST:$PORT/transmission/rpc" -d "{\"method\":\"torrent-add\",\"arguments\":{\"filename\":\"${MAGNET}\"}}" >> /tmp/trm.log
exit 1
<div style='display:none'>javascript:navigator.registerProtocolHandler("magnet","http://127.0.0.1/trm.php?url=%s","TRM")</div>
<?php
if (isset($_GET['url'])) {
$MAGNET = $_GET['url'];
}else{
echo "Something Went Wrong..." ;
}
$HOST="your-remote-transmission-ip";
$PORT="your-remote-transmission-port";
$USER="transmission";
$PASS="transmission";
echo "<html><body><div style='max-width:400px;overflow-wrap: break-word;'>";
$SESSID = shell_exec('curl --silent --anyauth --user '.$USER'.':'.'$PASS.' http://'.$HOST.':'.$PORT.'/transmission/rpc | sed "s/.*<code>//g;s/<\/code>.*//g"');
$EXEC = shell_exec('/usr/local/bin/transmission-remote-magnet '.$MAGNET.' >> ~/trm.log');
echo "<pre class='session'>$SESSID</pre>";
echo "<pre class='link'>$MAGNET</pre>";
$LOG = shell_exec('tail -n 1 ~/trm.log');
echo "<pre class='log'>$LOG</pre>";
echo "</div>"
?>
<button onclick="goBack()">Go Back</button>
<script>
function goBack() {
window.history.back();
}
</script>
</body>
</html>
@lconnolly
Copy link

I was really excited to get this working – but despite trying every possible configuration I could think of, I can't seem to get this working.

When I hit 127.0.0.1/trm.php I get a 500 server error - "127.0.0.1 is currently unable to handle this request"

I verified that my apache2/httpd.conf was accurate, and I tested a php file at 127.0.0.1/test.php just to make sure Apache was running properly, but no luck...

Do you have any ideas? I would looove to get this working. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment