diff options
author | Othmar Gsenger <otti@wirdorange.org> | 2011-03-31 19:05:23 +0000 |
---|---|---|
committer | Othmar Gsenger <otti@wirdorange.org> | 2011-03-31 19:05:23 +0000 |
commit | d26fa8c1e0886c813de5364ee6570c860c6ce12c (patch) | |
tree | 418120693100150b0443d3d880f945aaa45a0d22 /nzbget | |
parent | added support for browsing by category (diff) |
hopfully repaired this after backup loss
Diffstat (limited to 'nzbget')
-rw-r--r-- | nzbget/cancel.pm | 43 | ||||
-rw-r--r-- | nzbget/download.pm | 95 | ||||
-rw-r--r-- | nzbget/enqueue.pm | 59 | ||||
-rw-r--r-- | nzbget/list.pm | 49 | ||||
-rw-r--r-- | nzbget/log.pm | 29 | ||||
-rw-r--r-- | nzbget/main.pm | 56 | ||||
-rw-r--r-- | nzbget/menu.pm | 15 | ||||
-rw-r--r-- | nzbget/pre_enqueue.pm | 36 | ||||
-rw-r--r-- | nzbget/read.pm | 26 | ||||
-rw-r--r-- | nzbget/remove.pm | 31 | ||||
-rw-r--r-- | nzbget/search.pm | 56 | ||||
-rw-r--r-- | nzbget/search_upload.pm | 37 |
12 files changed, 532 insertions, 0 deletions
diff --git a/nzbget/cancel.pm b/nzbget/cancel.pm new file mode 100644 index 0000000..556cc22 --- /dev/null +++ b/nzbget/cancel.pm @@ -0,0 +1,43 @@ +package nzbget::cancel; + +use strict; +use Apache2::Const -compile => qw(OK REDIRECT); +use CGI qw(:standard); +use CGI::Carp 'fatalsToBrowser'; +use local::nzbget; +use local::db; +use local::user; +use File::Path; +use config; + +sub handler +{ + my $user = new local::user(%ENV); + my $del_id = param('id'); + + my ($download) = local::db::download->retrieve($del_id) or die 'Error in cancel'; + die "Not allowed" if not $download->owner->owner_id() eq $user->get_id(); + my $basedir = $config::config{files_dir} or die 'Missing files_dir in config'; + my $cli = new local::nzbget; + my $row = $cli->send_request('listgroups') or die "Can't connect to nubget"; + foreach my $value ( @$row) + { + my ($dl) = local::db::download->retrieve($value->{NZBNicename}) or die ' Error in groups'; + if ($value->{NZBNicename} == $del_id) + { + my $status = $cli->send_request('editqueue','GroupDelete',0,"",[$value->{LastID}]) or die "Can't connect to nzbget"; + sleep 1; + File::Path::rmtree($basedir.'/'.$dl->category->name.'/'.$dl->download_id); + $user->obj->quota_used($user->obj->quota_used - $dl->size); + $user->obj->update; + my @seens = local::db::seen->search(download=>$del_id); + map {$_->delete()} @seens; + $dl->delete(); + } + } + + print CGI::redirect('download'); + return Apache2::Const::REDIRECT; +} + +1; diff --git a/nzbget/download.pm b/nzbget/download.pm new file mode 100644 index 0000000..39394e7 --- /dev/null +++ b/nzbget/download.pm @@ -0,0 +1,95 @@ +package nzbget::download; +use strict; +use Apache2::Const -compile => qw(OK REDIRECT); +use CGI qw(:standard); +use CGI::Carp 'fatalsToBrowser'; +use local::nzbget; +use local::db; +use local::user; +use utf8; +#use CGI::Ajax; + +sub handler +{ + print header(); + print Show_HTML(); +# 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); + return Apache2::Const::OK; +} + +sub get_download_speed +{ + my $cli = new local::nzbget; + my $status = $cli->send_request('status') or die "Can't connect to nzbget"; + my $speed = int($status->{DownloadRate}/(1024*1024)); + return $speed?$speed.'MB/s':int($status->{DownloadRate}/1024).'KB/s'; +} + +sub get_download_table +{ + my $user = new local::user(%ENV); + my $html; + my $cli = new local::nzbget; + my $row = $cli->send_request('listgroups') or die "Can't connect to nzbget"; + $html.= CGI::start_table(); + $html.= Tr(th([qw/Kategorie Name Besitzer Größe(MB) Fortschritt Abbrechen/])); + foreach my $value ( @$row) + { + my ($download) = local::db::download->retrieve($value->{NZBNicename}) or die ' Error in groups'; + my $remaining=$value->{RemainingSizeMB}; + my $total=$value->{FileSizeMB}; + my $percent = int(100-100*$remaining/$total); + if (not $download->size) + { + $download->size($total); + $download->update(); + $user->obj->quota_used($user->obj->quota_used+$total); + $user->obj->update(); + } + my $cancel; + if ($download->processing == 1) + { + $html.=Tr(td([$download->category->name,$download->name,$download->owner->name,$total,'unpacking',''])); + } else { + $cancel=a({href=>'cancel?id='.$value->{NZBNicename}},'Abbrechen') if ($download->owner->owner_id() eq $user->get_id()); + $html.= Tr(td([$download->category->name,$download->name,$download->owner->name,$total,$percent.'%',$cancel])); + } + #foreach my $key (keys %$value) + #{ + # $html.= Tr(td([$key,$value->{$key}])); + #} + } + $html.= CGI::end_table(); + 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("window.location.reload();", 2000); + //--></script> +LALALA + ; +# $html.= <<LALALA +# <script type="text/javascript" language="JavaScript"><!-- +# window.setInterval("get_download_speed( [], ['speed'] ); get_download_table( [], ['dls'] );", 1000); +# //--></script> +#LALALA +# ; + $html.= table({class=>'nohigh'},Tr(td['download speed '. span({id=>'speed'},get_download_speed())]),Tr(td([' ']))); + $html.= div({id=>'dls'},get_download_table()); + + $html.= $menu->end_html(); + return $html +} + +1; diff --git a/nzbget/enqueue.pm b/nzbget/enqueue.pm new file mode 100644 index 0000000..e786f38 --- /dev/null +++ b/nzbget/enqueue.pm @@ -0,0 +1,59 @@ +package nzbget::enqueue; +use strict; +use Apache2::Const -compile => qw(OK REDIRECT); +use CGI qw(:standard); +use CGI::Carp 'fatalsToBrowser'; +use local::db; +use local::nzbget; +use utf8; +use LWP::Simple; + +use local::user; + +sub handler +{ + my $user = new local::user(%ENV); + my $nzbget = new local::nzbget; + die "Kein name angegeben" if not param('name'); + + die 'Kein freier Speicher' if $user->obj->quota_used>$user->obj->quoata; + + my $cat = local::db::category->retrieve(name=>param('cat')); + die "Ungültige category" if not $cat; + + my $category = $cat->name(); + + # my ($size) = param('description') =~ m/<b>(.*)<\/b>/; + my $size = 0; + my $nzb; + my $title = param('title'); + if (param('url')) + { + $nzb = get(param('url')); + die "Couldn't get ".param('url') unless $nzb; + } elsif ($title = param('file')) { + my $upload_filehandle = upload("file"); + while ( <$upload_filehandle> ) + { + $nzb.=$_; + } + } + die "Could not get nzb" if not $nzb; + use MIME::Base64; + $nzb = encode_base64($nzb); + my $download = local::db::download->insert({owner=>$user->get_id(),name=>param('name'),category=>$cat->category_id(),description=>$title,size=>$size}); + my $resp = $nzbget->send_request('append',RPC::XML::string->new($download->download_id()),RPC::XML::string->new($category),RPC::XML::boolean->new(0),RPC::XML::string->new($nzb)); + + if ($resp) + { + print CGI::redirect('download'); + return Apache2::Const::REDIRECT; + } else { + print header; + print start_html('nzbget enqueue error'); + print 'Error'; + print end_html(); + return Apache2::Const::OK; + } +} +1; diff --git a/nzbget/list.pm b/nzbget/list.pm new file mode 100644 index 0000000..fc30069 --- /dev/null +++ b/nzbget/list.pm @@ -0,0 +1,49 @@ +#!/usr/bin/perl +package nzbget::list; +use strict; +use Apache2::Const -compile => qw(OK REDIRECT); +use CGI qw(:standard); +use CGI::Carp 'fatalsToBrowser'; +use utf8; +use local::nzbget; +use local::db; +use local::user; +use local::menu; + + +sub handler +{ + my $user = new local::user(%ENV); + print header; + my $menu=new local::menu(%ENV); + print $menu->start_html('my downloads'); + my $module = $ENV{REQUEST_URI}; + $module =~ s'.*/''; + $module =~ s'\?.*''; + my $only_me = $module eq 'download_2' ? 1:0; + + print table({class=>'nohigh'},Tr(td({align=>'right'},['benutzter Speicherplatz '. $user->obj->quota_used().' / '.$user->obj->quoata().' MB']))) if ($only_me); + + print CGI::start_table(); + print Tr(th([qw/Neu Kategorie Name Größe Löschen /])); + my @downloads = local::db::download->search( + $only_me ? (owner=>$user->get_id()) : () , + param('category') ? (category => param('category')) : (), + completed=>1,{ order_by => 'time DESC'}); + foreach my $dl ( @downloads) + { + my $dl_remove; + $dl_remove = a({href=>'remove?id='.$dl->download_id},"löschen") if $dl->owner->owner_id == $user->get_id; + print Tr(td([ (local::db::seen->search(user=>$user->get_id(),download=>$dl->download_id))?'':'x', + a({href=>"$module?category=".$dl->category->category_id},$dl->category->name),a({title=>$dl->description,href=>'read?id='.$dl->download_id},$dl->name),$dl->size.'MB',$dl_remove + + ])); + } + ; + print CGI::end_table(); + + print $menu->end_html(); + return Apache2::Const::OK; +} + +1; diff --git a/nzbget/log.pm b/nzbget/log.pm new file mode 100644 index 0000000..05670ec --- /dev/null +++ b/nzbget/log.pm @@ -0,0 +1,29 @@ +package nzbget::log; +use strict; +use Apache2::Const -compile => qw(OK); +use local::nzbget; +use CGI qw(:standard); +use CGI::Carp 'fatalsToBrowser'; +use URI::Escape; + + +sub handler { + print header; + use local::menu; + my $menu=new local::menu(%ENV); + print $menu->start_html('log'); + + my $cli = new local::nzbget; + print CGI::start_table(); + my $resp = $cli->send_request('log',0,10) or die "Can't connect to nubget"; + die $resp if not ref $resp; + foreach my $row (@$resp) + { + print Tr(td[$row->{Text}]); + } + print CGI::end_table(); + print $menu->end_html; + return Apache2::Const::OK; +} + +1; diff --git a/nzbget/main.pm b/nzbget/main.pm new file mode 100644 index 0000000..1ff34c5 --- /dev/null +++ b/nzbget/main.pm @@ -0,0 +1,56 @@ +package nzbget::main; + +use strict; +use warnings FATAL => 'all'; +no warnings 'redefine'; + +use Apache2::RequestRec (); +use Apache2::RequestIO (); + +use Apache2::Const -compile => qw(OK); + +use nzbget::list; +use nzbget::download; +use nzbget::cancel; +use nzbget::enqueue; +use nzbget::log; +use nzbget::pre_enqueue; +use nzbget::read; +use nzbget::remove; +use nzbget::search; +use nzbget::menu; +use nzbget::search_upload; + +my %modules = +( + download => \&nzbget::download::handler, + download_1 => \&nzbget::list::handler, + download_2 => \&nzbget::list::handler, + cancel=> \&nzbget::cancel::handler, + enqueue=> \&nzbget::enqueue::handler, + log=> \&nzbget::log::handler, + pre_enqueue=> \&nzbget::pre_enqueue::handler, + read=> \&nzbget::read::handler, + remove=> \&nzbget::remove::handler, + search=> \&nzbget::search::handler, + search_1=> \&nzbget::search_upload::handler, + menu=> \&nzbget::menu::handler, +); + + +sub handler { + my $r = shift; + + my $module = $ENV{REQUEST_URI}; + $module =~ s'.*/''; + $module =~ s'\?.*''; + if ($modules{$module}) + { + return $modules{$module}->($r); + } else { + $r->content_type('text/plain'); + print "Unknown action $module ($ENV{REQUEST_URI})\n"; + return Apache2::Const::OK; + } +} +1; diff --git a/nzbget/menu.pm b/nzbget/menu.pm new file mode 100644 index 0000000..e368d21 --- /dev/null +++ b/nzbget/menu.pm @@ -0,0 +1,15 @@ +package nzbget::menu; +use strict; +use Apache2::Const -compile => qw(OK REDIRECT); +use CGI qw(:standard); +use CGI::Carp 'fatalsToBrowser'; +use local::menu; + +sub handler { + print header; + my $menu= new local::menu(%ENV); + print $menu->start_html('nzbget status'); + print $menu->end_html; + return Apache2::Const::OK; +} +1; diff --git a/nzbget/pre_enqueue.pm b/nzbget/pre_enqueue.pm new file mode 100644 index 0000000..dda8967 --- /dev/null +++ b/nzbget/pre_enqueue.pm @@ -0,0 +1,36 @@ +package nzbget::pre_enqueue; +use strict; +use Apache2::Const -compile => qw(OK); +use CGI qw(:standard); +use CGI::Carp 'fatalsToBrowser'; +use local::db; +use local::menu; + +sub handler +{ + my @cat = local::db::category->retrieve_all(); + die "Ungültige category" if not @cat; + + print header; + my $menu = new local::menu(%ENV); + print $menu->start_html('nzbget pre_enqueue'); + print CGI::start_form(-action=>'enqueue'); + print table({class=>'nohigh'}, + Tr(td(param('title'))), + Tr(td(param('description'))), + ), + table({class=>'nohigh'}, + Tr(td(['name',textfield('name').hidden('description',param('description')).hidden('url',param('url')).hidden('title',param('title'))])), + Tr(td(['category', + CGI::popup_menu( + -name=>'cat', + -values=>[0,map {$_->name()} @cat], + -labels => {0=>'---', map { ($_->category_id() => $_->name() ) } @cat} + )])), + Tr(td([' ',submit("Download starten")])), + ); + print CGI::end_form; + print $menu->end_html(); + return Apache2::Const::OK; +} +1; diff --git a/nzbget/read.pm b/nzbget/read.pm new file mode 100644 index 0000000..3cc30ec --- /dev/null +++ b/nzbget/read.pm @@ -0,0 +1,26 @@ +package nzbget::read; +use strict; +use Apache2::Const -compile => qw(OK REDIRECT); +use CGI qw(:standard); +use CGI::Carp 'fatalsToBrowser'; +use utf8; +use local::db; +use local::user; +use config; + + +sub handler +{ + my $user = new local::user(%ENV); + + my $dl = local::db::download->retrieve(param('id')) or die "Ungültige download id"; + + if (not local::db::seen->search(user=>$user->get_id,download=>$dl->download_id)) + { + local::db::seen->insert({user=>$user->get_id,download=>$dl->download_id}); + } + + print CGI::redirect($config::config{web_dir}.'/'.$dl->category->name.'/'.$dl->download_id); + return Apache2::Const::REDIRECT; +} +1; diff --git a/nzbget/remove.pm b/nzbget/remove.pm new file mode 100644 index 0000000..71334bd --- /dev/null +++ b/nzbget/remove.pm @@ -0,0 +1,31 @@ +package nzbget::remove; +use strict; +use Apache2::Const -compile => qw(OK REDIRECT); +use CGI qw(:standard); +use CGI::Carp 'fatalsToBrowser'; +use local::db; +use local::user; +use File::Path; +use config; + + +sub handler +{ + my $user = new local::user(%ENV); + my $del_id = param('id'); + + my ($dl) = local::db::download->retrieve($del_id) or die 'Error in cancel'; + die "Not allowed" if not $dl->owner->owner_id() eq $user->get_id(); + + my $basedir = $config::config{files_dir} or die 'Missing files_dir in config'; + File::Path::rmtree($basedir.'/'.$dl->category->name.'/'.$dl->download_id); + $user->obj->quota_used($user->obj->quota_used - $dl->size); + $user->obj->update; + my @seens = local::db::seen->search(download=>$del_id); + map {$_->delete()} @seens; + $dl->delete(); + + print CGI::redirect('download_2'); + return Apache2::Const::REDIRECT; +} +1; diff --git a/nzbget/search.pm b/nzbget/search.pm new file mode 100644 index 0000000..e1c181d --- /dev/null +++ b/nzbget/search.pm @@ -0,0 +1,56 @@ +package nzbget::search; +use strict; +use Apache2::Const -compile => qw(OK); +use CGI qw(:standard); +use CGI::Carp 'fatalsToBrowser'; +use URI::Escape; +use local::params; +use local::nzbindex; +use local::menu; + +sub handler +{ + print header; + my $menu= new local::menu(%ENV); + print $menu->start_html('nzbget search'); + + print CGI::start_table(); + + print CGI::start_form(-action=>'search'); + print + table({class=>'nohigh'}, + Tr(td(['Search',textfield({size=>100,name=>'q'}),])), + Tr(td(['min Size',textfield({size=>5,name=>'minsize'}).' MB'])), + Tr(td(['max Size',textfield({size=>5, name=>'maxsize'}).' MB'.hidden('more',1)])), + Tr(td(['results',CGI::popup_menu( + -name => 'max', + -values => ['25','100','500'], + -default => '25', + -labels => {25=>'25',100=>'100',500=>500} + ) ])), + Tr(td(['',submit()])), + Tr(td([qw/ /])), + ); + print CGI::end_form; + my $param_obj=new local::params; + if ($param_obj->has_params()) + { + my $nzbindex=new local::nzbindex; + my @search = $nzbindex->search($param_obj->get_as_hash()); + if (@search) + { + print CGI::start_table(); + foreach my $item (@search) { + print Tr(td([ + a({href=>"pre_enqueue?url=".uri_escape_utf8($item->{url})."&name=".uri_escape_utf8(param('q'))."&description=".uri_escape_utf8($item->{description})."&title=".uri_escape_utf8($item->{title})},$item->{title}).' '. + $item->{description}])); + } + print CGI::end_table(); + } else { + print hr().'nothing found'; + } + } + print $menu->end_html; + return Apache2::Const::OK; +} +1; diff --git a/nzbget/search_upload.pm b/nzbget/search_upload.pm new file mode 100644 index 0000000..de3e10f --- /dev/null +++ b/nzbget/search_upload.pm @@ -0,0 +1,37 @@ +package nzbget::search_upload; +use strict; +use Apache2::Const -compile => qw(OK); +use CGI qw(:standard); +use CGI::Carp 'fatalsToBrowser'; +use local::db; +use local::menu; + +sub handler +{ + my @cat = local::db::category->retrieve_all(); + die "Ungültige category" if not @cat; + + print header; + my $menu = new local::menu(%ENV); + print $menu->start_html('nzbget pre_enqueue'); + print CGI::start_form(-action=>'enqueue'); + print table({class=>'nohigh'}, + Tr(td(param('title'))), + Tr(td(param('description'))), + ), + table({class=>'nohigh'}, + Tr(td(['nzb file',filefield('file')])), + Tr(td(['name',textfield('name')])), + Tr(td(['category', + CGI::popup_menu( + -name=>'cat', + -values=>[0,map {$_->name()} @cat], + -labels => {0=>'---', map { ($_->category_id() => $_->name() ) } @cat} + )])), + Tr(td([' ',submit("Download starten")])), + ); + print CGI::end_form; + print $menu->end_html(); + return Apache2::Const::OK; +} +1; |