File Coverage

blib/lib/App/Automaton/Plugin/Action/YouTube.pm
Criterion Covered Total %
statement 18 42 42.8
branch 0 8 0.0
condition n/a
subroutine 6 8 75.0
pod 1 1 100.0
total 25 59 42.3


line stmt bran cond sub pod time code
1             package App::Automaton::Plugin::Action::YouTube;
2              
3             # ABSTRACT: Download module for YouTube videos
4              
5 1     1   656 use strict;
  1         2  
  1         33  
6 1     1   4 use warnings;
  1         1  
  1         26  
7 1     1   429 use Moo;
  1         10243  
  1         6  
8 1     1   1751 use File::Spec::Functions;
  1         529  
  1         96  
9 1     1   439 use WWW::YouTube::Download 0.57;
  1         38731  
  1         32  
10              
11 1     1   622 use Data::Dumper;
  1         4905  
  1         266  
12              
13             sub go {
14 0     0 1   my $self = shift;
15 0           my $in = shift;
16 0           my $bits = shift;
17 0           my $d = $in->{debug};
18            
19 0           my $target = $in->{target};
20            
21 0           foreach my $bit (@$bits) {
22 0           my @urls = $bit =~ /http[s]?:\/\/www.youtube\.com\/watch\?v=.{11}/g;
23 0           foreach my $url (@urls) {
24 0           my $client = WWW::YouTube::Download->new();
25 0           my $video_data;
26 0 0         eval { $video_data = $client->prepare_download($url); }; warn "Error with $url\n".$@ if $@;
  0            
  0            
27             #TODO: Report errors
28 0 0         next unless $video_data;
29 0           my $target_file = catfile($target, $video_data->{title} . '.' . $video_data->{suffix} );
30 0 0         next if -e $target_file;
31 0           _logger($d, "downloading $url to $target_file");
32 0           eval{$client->download( $url, { filename => $target_file } );}
  0            
33             }
34             }
35            
36 0           return 1;
37             }
38              
39             sub _logger {
40 0     0     my $level = shift;
41 0           my $message = shift;
42 0 0         print "$message\n" if $level;
43 0           return 1;
44             }
45              
46             1;
47              
48             __END__