Jump to content

Show utorrent stats on apache server


linux16

Recommended Posts

Hmm i'm trying to do this too, and i've read the PHP thing pretty full, i just can't work out how to use it..

I ideally want to include as little code as possible to make it more lightweight.

I can set variables for the password etc, but just need the PHP code for using it to acquire just the CURRENT UPLOAD AND DOWNLOAD..

Thanks for any responses.. :)

Link to comment
Share on other sites

if you put this code in a php file called api.php PHP api

then you can create a php file called dlulspeed.php for example and in dlulspeed.php

you put this code

<?php
include("api.php");
//print out utorrent uldl speed
$utorrent = new uTorrent();
$torrents = $utorrent->getTorrents();
foreach($torrents as $torrent) {
$totul = $totul +$torrent[UTORRENT_TORRENT_UPSPEED];
$totdl = $totdl +$torrent[UTORRENT_TORRENT_DOWNSPEED];
}


$totul = $totul/1024;
$totdl = $totdl/1024;
$totdl= sprintf("%01.2f", $totdl);
$totul= sprintf("%01.2f", $totul);
echo "Ul speed is <span style='color:#006600;'>$totul KB/S</span> Dl speed is <span style='color:#ff0000;'>$totdl KB/S</span> ";
?>

hope this helped

Link to comment
Share on other sites

Oh right yeah ok. Think they should be working...

here it is:

curl

cURL support enabled

cURL Information libcurl/7.18.1 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5

and

json

json support enabled

json version 1.2.1

Urm, i copied the code from the PHP API as you said, but changed the variables to my details;

<?php
define("UTORRENT_TORRENT_HASH",0);
define("UTORRENT_TORRENT_STATUS",1);
define("UTORRENT_TORRENT_NAME",2);
define("UTORRENT_TORRENT_SIZE",3);
define("UTORRENT_TORRENT_PROGRESS",4);
define("UTORRENT_TORRENT_DOWNLOADED",5);
define("UTORRENT_TORRENT_UPLOADED",6);
define("UTORRENT_TORRENT_RATIO",7);
define("UTORRENT_TORRENT_UPSPEED",8);
define("UTORRENT_TORRENT_DOWNSPEED",9);
define("UTORRENT_TORRENT_ETA",10);
define("UTORRENT_TORRENT_LABEL",11);
define("UTORRENT_TORRENT_PEERS_CONNECTED",12);
define("UTORRENT_TORRENT_PEERS_SWARM",13);
define("UTORRENT_TORRENT_SEEDS_CONNECTED",14);
define("UTORRENT_TORRENT_SEEDS_SWARM",15);
define("UTORRENT_TORRENT_AVAILABILITY",16);
define("UTORRENT_TORRENT_QUEUE_POSITION",17);
define("UTORRENT_TORRENT_REMAINING",18);
define("UTORRENT_FILEPRIORITY_HIGH",3);
define("UTORRENT_FILEPRIORITY_NORMAL",2);
define("UTORRENT_FILEPRIORITY_LOW",1);
define("UTORRENT_FILEPRIORITY_SKIP",0);
define("UTORRENT_TYPE_INTEGER",0);
define("UTORRENT_TYPE_BOOLEAN",1);
define("UTORRENT_TYPE_STRING",2);
define("UTORRENT_STATUS_STARTED",1);
define("UTORRENT_STATUS_CHECKED",2);
define("UTORRENT_STATUS_START_AFTER_CHECK",4);



class uTorrent {
//set host to be the IP or hostname of your uTorrent machine
//set user and pass to be the user and pass you use for the webui
public $host = "myhostname:port";
public $user = "username";
public $pass = "password";
public function is_online() {
$ch = curl_init();
//uses start because at present (WebUI 0.31, uTorrent 1.7.2) this simply returns {"":""} and doesn't modify anything.
curl_setopt($ch,CURLOPT_URL,"http://".$this->host."/gui/?action=");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_USERPWD,$this->user.":".$this->pass);
$ret = curl_exec($ch);
if($ret !== false) {
$ret = true;
}

return $ret;
}
//returns an array of all torrent information
public function getTorrents() {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"http://".$this->host."/gui/?list=1");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_USERPWD,$this->user.":".$this->pass);
$ret = curl_exec($ch);
//echo curl_getinfo($ch,CURLINFO_HTTP_CODE);
$obj = json_decode($ret,true);
$torrents = $obj['torrents'];
curl_close($ch);
return $torrents;
}
//returns an array of all labels.
public function getLabels(){
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"http://".$this->host."/gui/?list=1");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_USERPWD,$this->user.":".$this->pass);
$ret = curl_exec($ch);
$obj = json_decode($ret,true);
$labels = $obj['label'];
curl_close($ch);
return $labels;
}


