Skip to content

Commit

Permalink
Fix YouTube (issue 91).
Browse files Browse the repository at this point in the history
  • Loading branch information
monsieurvideo committed Apr 3, 2010
1 parent 128d514 commit 1496188
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 34 deletions.
6 changes: 5 additions & 1 deletion FlashVideo/Site/Googlevideosearch.pm
Expand Up @@ -23,7 +23,11 @@ sub search {
chomp(my $name = $_->text);
{ name => $name, url => $_->url_abs->as_string }
}
grep { $_->attrs()->{onclick} =~ /return resultClick/ }
grep {
$_->attrs->{onclick} =~ /return resultClick/
|| $_->attrs->{onmousedown} =~ /video_result/
|| $_->attrs->{class} eq 'l'
}
$browser->find_all_links(text_regex => qr/.+/);

return @links;
Expand Down
70 changes: 37 additions & 33 deletions FlashVideo/Site/Youtube.pm
Expand Up @@ -46,30 +46,8 @@ sub find_video {
# If the page contains fmt_url_map, then process this. With this, we
# don't require the 't' parameter.
if ($browser->content =~ /["']fmt_url_map["']:\s{0,3}["']([^"']+)["']/) {
debug "Using fmt_url_map method";

my $fmt_url_map = parse_youtube_format_url_map($1);

if (!$title and $browser->uri->as_string =~ m'/user/.*?#') {
# This is a playlist and getting the video title without the ID is
# practically impossible because multiple videos are referenced in the
# page. However, the encrypted (apparently) video ID is included in the
# URL.
my $video_id = (split /\//, $browser->uri->fragment)[-1];

my %info = get_youtube_video_info($browser->clone, $video_id);

$title = $info{title};
}

# Sort by quality...
my $preferred_quality = $prefs->quality->choose(map { $fmt_url_map->{$_->{id}}
? { resolution => $_->{resolution}, url => $fmt_url_map->{$_->{id}} }
: () } @formats);

$browser->allow_redirects;

return $preferred_quality->{url}, title_to_filename($title, "mp4");
debug "Using fmt_url_map method from page";
return $self->download_fmt_map($prefs, $browser, $title, {}, $1);
}

my $video_id;
Expand All @@ -80,11 +58,9 @@ sub find_video {
check_die($browser, "Couldn't extract video ID");
}

my $t; # no idea what this parameter is but it seems to be needed
my $t;
if ($browser->content =~ /\W['"]?t['"]?: ?['"](.+?)['"]/) {
$t = $1;
} else {
check_die($browser, "Couldn't extract mysterious t parameter");
}

# Try to get Youtube's info for this video - needed for some types of
Expand Down Expand Up @@ -141,13 +117,44 @@ sub find_video {
rtmp => $rtmp_url,
swfhash($browser, $swf_url)
};
} elsif($info{fmt_url_map}) {
debug "Using fmt_url_map method from info";
return $self->download_fmt_map($prefs, $browser, $title, \%info, $info{fmt_url_map});
}
}

return download($browser, $prefs, $video_id, $title, $t);
# Try old get_video method, just incase.
return download_get_video($browser, $prefs, $video_id, $title, $t);
}

sub download_fmt_map {
my($self, $prefs, $browser, $title, $info, $fmt_map) = @_;

my $fmt_url_map = parse_youtube_format_url_map($fmt_map);

if (!$title and $browser->uri->as_string =~ m'/user/.*?#') {
# This is a playlist and getting the video title without the ID is
# practically impossible because multiple videos are referenced in the
# page. However, the encrypted (apparently) video ID is included in the
# URL.
my $video_id = (split /\//, $browser->uri->fragment)[-1];

my %info = get_youtube_video_info($browser->clone, $video_id);

$title = $info->{title};
}

# Sort by quality...
my $preferred_quality = $prefs->quality->choose(map { $fmt_url_map->{$_->{id}}
? { resolution => $_->{resolution}, url => $fmt_url_map->{$_->{id}} }
: () } @formats);

$browser->allow_redirects;

return $preferred_quality->{url}, title_to_filename($title, "mp4");
}

sub download {
sub download_get_video {
my($browser, $prefs, $video_id, $title, $t) = @_;

my $fetcher = sub {
Expand Down Expand Up @@ -184,7 +191,7 @@ sub check_die {
my($browser, $message) = @_;

if($browser->content =~ m{class="yt-alert-content">([^<]+)}) {
$message = $1;
$message .= "\n$1";
$message =~ s/(?:^\s+|\s+$)//g;
error $message;
exit 1;
Expand Down Expand Up @@ -343,9 +350,6 @@ sub parse_youtube_format_url_map {

my $map = {};

# Simple format. Needs to be URI unescaped first.
$raw_map = uri_unescape($raw_map);

# Now split on comma as the record is structured like
# $quality|$url,$quality|$url
foreach my $pair (split /,/, $raw_map) {
Expand Down

0 comments on commit 1496188

Please sign in to comment.