lib/Video/PlaybackMachine/DirectoryScanner.pm untested

filter: Statementstmt 0.0% 0 / 95 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 122 CC 3 SCAR 12.8
CC
3
Cov
0%
scan
12.0
_build_xine
2.0
_build_stream
2.0
CRAP 3.6
1 package Video::PlaybackMachine::DirectoryScanner;
2  
3 our $VERSION = '0.09'; # VERSION
4  
5 use Moo;
6  
7 use Types::Standard qw(ArrayRef Str);
8  
9 use Video::Xine;
10 use Video::Xine::Stream 'XINE_META_INFO_TITLE';
11  
12 use Video::PlaybackMachine::Schema;
13 use Video::PlaybackMachine::DB;
14  
15 use File::Find::Rule;
16 use File::Basename;
17 use List::MoreUtils 'uniq';
18 use POSIX 'ceil';
19  
20 has 'directories' => (
21 'is' => 'ro',
22 'isa' => ArrayRef,
23 'required' => 1
24 );
25  
26 has 'schedule_name' => (
27 'is' => 'ro',
28 'isa' => Str,
29 'default' => 'Test Schedule'
30 );
31  
32 has 'xine' => ( 'is' => 'lazy', );
33  
34 has 'stream' => ( 'is' => 'lazy', );
35  
36 has 'schema' => ( 'is' => 'lazy' );
37  
38 has 'suffixes' => (
39 'is' => 'lazy',
40 'isa' => ArrayRef
41 );
42  
43 sub _build_xine {
44 my $self = shift;
45  
46 return Video::Xine->new();
47 }
48  
49 sub _build_stream {
50 my $self = shift;
51  
52 my $xine = $self->xine();
53  
54 my $vo = Video::Xine::Driver::Video->new( $xine, 'none' );
55 my $ao = Video::Xine::Driver::Audio->new( $xine, 'none' );
56  
57 return $xine->stream_new( $ao, $vo );
58 }
59  
60 sub _build_schema {
61 my $self = shift;
62  
63 return Video::PlaybackMachine::DB->schema();
64 }
65  
66 sub _build_suffixes {
67 return ['*.mp4'];
68 }
69  
70 sub scan {
71 my $self = shift;
72  
73 my @movie_info = ();
74  
75 my @mrls = $self->file_mrls();
76  
77 foreach my $mrl (@mrls) {
78 $self->stream->open($mrl);
79 my ( undef, undef, $duration_millis ) = $self->stream->get_pos_length();
80 my $duration_secs = ceil( $duration_millis / 1000 );
81  
82 my $title = $self->stream->get_meta_info(XINE_META_INFO_TITLE);
83 $self->stream->close();
84  
85 if ( !defined $title ) {
86 $title = basename($mrl);
87 $title =~ s/\.\w{1,4}$//;
88 $title =~ s/_\d+kb//;
89 $title =~ s/[_-]/ /g;
90 }
91  
92 push( @movie_info, [ $duration_secs, $title, $mrl ] );
93 }
94  
95 my $movie_info_rs = $self->schema()->resultset('MovieInfo');
96  
97 my $schedule_end_rs = $self->schema->resultset('ScheduleEntryEnd');
98  
99 my $schedule_entry_rs = $self->schema->resultset('ScheduleEntry');
100  
101 my $run_sub = sub {
102 $movie_info_rs->delete();
103 $movie_info_rs->populate(
104 [ [ 'duration', 'title', 'mrl' ], @movie_info ] );
105  
106 $schedule_end_rs->delete();
107  
108 while ( my $entry = $schedule_entry_rs->next() ) {
109 my $movie = $movie_info_rs->find( { 'mrl' => $entry->mrl() } );
110 if ( !defined $movie ) {
111 warn "No Movie found for MRL '" . $entry->mrl . "'\n";
112 next;
113 }
114 $entry->create_related( 'schedule_entry_end',
115 { stop_time => $entry->start_time + $movie->duration() } );
116 }
117  
118 };
119  
120 $self->schema->txn_do($run_sub);
121  
122 return;
123 }
124  
125 sub file_mrls {
126 my $self = shift;
127  
128 my @files = File::Find::Rule->file()->name( @{ $self->suffixes() } )
129 ->in( @{ $self->directories() } );
130  
131 my @mrls = map { "file://$_" } @files;
132  
133 return @mrls;
134 }
135  
136 no Moo;
137  
138 1;