blib/lib/Videosubs/WebVTT.pm untested

filter: Statementstmt 0.0% 0 / 101 Branchbran 0.0% 0 / 20 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 140 CC 11 SCAR 27.7
CC
11
Cov
0%
parse_incoming_file
42.0
parse_block
12.0
new
6.0
CRAP 16.0
1 package Videosubs::WebVTT;
2 use Videosubs::WebVTTBlock;
3 use warnings;
4 use strict;
5  
6 our $VERSION = '0.05';
7  
8 # INSTANTIATE NEW WEBVTT OBJECT
9 # TAKES: OPTIONAL VALUES: HEADER, FILENAME
10 # DOES:
11 # CREATE NEW OBJECT
12 # PARSES INCOMING FILE, IF APPLICABLE
13 # RETURNS: NEWLY CREATED OBJECT
14 sub new {
15 my $class = shift;
16 my $self = {
17 'header' => '',
18 'filename' => '',
19 @_
20 };
21 bless $self, $class;
22  
23 if ($self->webvtt_filename() ne '') {
24 $self->parse_incoming_file();
25 }
26 return $self;
27 }
28  
29 # GET/SET THE HEADER
30 sub webvtt_header {
31 my $self = shift;
32 if (scalar(@_) == 1)
33 { $self->{'header'} = shift; }
34 return $self->{'header'};
35 }
36  
37 # GET/SET THE FILENAME
38 sub webvtt_filename {
39 my $self = shift;
40 if (scalar(@_) == 1)
41 { $self->{'filename'} = shift; }
42 return $self->{'filename'};
43 }
44  
45 # PARSE AN INCOMING FILE.
46 # TAKES: A WEBVTT OBJECT CALLER.
47 # DOES: ADD WEBVTT BLOCK OBJECT(S) TO CALLER.
48 # RETURNS:
49 # 0 IF FILE DOES NOT EXIST.
50 # 0 IF FILE NOT READABLE.
51 # 1 IF FILE SUCCESSFULLY PARSED INTO ZERO OR MORE BLOCKS
52 sub parse_incoming_file {
53 my $self = shift;
54 my $incoming_file = $self->webvtt_filename();
55 my $temp_webvtt_header = '';
56 my @temp_webvtt_blocks_array = ();
57 unless (-e $incoming_file) {
58 warn "Could not find $incoming_file - $!\n";
59 return 0;
60 }
61  
62 my $block_count = 0;
63 my $current_block = '';
64 my $incoming_fh;
65 unless (open ($incoming_fh, '<', $incoming_file)) {
66 warn "Failed to open $incoming_file - $!\n";
67 return 0;
68 }
69  
70 my $found_header = 0;
71 while (my $single_line = <$incoming_fh>) {
72 chomp $single_line;
73 $single_line =~ s/^\s+(\S)*\s+$/$1/i;
74 # IF WE GET TO A BLANK LINE, PROCESS STUFF IF NEEDED
75 if ($single_line eq '') {
76 # IF NEEDED, PROCESS A HEADER:
77 if ($found_header == 0) {
78 if ($current_block ne '') {
79 # ONCE WE FIND A HEADER, STORE IT AND RESET THE VARIOUS COUNTERS.
80 $temp_webvtt_header = $current_block;
81 $found_header = 1;
82 $current_block = '';
83 next;
84 }
85 else {
86 # IF WE DON'T HAVE A VALID $current_block THEN IT'S JUST AN OPENING BLANK LINE; SKIP.
87 next;
88 } # END: elsif, meaning we have a blank line and no data yet
89 } # END: IF WE HAVE NO HEADER
90 my $new_block_object = parse_block($current_block);
91 push @temp_webvtt_blocks_array, $new_block_object;
92 $current_block = '';
93 $block_count++;
94 } # END: IF $single_line IS BLANK, MEANING WE HAVE A BLOCK TO PROCESS.
95 else {
96 # WE DON'T HAVE A BLANK LINE YET, SO KEEP ADDING THIS TO THE TEMP BLOCK.
97 $current_block .= "$single_line\n";
98 } # END else, MEANING WE DON'T HAVE A BLANK LINE.
99 } # END WHILE LOOP, WHERE WE CYCLE THROUGH THE FILE
100 my $new_block_object = parse_block($current_block);
101 push @temp_webvtt_blocks_array, $new_block_object;
102 $block_count++;
103 $self->{'header'} = $temp_webvtt_header;
104 $self->{'blocks'} = \@temp_webvtt_blocks_array;
105 return 1;
106 }
107  
108 # WE MUST HAVE A VALID HEADER, SO WE'RE JUST PARSING A BIT OF TEXT NOW:
109 sub parse_block {
110 my $current_block = shift;
111 my @all_lines = split("\n", $current_block);
112 if (scalar @all_lines < 2) {
113 warn "Not sure what to do with [$current_block] - it's less than two lines!\n";
114 return 0;
115 }
116  
117 my $temp_label = '';
118 my $temp_date_line = '';
119  
120 # THE FIRST LINE CAN BE A LABEL OR A TIMESTAMP:
121 my $first_line = shift(@all_lines);
122 if ($first_line =~ /\d{2}(:\d{2})*([.]\d+)*(\s+[->]+\s+)\d{2}(:\d{2})([.]\d+)*/) {
123 # THE FIRST LINE IS THE TIMESTAMP, SO THERE'S NO LABEL...
124 $temp_label = '';
125 $temp_date_line = $first_line;
126 }
127 else {
128 $temp_label = $first_line;
129 $temp_date_line = shift(@all_lines);
130 }
131  
132 $temp_date_line =~ /([^- ]+)\s+\-\-\>\s+([^- ]+)/;
133 my $temp_start_time = $1;
134 my $temp_end_time = $2;
135 my $temp_data = join("\n", @all_lines);
136 my $new_block = Videosubs::WebVTTBlock->new(
137 'label' => '',
138 'start_time' => $temp_start_time,
139 'end_time' => $temp_end_time,
140 'data' => $temp_data
141 );
142  
143 #print "Attempting to create block with label, s, e, d: [$temp_label], [$temp_start_time], [$temp_end_time], [$temp_data]\n--\n";
144  
145 return $new_block;
146 }
147  
148 # COUNT HOW MANY BLOCKS THIS OBJECT HAS.
149 sub count_blocks {
150 my $self = shift;
151 my @blocks = @{$self->{'blocks'}};
152 my $num_blocks = scalar @blocks;
153 return $num_blocks;
154 }
155  
156 # PRINTS ENTIRE WEBVTT OBJECT.
157 sub printall {
158 my $self = shift;
159 my @all_blocks = @{$self->{'blocks'}};
160 print $self->webvtt_header;
161 foreach my $one_block (@all_blocks) {
162 print "\n";
163 print $one_block->printable_block();
164 }
165 }
166  
167 sub printall_srt {
168 my $self = shift;
169 my $counter =0;
170 my @all_blocks = @{$self->{'blocks'}};
171 foreach my $one_block (@all_blocks) {
172 print "\n";
173 print ++$counter . "\n";
174 print $one_block->printable_srt();;
175 }
176  
177 }
178  
179 1;