summaryrefslogtreecommitdiff
path: root/groups.pl
blob: cf5a0295d5b6397db404484c36ecba6089dd396f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/perl

use strict;
use CGI qw(:standard);
use CGI::Carp 'fatalsToBrowser';
use local::nzbget;
use local::db;

use CGI::Ajax;

my $cgi = new CGI;
my $pjx = new CGI::Ajax( 'get_download_speed' => \&get_download_speed,
                          'get_download_table' => \&get_download_table
                        );
print $pjx->build_html( $cgi, \&Show_HTML);

sub get_download_speed
{
  my $cli = new local::nzbget;
  my $status = $cli->send_request('status') or die "Can't connect to nubget"; 
  return int($status->{DownloadRate}->value/(1024*1024)).'MB/s';   
}

sub get_download_table
{
  my $html;
  my $cli = new local::nzbget;
  my $row = $cli->send_request('listgroups') or die "Can't connect to nubget";
  foreach my $value ( @$row)
  {
    my ($download) = local::db::download->retrieve($value->{NZBNicename}->value) or die ' Error in groups';
    my $remaining=$value->{RemainingSizeMB}->value;
    my $total=$value->{FileSizeMB}->value;
    my $percent = int(100-100*$remaining/$total);
    $download->size($total);
    $download->update();
    $html.= Tr(td([$download->category->name,$download->name,$download->owner->name,$percent.'%']));
    #foreach my $key (keys %$value)
    #{
    # $html.= Tr(td([$key,$value->{$key}->value]));
    #} 
  }
  return $html;
}

sub Show_HTML
{
  use local::menu;
  my $menu= new local::menu(%ENV);
  my $html= $menu->start_html('nzbget status');

  $html.= <<LALALA
  <script type="text/javascript" language="JavaScript"><!--
    window.setInterval("get_download_speed( [], ['speed'] );", 2000);
    window.setInterval("get_download_table( [], ['dltable'] );", 2000);
    //--></script> 
LALALA
  ;
  $html.= hr(); 
  $html.= CGI::start_table(); 
  $html.= Tr(td['download speed', div({id=>'speed'},get_download_speed())]);   
  $html.= CGI::end_table(); 
  $html.= hr(); 

  $html.= CGI::start_table(id=>'dltable');
  $html.=  get_download_table();
  $html.= CGI::end_table();

  $html.= $menu->end_html();
  return $html
}