All pastes #1706548 Raw Edit

torrentPrune - Deltas

public text v1 · immutable
#1706548 ·published 2009-12-08 06:49 UTC
rendered paste body
#!/usr/bin/perl -w
use strict;
use warnings;
use Text::CSV;
use open ':encoding(utf8)';
use utf8;
my $cmd = "remote -l|sort -hr|sed -n '3 s/\\([0-9][0-9][0-9]\\).*/\\1/p'|sed -e 's/^\\s//'"; 
#print "$cmd\n";
my $maximum = qx{$cmd} || die "Couldn't obtain number of torrents";
chomp($maximum);
my @fields = ("Hash:", "Downloaded:", "Uploaded:", "Tracker knows of");
for (1..$maximum) {
	my @output = ();
	open DATA, "remote -t$_ -i |" or die "Couln't execute program\n";
	while ( defined( my $line = <DATA> ) ) {
		chomp($line);
		foreach my $field(@fields) {
			my $out = &meta($line,$field);
			if ($out) {push (@output,$out);}
		}
	}
	close DATA;
	next unless @output;
	my $hash = $output[0];
	my $downloaded = $output[1];
	my $uploaded = $output[2];
	my $seedersleechers = $output[3];

	# Get time
	my $date = localtime();

	# Convert Downloaded to KB
	my @have = &sep($downloaded);
	my $size = $have[0];
	if ($size ~~ "None") {
		$size = 0.0;
	} else {
		my $unit = $have[1];
		if ( $unit ~~ (/MB/) ) { $size *= 1024; } 
		elsif ( $unit ~~ (/GB/) ) {$size *= 1024 *1024} 
	}
	$output[1] = $size ;

	# Convert Uploaded to KB
	@have = &sep($uploaded);
	$size = $have[0];
	if ($size ~~ "None") {
		$size = 0.0;
	} else {
		my $unit = $have[1];
		if ( $unit ~~ (/MB/) ) { $size *= 1024; } 
		elsif ( $unit ~~ (/GB/) ) {$size *= 1024 *1024} 
	}
	$output[2] = $size ;

	#Convert Date
	# Split date into fields
	my @date = &sep($date);
	my $month = $date[1];
	#Convert alpha month to numerical
	my %mon2num = qw(
	jan 01  feb 02  mar 03  apr 04  may 05  jun 06
	jul 07  aug 08  sep 09  oct 10 nov 11 dec 12
	);
	$month = $mon2num{ lc substr($month, 0, 3) };
	my $day = $date[2];
	if ($day < 10) {
		$day = "0" . $day;
	}
	my $time = $date[3];
	my $year = $date[4];
	$output[4] = $year . "/" . $month . "/" . $day . " " . $time ;

	# Split seeders and leechers
	my @seedandleech = &sep($seedersleechers);
	my $seeders = $seedandleech[0];
	my $leechers = $seedandleech[3];
	$output[3] = $seeders;
	push(@output,$leechers);

	my $csv = Text::CSV->new();
	$csv->sep_char(";");
	$csv->escape_char("\\");
	my $status = $csv->combine(@output);
	my $line = $csv->string();
	print $line . "\n";
	#print "@output" . "\n";
}
sub meta {
	my $line = $_[0];
	my $search = $_[1];
	my $m = undef;
	if ($line =~ s/$search//) {
		$line =~ s/^\s\s*//;
		$m = $line ;
	}
	return $m;
}

sub sep {
	#my $sep = "$_[0]";
	my $input = "$_[0]";
	my @fields = split /\s\s*/, $input;
	@fields;
}