AnimeSuki Forums

Register Forum Rules FAQ Community Today's Posts Search

Go Back   AnimeSuki Forum > Support > Tech Support

Notices

Reply
 
Thread Tools
Old 2006-10-18, 10:03   Link #1
Sylf
翻訳家わなびぃ
*Fansubber
 
 
Join Date: Nov 2003
Age: 50
Send a message via MSN to Sylf Send a message via Yahoo to Sylf
μTorrent and X-Chat

It was just last weekend I found out that µTorrent's webUI has entered the public beta stage. (Yes, I'm 3 weeks behind the scene.) I took the opportunity, and hacked up a quick plugin to display the status of µTorrent in the channel of your choice.

I wanted to post this here first, take a beating from a few people first, before posting on more public places, like official x-chat plugin repository. Constructive feedbacks are welcome. Feedbacks about the webui itself will be ignored

prerequisite
X-Chat with Perl plugin module
ActivePerl with Text-CSV module
latest µTorrent with webUI

Installation - XChat
This part is easy. Just grab a copy of XChat for windows, and make sure that you install the Perl plugin interface. You can get a free copy from Silverex's site.

Installation - ActivePerl
Grab a copy of ActivePerl from ActiveState site. Install as instructed.
Once that's installed, open cmd prompt. Type in ppm.
Once you're at ppm prompt, type in
install Text-CSV
That should install the module that parses comma separated values (CSV). You can type quit to exit out of ppm.

Installation - µTorrent
Grab a copy of latest beta version of µTorrent from here.
Unpack to a directory.
Run µTorrent, and open preference dialog, and go to Advanced->Web UI.

Enable the web interface.
Set up the authentication and port used for the web UI, and take a note of it.

Installation - the script
Save this code below as utorrent.pl (or whatever filename) and save it under C:\Documents and Settings\<USER>\Application Data\X-Chat 2
Code:
#!/usr/bin/perl
use Win32::Internet;
use Text::CSV;

package uTorrent;

Xchat::register('uTorrent','1.0');
Xchat::hook_command('utinfo','uTorrent::display_info');

sub display_info
{
    my $ut_host = "localhost";
    my $ut_port = "8086";
    my $ut_username = "myuser";
    my $ut_password = "mypass";

    my $REQ;
    my $HTTP;
    my $INET = new Win32::Internet();
    my $result = $INET->HTTP($HTTP, $ut_host, $ut_username, $ut_password, $ut_port);
    $HTTP->OpenRequest($REQ, "/gui/?list=1");
    $REQ->AddHeader("If-Modified-Since: Saturday, 16-Nov-96 15:58:50 GMT");
    $REQ->SendRequest();

    my $file = $REQ->ReadEntireFile();
    $REQ->Close();
    $HTTP->Close();
    $INET->Close();

    if ($file)
    {
        @lines = split (/\n/,$file);
        my @torrents;
        my $ready = 0;
        for $line (@lines)
        {
            if ($line =~ m/^\["/ && $ready)
            {
                $torrent_info = substr ($line, 1, -2);
                %status = print_ut_status($torrent_info);
            }
            if ($line eq ',"torrents": [') { $ready = 1; }
        }
    }
    else
    {
        Xchat::print ("uTorrent not running");
    }
    return Xchat::EAT_XCHAT;
}

sub print_ut_status
{
    my $torrent_info = shift;
    my $csv = new Text::CSV;
    $csv->parse($torrent_info);
    @ut_info = $csv->fields();
    if ($ut_info[1] & 1 || ! $ut_info[1] & 32)
    {
        %info = (
            "name" => $ut_info[2],
            "size" => $ut_info[3],
            "percent" => $ut_info[4],
            "down_bytes" => $ut_info[5],
            "up_bytes" => $ut_info[6],
            "ratio" => $ut_info[7],
            "up_speed" => $ut_info[8],
            "down_speed" => $ut_info[9],
            "eta" => $ut_info[10],
            "label" => $ut_info[11],
            "peers_conn" => $ut_info[12],
            "peers_total" => $ut_info[13],
            "seeds_conn" => $ut_info[14],
            "seeds_total" => $ut_info[15],
            "availability" => $ut_info[16],
            "priority" => $ut_info[17],
            "remaining" => $ut_info[18]
        );
        $size = byte_format($info{size});
        $up_speed = byte_format($info{up_speed});
        $down_speed = byte_format($info{down_speed});
        $eta = ($info{eta}>0)?' (ETA ' . time_format($info{eta}) . ')':'';
        Xchat::commandf ("say [uTorrent] - %s (%s) - %s Complete%s - Upload %s/s - Download %s/s - Peer/Seed %d(%d)/%d(%d)",
            $info{name}, $size, $info{percent}/10 . "%", $eta, $up_speed, $down_speed,
            $info{peers_conn}, $info{peers_total}, $info{seeds_conn}, $info{seeds_total});
    }
}

sub byte_format
{
    my $size = shift;
    my $dsize, $size_unit;
    if ($size < 1024)
    {
        $dsize = $size;
        $size_unit = "B";
    }
    else
    {
        if ($size < 1048576)
        {
            $dsize = sprintf ("%.1f", $size / 1024);
            $size_unit = "KB";
        }
        else
        {
            if ($size < 1073741824)
            {
                $dsize = sprintf ("%.1f", $size / 1048576);
                $size_unit = "MB";
            }
            else
            {
                $dsize = sprintf ("%.1f", $size / 1073741824);
                $size_unit = "GB";
            }
        }
    }
    return "$dsize$size_unit";
}

sub time_format
{
    my $time = shift;
    $time = int($time / 60);
    return "$time min";
}
Change this following section to match your settings for uTorrent's web UI:
Code:
    my $ut_host = "localhost";
    my $ut_port = "8086";
    my $ut_username = "myuser";
    my $ut_password = "mypass";
As you can see, we're storing the password in clear text, so be smart about where to set the right file permission for this script, and use a proper password, etc.
In X-Chat, go to Windows->Plugins and Scripts.
Click "Load", locate the script you just saved, and load her up.
Type /utinfo to display the info. It should display all currently active torrents you're running. (Not paused, not stoped, not queued.)

To-Do
Why win32::Internet? That really should be written to use perl's native network module.
And why Text::CSV? Since the file we're parsing is in JSON, we should be using Text::JSON module.
As far as I know, it can't handle non-ascii text. Maybe it's Text::CSV's limitation. That should be looked into as well.
Sylf is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 18:01.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
We use Silk.