Bounty: Torrent Info

lisas4567

Member
May 25, 2018
773
0
16
Willing to pay $20us.

Im after a php script to be able to be run and copy all the current torrent informtion in to an array to be be stored.

Essentially im after a scrape of all the torrent information from rtorrent into a php array.

So if I have 20 torrents I would have all this infomation stored in an array organised along the line of name, size, speed, Label etc

Will add an extra $10 to the bounty if some one can comment the code so I know how to follow it for my own learning experience. ( I have looked at xmlrpc.php and can not find a simple way to run a php command to get the information I want)

what I will use this code for in the end is adding up how many gb of data each lablel of hardrive space is used up
 

simur612

Member
May 25, 2018
879
0
16
1) Is you really need a *php* script for this? For example, all that work may be done by javascript. If that javascript will be running inside ruTorrent.
2) Is this php scrip must be a standalone or it may use ruTorrent scripts? If first - this require a passing a lot of configuration parameters.
 

randac56

Member
May 25, 2018
915
0
16
Would like the script to be php, I dont know how to program Javascript. This script will be the building block I need to make some addons to my seedbox.

Does not need to be standalone script so can include any files already in rutorrent (please comment each include so i know whats happening)
 

peshua19

Member
May 25, 2018
897
0
16
File must be placed in rutorrent/php.
Function for get pairs label/size:
Code:
<?php