//if you set $estring then when the function is done and returns false, estring will be a string telling you what error happened.
//$filename could probably be a temporary filenames ($_FILES['somefile']['tmp_name']) but I haven't checked
//$filename can be a URL
public function addTorrent($filename,&$estring = false) {

if(substr($filename,0,7) == "http://") {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"http://".$this->host."/gui/?action=add-url&s=".urlencode($filename));
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_USERPWD,$this->user.":".$this->pass);
curl_exec($ch);
} else if(file_exists($filename)) {
$form_fields = array();
//$form_fields['add_button'] = "Add File \n";
$form_fields['torrent_file'] = "@".realpath($filename);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"http://".$this->host."/gui/?action=add-file");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
//curl_setopt($ch,CURLOPT_HEADER,true);
curl_setopt($ch,CURLOPT_USERPWD,$this->user.":".$this->pass);
curl_setopt($ch,CURLOPT_POSTFIELDS,$form_fields);
curl_setopt($ch,CURLOPT_VERBOSE,true);
$ret = curl_exec($ch);
//echo $ret;
//echo curl_error($ch);

curl_close($ch);
$obj = json_decode($ret,true);
if(isset($obj['error'])) {
if($estring !== false) {
$estring = $obj['error'];
}
return false;
} else {
return true;
}
} else {
$estring = "File doesn't exist!";
return false;
}
}
public function removeTorrent($hash,$data=false) {
$hashes = "";
if(is_array($hash)) {
foreach($hash as $value) {
$hashes.="&hash=".$value;
}
} else {
$hashes = "&hash=".$hash;
}
if($data) {
$action='removedata';
} else {
$action='remove';
}

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"http://".$this->host."/gui/?action=".$action.$hashes);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_USERPWD,$this->user.":".$this->pass);
curl_exec($ch);
}
public function startTorrent($hash) {
$hashes = "";
if(is_array($hash)) {
foreach($hash as $value) {
$hashes.="&hash=".$value;
}
} else {
$hashes = "&hash=".$hash;
}
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"http://".$this->host."/gui/?action=start".$hashes);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_USERPWD,$this->user.":".$this->pass);
curl_exec($ch);
}
public function pauseTorrent($hash) {
$hashes = "";
if(is_array($hash)) {
foreach($hash as $value) {
$hashes.="&hash=".$value;
}
} else {
$hashes = "&hash=".$hash;
}
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"http://".$this->host."/gui/?action=pause".$hashes);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_USERPWD,$this->user.":".$this->pass);
curl_exec($ch);
}
public function stopTorrent($hash) {
$hashes = "";
if(is_array($hash)) {
foreach($hash as $value) {
$hashes.="&hash=".$value;
}
} else {
$hashes = "&hash=".$hash;
}
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"http://".$this->host."/gui/?action=stop".$hashes);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_USERPWD,$this->user.":".$this->pass);
curl_exec($ch);
}
public function forcestartTorrent($hash) {
$hashes = "";
if(is_array($hash)) {
foreach($hash as $value) {
$hashes.="&hash=".$value;
}
} else {
$hashes = "&hash=".$hash;
}
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"http://".$this->host."/gui/?action=forcestart".$hashes);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_USERPWD,$this->user.":".$this->pass);
curl_exec($ch);
}
public function recheckTorrent($hash) {
$hashes = "";
if(is_array($hash)) {
foreach($hash as $value) {
$hashes.="&hash=".$value;
}
} else {
$hashes = "&hash=".$hash;
}
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"http://".$this->host."/gui/?action=recheck".$hashes);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_USERPWD,$this->user.":".$this->pass);
curl_exec($ch);
}
//This could get the files of multiple torrents, but unfortunately json_decode overwrites the files array each time, instead of
// doing something nice like having files[i] where i is the number of how many hashes you used.
//If someone writes a better JSON decoding function which does that I'd be happy to use that instead.
public function getFiles($hash) {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"http://".$this->host."/gui/?action=getfiles&hash=".$hash);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_USERPWD,$this->user.":".$this->pass);
$ret = curl_exec($ch);
$obj = json_decode($ret,true);
return $obj;
}

