-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfeed-torrents-to-transmission.pl
executable file
·154 lines (115 loc) · 3.17 KB
/
feed-torrents-to-transmission.pl
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
#!/usr/bin/env perl
#
# This program should only be ran once in a day, presumably from a crontab.
# It is the best to run in in at 00:01.
#
use v5.14;
use encoding 'utf8';
package TransmissionFeeder;
use Moose;
use Moose::Util::TypeConstraints;
use XML::Feed;
use Try::Tiny;
use Transmission::Client;
use URI;
use DateTime;
subtype 'TransmissionFeeder::Feed' => as class_type 'XML::Feed';
coerce 'TransmissionFeeder::Feed' => from 'Str' => via { XML::Feed->parse(URI->new($_)) };
has feed => (
is => "rw",
isa => "TransmissionFeeder::Feed",
required => 1,
coerce => 1
);
has transmission_client => (
is => "rw",
isa => "Transmission::Client",
builder => '_build_transmission_client',
lazy => 1
);
has options => (
is => "rw",
isa => "HashRef"
);
sub run {
my $self = shift;
my $today = DateTime->today;
my @feeds;
for my $entry ($self->feed->entries) {
my $delta = $today - $entry->issued->truncate(to => 'day');
if ($self->options->{'only-today'}) {
if ( $delta->days == 0) {
push @feeds, [$entry->title, $entry->content->body];
}
}
else {
push @feeds, [$entry->title, $entry->content->body];
}
}
for my $pattern (@{ $self->options->{only} ||[] }) {
@feeds = grep { $_->[0] =~ /$pattern/ } @feeds;
}
for my $pattern (@{ $self->options->{exclude} ||[] }) {
@feeds = grep { $_->[0] !~ /$pattern/ } @feeds;
}
for (@feeds) {
if ($self->options->{'dont-run'}) {
say "Add: " . $_->[0];
}
else {
try {
$self->transmission_client->add(filename => $_->[1]);
}
}
}
}
sub _build_transmission_client {
my $self = shift;
my %opts = ( autodie => 1 );
for (grep { defined($_->[1]) } map { [$_, $self->options->{$_}] } qw(url username password)) {
$opts{$_->[0]} = $_->[1];
}
return Transmission::Client->new(%opts);
}
package main;
use utf8;
use Getopt::Long;
use Pod::Usage;
utf8::decode($_) for @ARGV;
my %options;
GetOptions(
\%options,
'dont-run',
'url=s',
'username=s',
'password=s',
'only=s@',
'exclude=s@',
'only-today',
);
my $feed_url = shift @ARGV or pod2usage({ -verbose => 1, -exitval => 1 });
my $feeder = TransmissionFeeder->new(
options => \%options,
feed => $feed_url
);
$feeder->run;
__END__
=head1 SYNOPSIS
feed-torrents-to-transmission.pl [options] <URL>
--dont-run
Do not really add torrents into Transmission. Print their name instead.
--url <url>
Transmission RPC URL
--username <xxx>
Transmission RPC Username
--password <yyy>
Transmission RPC Password
--only <regex>
A regex (without the surrounding //) to match against feed entry names to
narrow down the items to feed to transmission. This option may be given
multiple times. Feed entry names matching all of the regexes are fed to
transmission.
--exclude <regex>
A regex (without the surrounding //) to match against feed entry names to
exclude. This option can be given multiple times. Feed entry names matching
any one of the regexes are excluded.