require_once( &#39;xmlrpc.php&#39; );

function getTorrentsSize()
{
$req = new rXMLRPCRequest(
new rXMLRPCCommand( "d.multicall", array( "", "d.get_custom1=", "d.get_size_bytes=" ))
);
if($req->success())
{
$ret = array();
for($i=0; $i<count($req->val); $i+=2)
{
$label = rawurldecode($req->val[$i]);
$size = floatval($req->val[$i+1]);
if(array_key_exists($label,$ret))
$ret[$label] += $size;
else
$ret[$label] = $size;
}
return($ret);
}
return(false);
}
?>
Result on my system:
Code:
Array
(
[Video] => 116129418177
[Music] => 1906464616
[Games] => 25924406313
[] => 6077354731
)
Function for get all torrents and store some it's parameters to array:
Code: [Select]
<?php

require_once( $rootPath.&#39;xmlrpc.php&#39; );

function getTorrentsInfo()
{
$req = new rXMLRPCRequest(
new rXMLRPCCommand( "d.multicall", array( "", "d.get_hash=", "d.get_up_rate=", "d.get_down_rate=", "d.get_name=", "d.get_custom1=", "d.get_size_bytes=" ))
);
if($req->success())
{
$ret = array();
for($i=0; $i<count($req->val); $i+=6)
{
$ret[] = array( "hash"=>$req->val[$i],
"speedUL"=>$req->val[$i+1],
"speedDL"=>$req->val[$i+2],
"name"=>$req->val[$i+3],
"label" => rawurldecode($req->val[$i+4]),
"size" => floatval($req->val[$i+5]));
}
return($ret);
}
return(false);
}
?>
Quote
if some one can comment the code so I know how to follow it for my own learning experience.​

I'm not teacher. I think, code above is very easy for understand.
If you need information about rtorrent xmlrpc API you can found it, for example, here - http://code.google.com/p/gi-torrent/wiki/rTorrent_XMLRPC_reference
 

lisas4567

Member
May 25, 2018
773
0
16
This is exactly the type of script I was looking for and very easy to follow thankyou very much.

Sponge

Do I just send the bounty to the donation link ?
 

randac56

Member
May 25, 2018
915
0
16
Sent $20 I know you said just $5 but was willing to spend the $20 so call the other $15 a General Donation if you like
 

peshua19

Member
May 25, 2018
897
0
16
I was wondering if you can call the url from rtorrent as well tried a few variations to the code above using

Code:
$req2 = new rXMLRPCRequest( array(
new rXMLRPCCommand("t.get.url")));
and tried

Code:
$req2 = new rXMLRPCRequest(
new rXMLRPCCommand( "t.multicall", array( "", "t.get_url=")));
then tried to read the data into the array with

Code:
"tracker"=>$req2->val[$i]);
This does not seem to work any pointers.
 

lisas4567

Member
May 25, 2018
773
0
16
Ok Played an awfull lot with the code and came up with a solution

Code:
function getTorrentsInfo()
{
$req = new rXMLRPCRequest(
new rXMLRPCCommand( "d.multicall", array( "", "d.get_hash=", "d.get_custom1=", "d.get_name=", "d.get_up_total=", "d.get_down_total=", "d.get_size_bytes=", "d.get_custom=addtime", "d.get_ratio=" )));
if($req->success())
{
$ret = array();
for($i=0; $i<count($req->val); $i+=8)
{
$ret[] = array( "hash"=>$req->val[$i],
"label"=>$req->val[$i+1],
"name"=>$req->val[$i+2],
"UL"=>$req->val[$i+3], this in bytes size use a function to convert it to a more readable format eg mb/gb
"DL"=>$req->val[$i+4], this in bytes size use a function to convert it to a more readable format eg
"size" => floatval($req->val[$i+5]), this in bytes size use a function to convert it to a more readable format
"addtime"=>$req->val[$i+6], // needs to be coverted to date time format to get readable added time date
"ratio"=> intval($req->val[$i+7]), // array has to be divided by 1000 to get correct ratio
"tracker"=>tracker($req->val[$i]));
}
return($ret);
}
return(false);
}
Function needed for the tracker (I did not write this function just used some code from an addon and changed it slightly)

Code:
function tracker($key)
{
$trackers = array();
$req = new rXMLRPCRequest( array(
new rXMLRPCCommand("t.multicall",
array($key,"","t.is_enabled=","t.get_type=","t.get_group=","t.get_url="))));
$req->setParseByTypes();
if($req->run() && !$req->fault)
{
$lastGroup = 65535;
for($i = 0; $i<count($req->strings); $i++)
{
if($req->i8s[$i*3+2]>$lastGroup)
break;
if(($req->i8s[$i*3]!=0) && ($req->i8s[$i*3+1]<3))
{
$lastGroup = $req->i8s[$i*3+2];
$domain = parse_url($req->strings[$i],PHP_URL_HOST);
if(preg_match("/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/",$domain)!=1)
{
$pos = strpos($domain,'.');
if($pos!==false)
{
$tmp = substr($domain,$pos+1);
if(strpos($tmp,'.')!==false)
$domain = $tmp;
}
}
if(array_key_exists($domain,$trackers))
{
$trackers[$domain][0]+=$needTorrents[$key][0];
$trackers[$domain][1]+=$needTorrents[$key][1];
}
else
$trackers[$domain] = $needTorrents[$key];
}
}
}
return $domain;
}
Now when you call

Code: [Select]
$result=getTorrentsInfo();
Your given all your torrents each one in it own array($result) with hash,label,name,UL(uploaded),DL(downloaded),size,ratio,addtime and tracker

To view this information nicely on screen add this function

Code:
function print_r2($val){
echo '<pre>';
print_r($val);
echo '</pre>';
}
then

Code:
print_r2($result);
Now that you have all your torrent information in a nice easy array you can do with it as you please

I'm Filtering the array to just have a list of torrents with a certain label by adding this function

Code:
function filter_by_value ($array, $index, $value){
if(is_array($array) && count($array)>0)
{
foreach(array_keys($array) as $key){
$temp[$key] = $array[$key][$index];

if ($temp[$key] == $value){
$newarray[$key] = $array[$key];
}
}
}
return $newarray;
}
And then calling the function with

Code:
$filter="movies"; // this could be anything you want from how you have your labels set up on your site
$nResults = filter_by_value($result, 'label', $filter);
Now I found the array was not numbered corectly so to fix that issue

Code:
$n2Results = array_merge($nResults);
To print out the new sorted array
Code:
print_r2($n2Results);
From here I have created a databse and added all the information into it, I have the database check every 5 mins and either insert or upadte the data. I can/have used that database information to create lots of other pages