-
Notifications
You must be signed in to change notification settings - Fork 0
/
trrr
188 lines (129 loc) · 4.31 KB
/
trrr
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env perl
use 5.010;
use strict;
=head1 NAME
trrr - search torrents
=cut
use POSIX qw< ceil >;
use Encode qw< encode >;
use Term::ANSIColor;
use App::Trrr qw< open_app >;
use App::Trrr::KAT qw< kat >;
use App::Trrr::TPB qw< tpb >;
use App::Trrr::Clipboard qw< clip >;
if( $ARGV[0] eq '-h' ){ system("perldoc trrr"); exit }
my @keyword = ();
if($ARGV[0] eq undef or $ARGV[0] eq '-P'){
push @keyword, split(" ", clip());
for(@keyword){ s/\s*//g }
#print "$_" for @keyword;
}
sub api {
my $a = shift || "kat";
my %api = (
tpb => sub { tpb(@_) },
kat => sub { kat(@_) },
);
return $api{$a};
}
my $opt = {
os => "$^O",
seeds => 1,
api => "kat",
};
for(@ARGV){
if(/^\-/){
s/\-//;
if(/[a-z]/){ $opt->{key} = lc $_ }
if(/[0-9]+/){ $opt->{seeds} = int $_ }
if(/[P]/){ $opt->{api} = "tpb" }
} else { my $keyword = lc $_; push @keyword, $keyword }
}
my $strip = sub {
my( $item, $field ) = @_;
my $term_width = int `tput cols`;
my $line = $item->{key} . ' ' . $item->{seeds} . ' ' . "$item->{title}" . ' ' . $item->{category} . ' ' . $item->{size};
my $max = $term_width - length($line);
if( $term_width < length($line)){
my $strip = length($line) - $term_width;
my $stripped_title = $term_width - $strip + 0;
$item->{title} = substr( $item->{title},0,$max);
}
my $title = $item->{title};
my $striped = {};
$striped->{title} = $title;
return $striped->{$field}
};
sub show {
my $key = 'A';
my( $key_color ) = ();
my $i = 1;
my $api = api($opt->{api});
my @filter = grep { int($_->{seeds}) >= int($opt->{seeds}) } @{ $api->(\@keyword) };
@filter = sort { $b->{seeds} <=> $a->{seeds} } @filter;
my @f = splice(@filter,0,15);
unless(@f){ say colored(['yellow'], 'no results') and exit }
for(@f){
if( $i % 2 ){ $key_color = 'black on_white' } else { $key_color = 'white on_black' }
$_->{key} = $key;
my $title = $strip->($_,'title');
say colored([$key_color],$key) . ' ' . colored(['cyan'],$_->{seeds}) . ' ' . colored(['yellow'],$title) . ' ' . colored(['grey8'],$_->{category}) . ' ' . colored(['bold'],$_->{size}) if defined $_->{key};
$key++; $i++;
}
key(\@f);
}
sub key {
my $filter = shift;
get_torrent($filter) if defined $opt->{key};
if( $opt->{os} eq 'MSWin32' or $opt->{os} eq 'msys' ){
exit unless defined $opt->{key};
} else {
exit if defined $opt->{key};
say colored(['blink'],'^') . ' ' . colored(['grey5 on_grey15'],'P') . colored(['grey15 on_grey5'],'RESS') . colored(['grey5 on_grey15'],'K') . colored(['grey15 on_grey5'],'EY');
require App::Trrr::HotKey;
App::Trrr::HotKey->import( 'readkey' ) ;
$opt->{key} = readkey();
get_torrent($filter);
}
}
sub get_torrent {
my $filter = shift;
my( $picked ) = grep { $_->{key} eq uc $opt->{key} } @$filter;
open_app("$picked->{magnet}") if $opt->{key} =~ /[a-o]/;
}
show();
=head1 DESCRIPTION
CLI tool to search torrents. Results are sorted by number of seeders and each is mapped to key. Pressing the key with assigned letter will open magnet link in your default client. On iOS, magnet link is placed into clipboard.
=head1 USAGE
Search with as many parameters as needed. Uses KAT by default, C<-P> will switch to TPB.
=over 10
C<trrr keyword1 keyword2 keywordN>
C<trrr keyword1 keyword2 keywordN -P>
=back
_
On Linux, start it without any parameter and it'll use clipboard content as keywords. ( needs 'xclip' or 'xsel' to be installed )
=over 10
C<trrr>
=back
_
Limit results which have at least 100 seeders.
=over 10
C<trrr keyword1 keyword2 keywordN -100>
=back
_
To get another torrent from previous search add key as parameter. This is mandatory on Windows running 'Git/Bash for Windows' where you have to specify key on CLI upfront.
=over 10
C<trrr keyword1 keyword2 keywordN -b>
=back
_
See this perdoc.
=over 10
C<trrr -h>
=back
=head1 AUTHOR
Zdenek Bohunek. <[email protected]>
App::Trr::HotKey is taken from StackOverflow post by brian d foy
=head1 COPYRIGHT AND LICENSE
Copyright 2016 by Zdenek Bohunek
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
=cut