<commit_before># Part of get-flash-videos. See get_flash_videos for copyright.
package FlashVideo::Site::Svtplay;
use strict;
use FlashVideo::Utils;

my $encode_rates = {
     "ultralow" => 320,
     "low" => 850,
     "medium" => 1400, 
     "high" => 2400 };

sub find_video {
  my ($self, $browser, $embed_url, $prefs) = @_;
  my @rtmpdump_commands;
  my $url;
  my $low;
  my $ultralow;
  my $medium;
  my $high;
  my $data = ($browser->content =~ /dynamicStreams=(.*?)&/)[0];
  my @values = split(/\|/, $data); 
  foreach my $val (@values) {
    if (($val =~ m/url:(.*?),bitrate:2400/)){
       $high = ($val =~ /url:(.*?),bitrate:2400/)[0];
       debug "Found " . "$high";
    } elsif (($val =~ m/url:(.*?),bitrate:1400/)){
       $medium = ($val =~ /url:(.*?),bitrate:1400/)[0];
       debug "Found " . "$medium";
    }elsif (($val =~ m/url:(.*?),bitrate:850/)){
       $low = ($val =~ /url:(.*?),bitrate:850/)[0];
       debug "Found " . "$low";
    }elsif(($val =~ m/url:(.*?),bitrate:320/)){
       $ultralow = ($val =~ /url:(.*?),bitrate:320/)[0];
       debug "Found " . "$ultralow";
    }
  }

  my $encode_rate = $encode_rates->{$prefs->{quality}};
  if ($encode_rate == 2400 && defined $high) {
    $url = $high;
  } elsif ($encode_rate == 1400 && defined $medium) {
    $url = $medium;
  } elsif ($encode_rate == 850 && defined $low) {
    $url = $low;
  } elsif ($encode_rate == 320 && defined $ultralow) {
    $url = $ultralow;
  } elsif (defined $high){
    $url = $high;
    debug "Using high"
  } elsif (defined $medium) {
    $url = $medium;
    debug "Using medium"
  } elsif (defined $low) {
    $url = $low;
    debug "Using low"
  } elsif (defined $ultralow) {
    $url = $ultralow;
    debug "Using ultralow"
  }
  
  info "Using rtmp-url: $url";
  my $sub = ($browser->content =~ /subtitle=(.*?)&/)[0];
  my $videoid = ($browser->content =~ /videoId:'(.*?)'}/)[0];
  debug "videoid:$videoid";
  $browser->get("http://svtplay.se/popup/lasmer/v/" . "$videoid");
  my $title = ($browser->content =~ /property="og:title" content="(.*?)" \/>/)[0];
  my $flv_filename = title_to_filename($title, "flv");

  if ($prefs->{subtitles} == 1) {
    if ($sub) {
      info "Found subtitles: $sub";
      $browser->get("$sub");
      my $srt_filename = title_to_filename($title, "srt"); 
      my $srt_content = $browser->content;
      open (SRT, '>>',$srt_filename) 
        or die "Can't open subtitles file $srt_filename: $!";
      binmode SRT, ':utf8';
      print SRT $srt_content;
      close SRT;
    } else {
      info "No subtitles found!";
    }
  }
  return{
      rtmp => "$url",
      flv => "$flv_filename",
  };


}

1;<commit_msg>SVTplay updated for new site<commit_after># Part of get-flash-videos. See get_flash_videos for copyright.
package FlashVideo::Site::Svtplay;
use strict;
use warnings;
use FlashVideo::Utils;
use FlashVideo::JSON;

sub find_video {
  my ($self, $browser, $embed_url, $prefs) = @_;
  my @rtmpdump_commands;
    
  if (!($browser->uri->as_string =~ m/video\/([0-9]*)/)) {
    die "No video id found in url";
  }

  my ($video_id) = $1;
  my $info_url = "http://www.svtplay.se/video/$video_id?output=json";
  $browser->get($info_url);
    
  if (!$browser->success) {
    die "Couldn't download $info_url: " . $browser->response->status_line;
  }

  my $jsonstr = $browser->content;
  my $json = from_json($jsonstr);
  my $name = $json->{context}->{title};
  my ($bitrate) = 0;
  my $rtmp;
  my $i;
  foreach $i (keys $json->{video}->{videoReferences}) {
    my ($rate) = int($json->{video}->{videoReferences}[$i]->{bitrate});
    if ($bitrate < $rate) {
      $rtmp = $json->{video}->{videoReferences}[$i]->{url};
      $bitrate = $rate;
    }
  }

  if ($prefs->{subtitles} == 1) {
    my $sub = $json->{video}->{subtitleReferences}[0]->{url}; 
    if ($sub) {
      info "Found subtitles: " . $sub;
      $browser->get("$sub");
      my $srt_filename = title_to_filename($name, "srt"); 
      my $srt_content = $browser->content;
      open (SRT, '>>',$srt_filename) 
	or die "Can't open subtitles file $srt_filename: $!";
      binmode SRT, ':utf8';
      print SRT $srt_content;
      close SRT;
    } else {
      info "No subtitles found!";
    }
  }
  return {
	  flv => title_to_filename($name, "flv"),
	  rtmp => $rtmp,
	  swfVfy => "http://www.svtplay.se/public/swf/video/svtplayer-2012.15.swf"
	 };
}

1;
{'fraction_non_alphanumeric': 0.21645714285714285, 'fraction_numerical': 0.018514285714285716, 'mean_word_length': 3.0, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1338486', 'n_tokens_mistral': 1802, 'n_tokens_neox': 1709, 'n_words': 361}