forked from robin13/mediathekp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mediathek.pl
executable file
·348 lines (300 loc) · 11.7 KB
/
mediathek.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/usr/bin/perl
use strict;
use warnings;
use Mediathek;
use Getopt::Long;
use Log::Log4perl;
use YAML::Any qw/Dump LoadFile DumpFile/;
use Encode;
use File::Util;
use Data::Dumper;
$SIG{'INT'} = 'cleanup';
$SIG{'QUIT'} = 'cleanup';
my $args = {};
my $result = GetOptions (
$args,
'agent=s',
'cache_dir=s',
'cache_time=i',
'cookie_jar=s',
'timeout=i',
'flvstreamer=s',
'proxy=s',
'socks=s',
'config=s',
'test',
'tries=i',
# Filters
'channel=s',
'theme=s',
'title=s',
'date=s',
'id=i',
# Required for downloading
'target_dir=s',
# Actions: refresh_media, download, count, list,
# add_abo, del_abo, run_abo, list_abos
'action=s',
# Help
'help',
);
if( ! $result ){
die( "Illegal arguments...\n" );
}
if( $args->{help} ){
usage();
exit( 0 );
}
if( $args->{config} && -f $args->{config} ){
eval{
my $load_args = LoadFile( $args->{config} );
foreach( keys( %$load_args ) ){
$args->{$_} = $load_args->{$_};
}
};
if( $@ ){
die( "Could not load config from $args->{config}: $@\n" );
}
}
my $f = File::Util->new();
$args->{agent} ||= 'Mediathek-PL/0.2';
$args->{cache_dir} ||= '/tmp/mediathek_cache';
$args->{cache_time} ||= 0;
$args->{cookie_jar} ||= $args->{cache_dir} . $f->SL() . 'cookie_jar.dat';
if( ! -d $args->{cache_dir} ){
warn( "Need cache_dir to be defined and exist\n" );
usage();
exit;
}
Log::Log4perl->init( 'log.conf' );
my $logger = Log::Log4perl->get_logger();
$logger->debug( "Args:\n" . Dump( $args ) );
# Pass the memory usage monitor to Mediathek
my $media = Mediathek->new( $args );
if( $args->{action} ){
if( $args->{action} eq 'refresh_media' ){
# Refresh the media listing?
$media->refresh_media();
}elsif( $args->{action} eq 'download' ){
# Download videos
$media->get_videos( { channel => $args->{channel},
theme => $args->{theme},
title => $args->{title},
date => $args->{date},
media_id => $args->{id},
test => $args->{test},
} );
}elsif( $args->{action} eq 'count' ){
# Count the number of videos
my $count_videos = $media->count_videos( { channel => $args->{channel},
theme => $args->{theme},
title => $args->{title},
date => $args->{date},
media_id => $args->{id},
} );
print "Number of videos matching: $count_videos\n";
}elsif( $args->{action} eq 'list' ){
print list( $media );
}elsif( $args->{action} eq 'init_db' ){
$media->init_db();
}elsif( $args->{action} =~ /^add_abo,(\w+),(\d+)/ ){
$media->add_abo( { name => $1,
expires => $2,
channel => $args->{channel},
theme => $args->{theme},
title => $args->{title},
} );
}elsif( $args->{action} =~ /^del_abo,(\w+)/ ){
$media->del_abo( { name => $1 } );
}elsif( $args->{action} =~ /^run_abo,(\w+)/ ){
$media->run_abo( { name => $1 } );
}elsif( $args->{action} eq 'list_abos' ){
print list_abos( $media );
}elsif( $args->{action} eq 'list_downloads' ){
print list_downloads( $media );
}elsif( $args->{action} =~ /^del_download,(\d+)/ ){
$media->del_downloaded( { id => $1 } );
}else{
die( "Unknown action: $args->{action}" );
}
}
$logger->debug( "Just before natural exit" );
exit( 0 );
sub get_log_filename{
return $args->{cache_dir} . $f->SL() . 'debug.log';
}
sub cleanup{
my( $sig ) = @_;
$logger->warn( "Caught a SIG$sig--shutting down" );
exit( 0 );
}
sub list{
my( $media ) = @_;
my $list = $media->list({ channel => $args->{channel},
theme => $args->{theme},
title => $args->{title},
date => $args->{date},
media_id => $args->{id},
} );
if( ! $list or ! $list->{channels} ){
return "No matches found\n";
}
if( ! $list->{themes} ){
return list_channels( $list );
}elsif( ! $list->{media} and $list->{themes} ){
return list_themes( $list );
}elsif( $list->{media} and $list->{themes} ){
return list_titles( $list );
}else{
return "No suitable list to print...\n" . Dumper( $list ) . "\n";
}
}
sub list_channels{
my $list = shift;
my $fmt = ( ' ' x 4 ) . "%s\n";
my $rtn = sprintf( $fmt, 'Channel' );
$rtn .= sprintf( $fmt, '=======' );
foreach( sort ( values( %{ $list->{channels} } ) ) ){
$rtn .= sprintf $fmt, $_;
}
return $rtn;
}
sub list_themes{
my $list = shift;
# Find length of longest channel name
my $max_channel = length( "Channel" );
foreach( keys( %{ $list->{channels} } ) ){
if( ! $max_channel || length( $list->{channels}->{$_} ) > $max_channel ){
$max_channel = length( $list->{channels}->{$_} );
}
}
my $fmt = ( ' ' x 4 ) . '%-' . $max_channel . "s || %s\n";
my $rtn = sprintf( $fmt, 'Channel', 'Theme' );
$rtn .= sprintf( $fmt, '=======', '=====' );
foreach my $channel_id ( sort{ $list->{channels}->{$a} cmp $list->{channels}->{$b} }( keys( %{ $list->{channels} } ) ) ){
foreach my $theme_id ( sort{ $list->{themes}->{$a}->{theme} cmp $list->{themes}->{$b}->{theme} }( keys( %{ $list->{themes} } ) ) ){
$rtn .= sprintf( $fmt, $list->{channels}->{$channel_id}, $list->{themes}->{$theme_id}->{theme} );
}
}
return $rtn;
}
sub list_titles{
my $list = shift;
# Find length of longest channel name
my $max_channel = length( 'Channel' );
foreach( keys( %{ $list->{channels} } ) ){
if( ! $max_channel || length( $list->{channels}->{$_} ) > $max_channel ){
$max_channel = length( $list->{channels}->{$_} );
}
}
# Find length of longest theme
my $max_theme = length( 'Theme' );
foreach( keys( %{ $list->{themes} } ) ){
if( ! $max_theme || length( $list->{themes}->{$_}->{theme} ) > $max_theme ){
$max_theme = length( $list->{themes}->{$_}->{theme} );
}
}
my $fmt = ( ' ' x 4 ) . '%-5s || %-' . $max_channel . "s || %-" . $max_theme . "s || %-10s || %s\n";
my $rtn = sprintf( $fmt, 'ID', 'Channel', 'Theme', 'Date', 'Title' );
$rtn .= sprintf( $fmt, '==', '=======', '=====', '====', '=====' );
foreach my $channel_id ( sort{ $list->{channels}->{$a} cmp $list->{channels}->{$b} }( keys( %{ $list->{channels} } ) ) ){
foreach my $theme_id ( sort{ $list->{themes}->{$a}->{theme} cmp $list->{themes}->{$b}->{theme} }( keys( %{ $list->{themes} } ) ) ){
if( $list->{themes}->{$theme_id}->{channel_id} eq $channel_id ){
foreach my $media_id ( sort{ $list->{media}->{$a}->{title} cmp $list->{media}->{$b}->{title} }( keys( %{ $list->{media} } ) ) ){
if( $list->{media}->{$media_id}->{theme_id} eq $theme_id ){
$rtn .= sprintf( $fmt, $media_id, $list->{channels}->{$channel_id},
$list->{themes}->{$theme_id}->{theme}, $list->{media}->{$media_id}->{date},
$list->{media}->{$media_id}->{title} );
}
}
}
}
}
return $rtn;
}
sub list_abos{
my( $media ) = @_;
my @list = $media->get_abos();
my $rtn = "Abo name\n========\n";
for( @list ){
$rtn .= "@{$_}\n";
}
return $rtn;
}
sub list_downloads{
my( $media ) = @_;
my @list = $media->get_downloaded_media();
# find length of longest abo name
my $max_abo = length( 'Abo' );
foreach my $download ( @list ){
my $name_length = length( $download->{name} ) || 0;
if( $name_length > $max_abo ){
$max_abo = $name_length;
}
}
my $fmt = ( ' ' x 4 ) . '%-5s || %-' . $max_abo . "s || %-19s || %s\n";
my $rtn = sprintf( $fmt, 'ID', 'Abo', 'Download time', 'Path' );
$rtn .= sprintf( $fmt, '==', '===', '===================', '=====================' );
foreach my $row ( @list ) {
$rtn .= sprintf( $fmt, $row->{media_id}, $row->{name} || "N/A", $row->{time}, $row->{path} );
}
$rtn .= "\n" . scalar(@list) . " downloaded videos.\n\n";
return $rtn;
}
sub usage{
print qq{Usage:
./mediathek.pl [options]
Required Options:
--cache_dir Cache directory to keep downloaded XMLs, zip files and database
--target_dir If you use the action --download, where videos are downloaded to
Optional options
--agent User agent I should pretend to be. (Default Mediathek-PL/0.2)
--cache_time Time for which downloaded files should be cached. (Default: 0)
--cookie_jar Store your cookies somewhere else (Default in cache_dir)
--timeout Seconds timeout for flvstreamer. (Default: 10)
--flvstreamer Location of your flvstreamer binary (Default: /usr/bin/flvstreamer)
--proxy http Proxy to use (e.g. http://proxy:8080)
flvstreamer can only work through a socks proxy!
--socks Socks proxy to use for flvstreamer
--config Load settings from a config file:
you can put all the options listed here in a config file!
--tries The number of tries Video::Flvstreamer should make per video
There are often interruptions during a download, so a high number
like 50 is pretty safe. Default is 10
--help Print this help out
Action options (--action ACTION):
count Count number of videos matching your search
list List the videos matching your search
list_downloads List the videos previously downloaded
download Download the videos matching your search
del_download,\$i Deletes the video with id \$i from target_dir
add_abo,\$n,\$d Create a new abo with name \$n that expires after \$d days.
Specify search options (see below) to define the media
belonging to an abo. To keep downloads forever, specify 0 days
del_abo,\$n Delete an abo with name \$n
list_abos Shows all abo names
run_abo,\$name Runs the specified abo, downloading all media that is not
yet in target_dir and that has not expired yet. Checks also
if media has expired and removes it from the target
refresh_media Refresh your database from the internet
init_db (re)initialise your database (!!delete everything in DB!!)
Search options:
One or more search options can be given
!! WARNING !! If you use the action --download, and no search
options, you will download ALL the videos...
--channel Limit action to this channel
--theme Limit action to this theme
--title Limit action to this title
--date Limit action to the media entry with given air date.
Possible \$Modifiers are:
"<" earlier than the given date
">" later than the given date
"=" on the given date
The format is \$ModifierYYYY-MM-DD.
Example (double quotes are needed!):
--date ">2011-09-25"
--id Limit action to the media entry with this id
Search options can be explicit: Arte.DE
or contain wildcards: "Doku*"
};
}