//This could be get the properties of multiple torrents, but unfortunately json_decode overwrites the props array each time, instead of
// doing something nice like having props[i] where i is the number of how many hashes you used.
//If someone writes a better JSON decoding function which does that I'd be happy to use that instead.
public function getProperties($hash) {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"http://".$this->host."/gui/?action=getprops&hash=".$hash);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_USERPWD,$this->user.":".$this->pass);
$ret = curl_exec($ch);
$obj = json_decode($ret,true);
return $obj;
}

/*
* properties (in format * propertyname type and explanation)
* trackers string (what trackers to use seperated by \r\n\r\n)
* ulrate int (maximum upload rate of torrent)
* dlrate int (maximum download rate of torrent)
* superseed bool (whether or not to superseed)
* dht bool (whether or not to use DHT)
* pex bool (whether or not to use peer exchange)
* seed_override bool (whether or not global seeding settings are overridden)
* seed_ratio int (what ratio to seed to before stopping)
* seed_time int (what time to seed to before stopping (seconds))
* ulslots int (number of upload slots)
*/
//this might work using multiple hashes but for now I haven't set to be able to
//it equally might work with multiple properties and values, again I haven't tried so haven't coded it to do so
public function setProperty($hash,$property,$value) {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"http://".$this->host."/gui/?action=setprops&hash=".$hash."&s=".$property."&v=".$value);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_USERPWD,$this->user.":".$this->pass);
curl_exec($ch);
}
//$file is a number between 0 and the number of files in the torrent minus one. ( I think. might be 1-based)
//Files are in the order that getFiles returns them in
//Priority is one of the UTORRENT_FILEPRIORITYs I defined earlier
//If $files is an array it will set the priority on each
public function setPriority($hash,$files,$priority) {
$filenums = "";
if(is_array($files)) {
foreach($files as $value) {
$filenums.="&f=".$value;
}
} else {
$filenums = "&f=".$file;
}
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"http://".$this->host."/gui/?action=setprio&hash=$hash&p=".$priority.$filenums);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_USERPWD,$this->user.":".$this->pass);
curl_exec($ch);
}

public function getSettings() {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"http://".$this->host."/gui/?action=getsettings");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_USERPWD,$this->user.":".$this->pass);
$ret = curl_exec($ch);
$obj = json_decode($ret,true);
return $obj;
}
public function setSetting($setting,$value) {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"http://".$this->host."/gui/?action=setsetting&s=".$setting."&v=".$value);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_USERPWD,$this->user.":".$this->pass);
curl_exec($ch);
}

}

?>

Link to comment
Share on other sites

Yeah i've copied the exact code from your post above, and it just loads for ages then says Ul speed is 0.00 KB/S Dl speed is 0.00 KB/S

I can go to the webUI address normally and it shows up everything fine, both on localhost and via the address.

The only other thing is that the php isnt running on this computer but at a web host. Could that be a problem?

Versions:

uTorrent 1.7.7

