Merge pull request #104 from karjonas/rtmpcheck

Adding a try_download function for RTMPDownloader
diff --git a/lib/FlashVideo/RTMPDownloader.pm b/lib/FlashVideo/RTMPDownloader.pm
index 7ab2ec0..cb37540 100644
--- a/lib/FlashVideo/RTMPDownloader.pm
+++ b/lib/FlashVideo/RTMPDownloader.pm
@@ -6,6 +6,8 @@
 use IPC::Open3;
 use Fcntl ();
 use Symbol qw(gensym);
+use File::Temp qw(tempfile tempdir);
+use Storable qw(dclone);
 use FlashVideo::Utils;
 
 use constant LATEST_RTMPDUMP => 2.2;
@@ -91,6 +93,54 @@
   return -s $file;
 }
 
+# Check if a stream is active by downloading a sample
+sub try_download {
+
+  my ($self, $rtmp_data_orig) = @_;
+  my $rtmp_data = dclone($rtmp_data_orig);
+
+  # Create a temporary file for the test
+  my ($fh, $filename) = tempfile();
+  $rtmp_data->{flv} = $filename;
+
+  # Just download a second of video
+  $rtmp_data->{stop} = "1";
+
+  if(my $socks = FlashVideo::Mechanize->new->get_socks_proxy) {
+    $rtmp_data->{socks} = $socks;
+  }
+
+  my $prog = $self->get_rtmp_program;
+
+  if($prog eq 'flvstreamer' && ($rtmp_data->{rtmp} =~ /^rtmpe:/ || $rtmp_data->{swfhash})) {
+    error "FLVStreamer does not support "
+      . ($rtmp_data->{swfhash} ? "SWF hashing" : "RTMPE streams")
+      . ", please install rtmpdump.";
+    exit 1;
+  }
+
+  if($self->debug) {
+    $rtmp_data->{verbose} = undef;
+  }
+
+  my($return, @errors) = $self->run($prog, $rtmp_data);
+
+  if($return != 0 && "@errors" =~ /failed to connect/i) {
+    # Try port 443 as an alternative
+    info "Couldn't connect on RTMP port, trying port 443 instead";
+    $rtmp_data->{port} = 443;
+    ($return, @errors) = $self->run($prog, $rtmp_data);
+  }
+
+  # If we got an unrecoverable error return false
+  if($return == 1) {
+    info "\n Tested stream failed.";
+    return 0;
+  }
+
+  return 1;
+}
+
 sub get_rtmp_program {
   if(is_program_on_path("rtmpdump")) {
     return "rtmpdump";
diff --git a/lib/FlashVideo/Site/Kanal5play.pm b/lib/FlashVideo/Site/Kanal5play.pm
index 862c2b9..125a627 100644
--- a/lib/FlashVideo/Site/Kanal5play.pm
+++ b/lib/FlashVideo/Site/Kanal5play.pm
@@ -6,15 +6,9 @@
 use FlashVideo::Utils;
 use FlashVideo::JSON;
 
-our $VERSION = '0.01';
+our $VERSION = '0.02';
 sub Version() { $VERSION;}
 
-my $bitrates = {
-  low    => 250000,
-  medium => 450000,
-  high   => 900000
-};
-
 sub find_video {
   my ($self, $browser, $embed_url, $prefs) = @_;
   if (!($browser->uri->as_string =~ m/video\/([0-9]*)/)) {
@@ -37,20 +31,36 @@
   my $filename = sprintf "%s - S%02dE%02d", $name, $season, $episode;
   my $rtmp     = "rtmp://fl1.c00608.cdn.qbrick.com:1935/00608";
   my $playpath = $json->{streams}[0]->{source};
+  my $max_rate = 0;
 
+  # Always take the highest bitrate stream
   foreach my $stream (@{$json->{streams}}) {
     my $rate = int($stream->{bitrate});
-    if ($bitrates->{$prefs->{quality}} == $rate) {
+    if ($rate > $max_rate) {
       $playpath = $stream->{source};
-      last;
+      $max_rate = $rate;
     }
   }
 
-  return {
+  # Check if the maximum quality stream is available.
+  # The stream is not present in the json object even if it exists,
+  # so we have to try the playpath manually.
+  my $downloader = FlashVideo::RTMPDownloader->new;
+  $playpath =~ m/(.*)_([0-9]*)\Z/;
+  my $playpath_max = $1 . "_1600";
+
+  my $args = {
     flv      => title_to_filename($filename, "flv"),
     rtmp     => $rtmp,
-    playpath => $playpath,
+    playpath => $playpath_max,
     swfVfy   => "http://www.kanal5play.se/flash/K5StandardPlayer.swf"
   };
+
+  # If the stream was not found we revert the playpath
+  if (!$downloader->try_download($args)) {
+    $args->{playpath} = $playpath;
+  }
+
+  return $args;
 }
 1;