Created
April 16, 2012 10:20
-
-
Save briandfoy/2397521 to your computer and use it in GitHub Desktop.
Mojo::UserAgent interacting with Mac app Tranmission's RPC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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