diff options
author | Jonas <jonaskarlsson@fripost.org> | 2012-12-08 15:22:33 +0100 |
---|---|---|
committer | Jonas <jonaskarlsson@fripost.org> | 2012-12-08 15:22:33 +0100 |
commit | 29d159e0e4c3fbea02a5bbf234c10a91ff839734 (patch) | |
tree | dfe8bae1bfc211c8c567ee270e4a3a620b3fd148 | |
parent | abe8f16fb4d2f5d448e522702b1d6b24971fd535 (diff) | |
download | get-flash-videos-29d159e0e4c3fbea02a5bbf234c10a91ff839734.tar.gz |
Tv4play.pm: refactoring code
-rw-r--r-- | lib/FlashVideo/Site/Tv4play.pm | 52 |
1 files changed, 27 insertions, 25 deletions
diff --git a/lib/FlashVideo/Site/Tv4play.pm b/lib/FlashVideo/Site/Tv4play.pm index 1cfad1d..f45458a 100644 --- a/lib/FlashVideo/Site/Tv4play.pm +++ b/lib/FlashVideo/Site/Tv4play.pm @@ -2,12 +2,12 @@ package FlashVideo::Site::Tv4play; use strict; use FlashVideo::Utils; - +use List::Util qw(reduce); sub find_video { my ($self, $browser, $embed_url, $prefs) = @_; - my $vid = ($embed_url =~ /video_id=([0-9]*)/)[0]; - my $smi_url = "http://premium.tv4play.se/api/web/asset/$vid/play"; + my $video_id = ($embed_url =~ /video_id=([0-9]*)/)[0]; + my $smi_url = "http://premium.tv4play.se/api/web/asset/$video_id/play"; my $title = ($browser->content =~ /property="og:title" content="(.*?)"/)[0]; my $flv_filename = title_to_filename($title, "flv"); @@ -17,20 +17,20 @@ sub find_video { my @streams; my $subtitle_url; - for ($i = 0; $i < length($content->{items}); $i++){ - my $format = $content->{items}->{item}[$i]->{mediaFormat}; - my $bitrate = $content->{items}->{item}[$i]->{bitrate}; - my $rtmp = $content->{items}->{item}[$i]->{base}; - my $mp4 = $content->{items}->{item}[$i]->{url}; - @streams[$i] = { 'rtmp' => $rtmp, - 'bitrate' => $bitrate, - 'mp4' => $mp4, - 'format' => $format - }; + foreach my $item (@{ $content->{items}->{item} || [] }) { + push @streams, { + rtmp => $item->{base}, + bitrate => $item->{bitrate}, + mp4 => $item->{url}, + format => $item->{mediaFormat} + }; } foreach (@streams) { - if($_->{format} eq 'smi'){ $subtitle_url = $_->{mp4};} + if ($_->{format} eq 'smi') { + $subtitle_url = $_->{mp4}; + last; + } } if ($prefs->{subtitles} == 1) { @@ -39,24 +39,26 @@ sub find_video { if (!$browser->success) { info "Couldn't download subtitles: " . $browser->status_line; } else { - my $srt_filename = title_to_filename($title, "srt"); - info "Saving subtitles as " . $srt_filename; - open my $srt_fh, '>', $srt_filename - or die "Can't open subtitles file $srt_filename: $!"; - binmode $srt_fh, ':utf8'; - print $srt_fh $browser->content; - close $srt_fh; + my $srt_filename = title_to_filename($title, "srt"); + info "Saving subtitles as " . $srt_filename; + open my $srt_fh, '>', $srt_filename + or die "Can't open subtitles file $srt_filename: $!"; + binmode $srt_fh, ':utf8'; + print $srt_fh $browser->content; + close $srt_fh; } } else { info "No subtitles found"; } } + my $max_stream = reduce {$a->{bitrate} > $b->{bitrate} ? $a : $b} @streams; + return { - rtmp => $streams[0]->{rtmp}, - swfVfy => "http://www.tv4play.se/flash/tv4playflashlets.swf", - playpath => $streams[0]->{mp4}, - flv => $flv_filename + rtmp => $max_stream->{rtmp}, + swfVfy => "http://www.tv4play.se/flash/tv4playflashlets.swf", + playpath => $max_stream->{mp4}, + flv => $flv_filename }; } |