WebUI 0.3.1.5 apparently (although i downloaded it from here:

http://forum.utorrent.com/viewtopic.php?id=14565

Link to comment
Share on other sites

tested this code ? dont know if i postet it and then edited it away

 <?php

include("api.php");
//print out utorrent uldl speed
$utorrent = new uTorrent();
$torrents = $utorrent->getTorrents();
foreach($torrents as $torrent) {
$totul = $totul +$torrent[UTORRENT_TORRENT_UPSPEED];
$totdl = $totdl +$torrent[UTORRENT_TORRENT_DOWNSPEED];
echo " ul speed" . $torrent[UTORRENT_TORRENT_UPSPEED];
echo " dl speed" . $torrent[UTORRENT_TORRENT_DOWNSPEED] . " <br>";
}

if you havent test it and see if it says anything

and do you have utorrent on the server or at home?

if at home do you use a dynamisk dns or do you just put in the ip address in the details?

and test this api code in api.php instead then one you printed doesnt look like mine

<?php

define("UTORRENT_TORRENT_HASH", 0);
define("UTORRENT_TORRENT_STATUS", 1);
define("UTORRENT_TORRENT_NAME", 2);
define("UTORRENT_TORRENT_SIZE", 3);
define("UTORRENT_TORRENT_PROGRESS", 4);
define("UTORRENT_TORRENT_DOWNLOADED", 5);
define("UTORRENT_TORRENT_UPLOADED", 6);
define("UTORRENT_TORRENT_RATIO", 7);
define("UTORRENT_TORRENT_UPSPEED", 8);
define("UTORRENT_TORRENT_DOWNSPEED", 9);
define("UTORRENT_TORRENT_ETA", 10);
define("UTORRENT_TORRENT_LABEL", 11);
define("UTORRENT_TORRENT_PEERS_CONNECTED", 12);
define("UTORRENT_TORRENT_PEERS_SWARM", 13);
define("UTORRENT_TORRENT_SEEDS_CONNECTED", 14);
define("UTORRENT_TORRENT_SEEDS_SWARM", 15);
define("UTORRENT_TORRENT_AVAILABILITY", 16);
define("UTORRENT_TORRENT_QUEUE_POSITION", 17);
define("UTORRENT_TORRENT_REMAINING", 18);
define("UTORRENT_FILEPRIORITY_HIGH", 3);
define("UTORRENT_FILEPRIORITY_NORMAL", 2);
define("UTORRENT_FILEPRIORITY_LOW", 1);
define("UTORRENT_FILEPRIORITY_SKIP", 0);
define("UTORRENT_TYPE_INTEGER", 0);
define("UTORRENT_TYPE_BOOLEAN", 1);
define("UTORRENT_TYPE_STRING", 2);
define("UTORRENT_STATUS_STARTED", 1);
define("UTORRENT_STATUS_CHECKED", 2);
define("UTORRENT_STATUS_START_AFTER_CHECK", 4);

class uTorrent {
// class static variables
private static $base = "http://%s:%s/gui/%s";

// member variables
public $host;
public $port;
public $user;
public $pass;

// constructor
function __construct($host = "utorrent webui hostname/ip", $port = "port", $user = "admin", $pass = "pass") {
$this->host = $host;
$this->port = $port;
$this->user = $user;
$this->pass = $pass;
}

// performs request
private function makeRequest($request, $decode = true, $options = array()) {
$ch = curl_init();

curl_setopt_array($ch, $options);
curl_setopt($ch, CURLOPT_URL, sprintf(self::$base, $this->host, $this->port, $request));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $this->user.":".$this->pass);

$req = curl_exec($ch);
curl_close($ch);

return ($decode ? json_decode($req, true) : $req);
}

// implodes given parameter with glue, whether it is an array or not
private function paramImplode($glue, $param) {
return $glue.implode($glue, is_array($param) ? $param : array($param));
}

// returns the uTorrent build number
public function getBuild(){
$json = $this->makeRequest("?");
return $json['build'];
}

// returns an array of files for the specified torrent hash
// TODO:
// - (when implemented in API) allow multiple hashes to be specified
public function getFiles($hash) {
$json = $this->makeRequest("?action=getfiles&hash=".$hash);
return $json['files'];
}

// returns an array of all labels
public function getLabels(){
$json = $this->makeRequest("?list=1");
return $json['label'];
}

// returns an array of the properties for the specified torrent hash
// TODO:
// - (when implemented in API) allow multiple hashes to be specified
public function getProperties($hash) {
$json = $this->makeRequest("?action=getprops&hash=".$hash);
return $json['props'];
}

// returns an array of all settings
public function getSettings() {
$json = $this->makeRequest("?action=getsettings");
return $json['settings'];
}

// returns an array of all torrent jobs and related information
public function getTorrents() {
$json = $this->makeRequest("?list=1");
return $json['torrents'];
}

// returns true if WebUI server is online and enabled, false otherwise
public function is_online() {
return is_array($this->makeRequest("?"));
}

// sets the properties for the specified torrent hash
// TODO:
// - allow multiple hashes, properties, and values to be set simultaneously
public function setProperties($hash, $property, $value) {
$this->makeRequest("?action=setprops&hash=".$hash."&s=".$property."&v=".$value, false);
}

// sets the priorities for the specified files in the specified torrent hash
public function setPriority($hash, $files, $priority) {
$this->makeRequest("?action=setprio&hash=".$hash."&p=".$priority.$this->paramImplode("&f=", $files), false);
}

// sets the settings
// TODO:
// - allow multiple settings and values to be set simultaneously
public function setSetting($setting, $value) {
$this->makeRequest("?action=setsetting&s=".$setting."&v=".$value, false);
}

// add a file to the list
public function torrentAdd($filename, &$estring = false) {
$split = explode(":", $filename, 2);
if (count($split) > 1 && (stristr("|http|https|file|", "|".$split[0]."|") !== false)) {
$this->makeRequest("?action=add-url&s=".urlencode($filename), false);
}
elseif (file_exists($filename)) {
$json = $this->makeRequest("?action=add-file", true, array(CURLOPT_POSTFIELDS => array("torrent_file" => "@".realpath($filename))));

if (isset($json['error'])) {
if ($estring !== false) $estring = $json['error'];
return false;
}
return true;
}
else {
if ($estring !== false) $estring = "File doesn't exist!";
return false;
}
}

// force start the specified torrent hashes
public function torrentForceStart($hash) {
$this->makeRequest("?action=forcestart".$this->paramImplode("&hash=", $hash), false);
}

// pause the specified torrent hashes
public function torrentPause($hash) {
$this->makeRequest("?action=pause".$this->paramImplode("&hash=", $hash), false);
}

// recheck the specified torrent hashes
public function torrentRecheck($hash) {
$this->makeRequest("?action=recheck".$this->paramImplode("&hash=", $hash), false);
}

// start the specified torrent hashes
public function torrentStart($hash) {
$this->makeRequest("?action=start".$this->paramImplode("&hash=", $hash), false);
}

// stop the specified torrent hashes
public function torrentStop($hash) {
$this->makeRequest("?action=stop".$this->paramImplode("&hash=", $hash), false);
}

// remove the specified torrent hashes (and data, if $data is set to true)
public function torrentRemove($hash, $data = false) {
$this->makeRequest("?action=".($data ? "removedata" : "remove").$this->paramImplode("&hash=", $hash), false);
}
}



