Skip to content

Instantly share code, notes, and snippets.

@briandfoy
Created April 16, 2012 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briandfoy/2397521 to your computer and use it in GitHub Desktop.
Save briandfoy/2397521 to your computer and use it in GitHub Desktop.
Mojo::UserAgent interacting with Mac app Tranmission's RPC
use v5.10;
use utf8;
use strict;
use warnings;
# https://trac.transmissionbt.com/browser/branches/1.7x/doc/rpc-spec.txt
use Mojo::UserAgent;
use JSON qw(to_json);
my $ua = Mojo::UserAgent->new;
my $url = 'http://localhost:9091/transmission/rpc/';
get_session( $ua );
add_torrent( $ARGV[0] ) unless caller;
sub add_torrent {
my( $filename ) = @_;
my $rpc_request = {
arguments => {
filename => $filename,
},
method => 'torrent-add',
};
my $json = to_json( $rpc_request );
$ua->post( $url, $json );
}
sub get_session {
my $session = $ua->get( $url )->
res->
headers->
header( 'X-Transmission-Session-Id' );
$ua->on( start => sub {
my( $ua, $tx ) = @_;
$tx->req->headers->header( 'X-Transmission-Session-Id' => $session );
},
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment