Jump to content

Perl script accessing WEBUI problem


Recommended Posts

I am trying to write a Perl script that will pause all Torrents. I am just getting started and cannot get a list of Torrents returned. The script is using the Net::uTorrent module: http://cpan.uwinnipeg.ca/htdocs/Net-uTorrent/Net/uTorrent.html

This is the script so far:

# uTorrent_pause.pl
# Perl script to pause all uTorrent downloads...
use strict;
use warnings;
use Net::uTorrent;

my $utorrent = Net::uTorrent->new (
hostname => 'localhost',
port => '26693',
user => 'admin',
pass => 'admin',
die unless $utorrent->login_success;

my $tors = $utorrent->torrents ;

The call $utorrent->torrents is failing. The exact error message:

malformed JSON string, neither array, object, number, string or atom, at character offset 2 (before "invalid request") at C:/Perl64/site/lib/Net/uTorrent.pm line 82.

The code around line 82:

sub torrents {
my ($class,%args) = @_;
my %params = (
list => 1,
%params = (%params,%args); #Add optional cache id from argument.
my @args = qw();
push @args,\%params;
my $json_text = api_query_result(@args);
LINE 82 -> my $decoded_json = $json->decode($json_text);

I was able to do some debugging and $json_text has the string "invalid" in it. I did some more debugging and I am getting write failed:Unknown error code: 500 errors when it is making HTTP call to get the list. I can't tell you exactly where the error is generated because the flow of instructions bounces around many modules.

This is a pretty basic call to get the hashref list of torrents. I was wondering if maybe there is a setting somewhere that is causing this error. I tried running the script from an elevated DOS command prompt but that didn't affect the problem. Running this on Windows 7 64bit and IE9. Can anybody steer me in the right direction?

Link to comment
Share on other sites

The issue is the uTorrent library doesn't deal with cookies. (See http://forum.utorrent.com/viewtopic.php?id=111446). This bit me on the upgrade to 3.1.2.

Fix is pretty simple, a 2 liner. I'm running Linux, so my fix is quick and dirty for that. I'm sure a more elegant solution would find a better file location for the cookie jar, but I'm lazy. Here's what I added:

Line 17: use HTTP::Cookies;

Line 51 (after $ua->credentials...): $ua->cookie_jar(HTTP::Cookies::Netscape->new('file' => '/tmp/utorrent_cookies' ));

And that did the trick.

Link to comment
Share on other sites

I am not sure what is going on here but you are corrrect sir.

I had to modify it slightly to get it to work with Internet Explorer.

For those interested:

use HTTP::Cookies::Microsoft;

use Win32::TieRegistry(Delimiter => "/");

my $cookies_dir= $Registry->

{"CUser/Software/Microsoft/Windows/CurrentVersion/Explorer/Shell Folders/Cookies"};

$ua->cookie_jar(HTTP::Cookies::Microsoft->new(file => "$cookies_dir\\index.dat", 'delayload' => 1,));

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

  • Create New...