?>

put your details in the function construct

Link to comment
Share on other sites

Cheers yeah i'd already tried that - it's Ultima's simplified version of the API i think.. doesn't make any difference.

Also tried using my IP instead of my dynamic dns address. No change.

Now i'm gonna set WebUI up on one of the other computers on the LAN and see if that works.

Thanks so much for all your help so far by the way linux16 :)

EDIT: Oh, and i copied your simplified code above (with and without a "?>" on the end) and it didn't seem to make a difference.

Is it a problem that i haven't built and head/body/html tags yet? - I've just left it as is...

EDIT2: And yeh uTorrent is on this computer. I'll be back in 10 mins with results from the other PC on the LAN.. :)

Link to comment
Share on other sites

no need for head html tags

and just a little check your behind a router, right?

youve made sure the utorrent webui is forwarded correctly to your lan computer.

also have you checked that you can connect to webui from outside the lan (the web)

know this is quite simple errors but sometimes you can forget :P

Link to comment
Share on other sites

Yep i am behind a router; it's all forwarded properly though - my mate uses my WebUI from time to time too, so i'm pretty sure it works from outside the LAN...

I'm gonna set up WAMP on this computer so i'll be able to do everything with localhost. At least that'll narrow down the options..

Will get back to you later. Cheers :)

Link to comment
Share on other sites

im running wamp on the comp that runs utorrent so it'll be almost same config then

and in wamp i had to left click wamp -> php-> php extensions -> php_curl

cause it wasnt activated from install

have made a rar of all the php files needed and some examples here it is

php files

just uploaded them to your web server. Then edit utorrent with your ip:port:user:pass in the construct function

Link to comment
Share on other sites

Yeah! That works just fine over the LAN! That's a bit weird... I can't think of what to do next.

I've tried connecting to another computer on the LAN and that works too. I can get speeds just fine from any of them, with the php running on my computer.

I suppose it must be a firewall issue or something. I'll get my mate to host his WebUI and the PHP and see if that works. Otherwise i'll contact my host and see if they can make sense of it.

Thanks a lot for all the help, and having it working over LAN like this is still really handy.. :D

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...