X-Git-Url: https://git.camperquake.de/gitweb.cgi?a=blobdiff_plain;f=videosite%2FYouTubeGrabber.pm;h=199143e8b3d99e46f09035e45f182b5ce3c6baf9;hb=76a9c69c78eec62f96cacba4b5ca1ac67b030661;hp=d6232a90e9c918e599b9ba0139b8a5cc30e58860;hpb=2247c833229fb2809d6e49c5074771067a2125d2;p=videosite.git diff --git a/videosite/YouTubeGrabber.pm b/videosite/YouTubeGrabber.pm index d6232a9..199143e 100644 --- a/videosite/YouTubeGrabber.pm +++ b/videosite/YouTubeGrabber.pm @@ -14,8 +14,10 @@ use videosite::GrabberBase; use LWP::UserAgent; use HTTP::Cookies; use HTML::TokeParser; +use HTML::Entities qw(decode_entities); +use Encode; use Data::Dumper; -use List::Util; +use videosite::JSArrayParser; use strict; @@ -56,11 +58,12 @@ sub _parse { my $videourl; my $quality = $self->_getval('QUALITY'); my %preflist = ( - 'hd' => [22, 35, 18, 34, 6, 5], - 'h264' => [18, 34, 22, 35, 6, 5], - 'high' => [34, 35, 18, 22, 6, 5], - 'normal' => [6, 5, 34, 35, 18, 22]); + 'hd' => [37, 22, 35, 18, 34, 6, 5], + 'h264' => [18, 34, 37, 22, 35, 6, 5], + 'high' => [34, 35, 18, 37, 22, 6, 5], + 'normal' => [6, 5, 34, 35, 18, 22, 37]); my $preflist; + my $jsp; $url =~ m|$pattern|; $url = $1; @@ -72,6 +75,8 @@ sub _parse { $metadata->{'TITLE'} = undef; $metadata->{'DLURL'} = undef; + $self->debug("Matched id %s from pattern %s", $2, $pattern); + $preflist = $preflist{$quality}; $self->debug("Quality: %s, preflist: [%s]", $quality, join(", ", @{$preflist})); @@ -95,40 +100,85 @@ sub _parse { $p = HTML::TokeParser->new(\$content); - while ($tag = $p->get_tag('div', 'meta', 'script')) { + SWF_ARGS: while ($tag = $p->get_tag('div', 'meta', 'script')) { if ('meta' eq $tag->[0]) { if ('title' eq $tag->[1]->{'name'}) { $metadata->{'TITLE'} = $tag->[1]->{'content'}; + # Convert HTML entities in the title. This is a bit convoluted. + $metadata->{'TITLE'} = encode("utf8", + decode_entities( + decode("utf8", $metadata->{'TITLE'}))); + $self->debug('Title found: %s', $metadata->{'TITLE'}); } } elsif ('script' eq $tag->[0]) { $e = $p->get_text(); $self->debug("Found script: %s", $e); - if ($e =~ m|\x22fmt_url_map\x22\s*:\s*\x22([^\x22]+)\x22|) { - $self->debug("Found fmt_url_map: %s", $1); - my $urls = $1; - my @urls; + if ($e =~ m|\x27SWF_ARGS\x27:\s+(.+),|) { my %urls; + my $args = $1; + + $self->debug("Found SWF_ARGS: %s", $args); + $jsp = videosite::JSArrayParser->new(); + $self->debug("Using %s to parse", ref($jsp)); + $r = $jsp->parse($args); + + unless(defined($r)) { + $self->error("Found information hash, but could not parse"); + return undef; + } - $urls =~ s/%(..)/chr(hex($1))/ge; - @urls = split(/,/, $urls); - foreach (@urls) { - my ($tag, $url) = split(/\|/); - if ($tag == 35) { - $self->debug('Found flv,h264,large: %s', $url); - } elsif ($tag == 34) { - $self->debug('Found flv,h264: %s', $url); - } elsif ($tag == 22) { - $self->debug('Found mp4,h264,large: %s', $url); - } elsif ($tag == 18) { - $self->debug('Found mp4,h264: %s', $url); - } elsif ($tag == 5) { - $self->debug('Found flv,flv: %s', $url); + if (exists($r->{'fmt_url_map'}) and ($r->{'fmt_url_map'} ne '')) { + my $urls = $r->{'fmt_url_map'}; + + $self->debug("Video has fmt_url_map: %s", $urls); + + $urls =~ s/%([[:xdigit:]]{2})/chr(hex($1))/ge; + %urls = split(/[\|,]/, $urls); + } elsif (exists($r->{'t'}) and ($r->{'t'} ne '')) { + my $thash = $r->{'t'}; + + if (exists($r->{'fmt_map'}) && ($r->{'fmt_map'} ne '')) { + my $fmt = $r->{'fmt_map'}; + my @fmt; + + $self->debug('Video has fmt_map'); + $fmt =~ s/%([[:xdigit:]]{2})/chr(hex($1))/ge; + @fmt = split(/,/, $fmt); + foreach (@fmt) { + split(/\//); + $urls{$_[0]} = sprintf('http://www.youtube.com/get_video?video_id=%s&fmt=%d&t=%s', + $metadata->{'ID'}, + $_[0], + $thash); + } + } else { + $urls{5} = sprintf('http://www.youtube.com/get_video?video_id=%s&t=%s', + $metadata->{'ID'}, + $thash); + } + } else { + $self->error('Neither fmt_url_map nor t found in video information hash'); + return undef; + } + $self->debug("Found quality levels [%s]", join(", ", keys(%urls))); + + foreach (keys(%urls)) { + if ($_ == 35) { + $self->debug('Found flv,h264,large: %s', $urls{$_}); + } elsif ($_ == 34) { + $self->debug('Found flv,h264: %s', $urls{$_}); + } elsif ($_ == 22) { + $self->debug('Found mp4,h264,720p: %s', $urls{$_}); + } elsif ($_ == 37) { + $self->debug('Found mp4,h264,1080p: %s', $urls{$_}); + } elsif ($_ == 18) { + $self->debug('Found mp4,h264: %s', $urls{$_}); + } elsif ($_ == 5) { + $self->debug('Found flv,flv: %s', $urls{$_}); } else { - $self->error('Unknown tag %s: %s', $tag, $url); - next; + $self->error('Unknown tag %s: %s', $_, $urls{$_}); } - $urls{$tag} = $url; } foreach (@{$preflist}) { @@ -140,9 +190,10 @@ sub _parse { } $self->debug('URL found: %s', $metadata->{'DLURL'}); + last SWF_ARGS; } } elsif ('div' eq $tag->[0]) { - if (exists($tag->[1]->{'class'}) and ('errorBox' eq $tag->[1]->{'class'})) { + if (exists($tag->[1]->{'class'}) and ('yt-alert-content' eq $tag->[1]->{'class'})) { $self->error("Could not get video data for youtube %s: %s", $metadata->{'ID'}, $p->get_trimmed_text()); return undef;