| 1 | package App::Automaton::Plugin::Action::TedTalks; | ||||||||||||
| 2 | |||||||||||||
| 3 | # ABSTRACT: Download module for Ted Talk videos | ||||||||||||
| 4 | |||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 1 | ▶ | use Moo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 1 | ▶ | use WWW::Offliberty qw/off/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 1 | ▶ | use LWP::UserAgent; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 1 | ▶ | use File::Spec::Functions; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | 1 | ▶ | use Data::Dumper; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | 0 | ▶ | sub go { | ||||||||||
|
Subroutine
go: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||
| 15 | 0 | my $self = shift; | |||||||||||
| 16 | 0 | my $in = shift; | |||||||||||
| 17 | 0 | my $bits = shift; | |||||||||||
| 18 | 0 | ▶ | my $target = $in->{target} || '.'; | ||||||||||
|
Condition0%0/2
$in->{'target'} || '.'
MC/DC0%0/1
$in->{'target'} || '.'
$in->{'target'}
Truth table0%0/2
A
A= $in->{'target'}
| |||||||||||||
| 19 | 0 | my $d = $in->{debug}; | |||||||||||
| 20 | |||||||||||||
| 21 | 0 | my $ua = LWP::UserAgent->new(); | |||||||||||
| 22 | |||||||||||||
| 23 | 0 | foreach my $bit (@$bits) { | |||||||||||
| 24 | 0 | my @urls = $bit =~ /www.ted.com\/talks\/[a-z,A-Z,0-9,_]+/g; | |||||||||||
| 25 | |||||||||||||
| 26 | 0 | foreach my $url (@urls) { | |||||||||||
| 27 | 0 | ▶ | next unless $url; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 28 | 0 | my $name = _get_name($url); | |||||||||||
| 29 | 0 | _logger($d, "getting links for $url"); | |||||||||||
| 30 | 0 | my $new_url = _get_link($url); | |||||||||||
| 31 | #TODO: what if url is undef?' | ||||||||||||
| 32 | 0 | ▶ | die "could not determine new url for $url" unless $new_url; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 33 | 0 | my $target_file = catfile($target, $name); | |||||||||||
| 34 | 0 | ▶ | next if -e $target_file; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 35 | 0 | my $ua = LWP::UserAgent->new(); | |||||||||||
| 36 | 0 | _logger($d, "downloading $new_url to $target_file"); | |||||||||||
| 37 | 0 | $ua->mirror( $new_url, $target_file ); | |||||||||||
| 38 | } | ||||||||||||
| 39 | |||||||||||||
| 40 | } | ||||||||||||
| 41 | |||||||||||||
| 42 | 0 | return (1); | |||||||||||
| 43 | } | ||||||||||||
| 44 | |||||||||||||
| 45 | 0 | ▶ | sub _get_link { | ||||||||||
|
Subroutine
_get_link: not called CC=3 SCAR=24.8
| |||||||||||||
| 46 | 0 | my $url = shift; | |||||||||||
| 47 | |||||||||||||
| 48 | 0 | my @links = off( $url, video_file => 1 ); | |||||||||||
| 49 | |||||||||||||
| 50 | 0 | my $_get_link; | |||||||||||
| 51 | 0 | foreach my $link (@links) { | |||||||||||
| 52 | #TODO: I'd like to make this more sophisticated, with less assumption | ||||||||||||
| 53 | #TODO: Also, maybe a flag to specify language or format preference, even audio only | ||||||||||||
| 54 | 0 | ▶ | if ( $link =~ m/-480p.mp4/ ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 55 | 0 | $_get_link = $link; | |||||||||||
| 56 | } | ||||||||||||
| 57 | } | ||||||||||||
| 58 | |||||||||||||
| 59 | 0 | return $_get_link; | |||||||||||
| 60 | } | ||||||||||||
| 61 | |||||||||||||
| 62 | 1 | ▶ | sub _get_name { | ||||||||||
|
Subroutine
_get_name: called CC=2 SCAR=7.0
| |||||||||||||
| 63 | 1 | my $uri = shift; | |||||||||||
| 64 | |||||||||||||
| 65 | 1 | my $name = ( split( /\//, $uri ) )[-1]; | |||||||||||
| 66 | |||||||||||||
| 67 | # swap out characters that we don't want in the file name | ||||||||||||
| 68 | 1 | $name =~ s/[^a-zA-Z0-9\\-]/_/g; | |||||||||||
| 69 | |||||||||||||
| 70 | #TODO: This should be based on the "_get_link" var from above? | ||||||||||||
| 71 | # put the .mp4 back on | ||||||||||||
| 72 | 1 | ▶ | if ( lc( substr( $name, -4 ) ) ne '.mp4' ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 73 | 1 | $name .= '.mp4'; | |||||||||||
| 74 | } | ||||||||||||
| 75 | |||||||||||||
| 76 | 1 | return $name; | |||||||||||
| 77 | } | ||||||||||||
| 78 | |||||||||||||
| 79 | 0 | ▶ | sub _logger { | ||||||||||
|
Subroutine
_logger: not called CC=2 SCAR=17.9
| |||||||||||||
| 80 | 0 | my $level = shift; | |||||||||||
| 81 | 0 | my $message = shift; | |||||||||||
| 82 | 0 | ▶ | print "$message\n" if $level; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 83 | 0 | return 1; | |||||||||||
| 84 | } | ||||||||||||
| 85 | |||||||||||||
| 86 | 1 | 1; | |||||||||||
| 87 | |||||||||||||
| 88 | __END__ | ||||||||||||