From 58b8459c57374848e30ec37cf026a8c7680a8a9f Mon Sep 17 00:00:00 2001 From: Ralf Ertzinger Date: Sun, 15 Nov 2009 16:19:30 +0100 Subject: [PATCH] - Add a fallback parser in case Parse::Lex is not available (*cough*Debian*cough). JSArrayParser->new() will return the best available parser --- videosite/JSArrayParser.pm | 56 ++++++++++++++++++++++++++++++++++++++++ videosite/JSLexArrayParser.pm | 13 ++++++---- videosite/JSSimpleArrayParser.pm | 38 +++++++++++++++++++++++++++ videosite/YouTubeGrabber.pm | 2 ++ 4 files changed, 104 insertions(+), 5 deletions(-) create mode 100644 videosite/JSArrayParser.pm create mode 100644 videosite/JSSimpleArrayParser.pm diff --git a/videosite/JSArrayParser.pm b/videosite/JSArrayParser.pm new file mode 100644 index 0000000..cadf8e8 --- /dev/null +++ b/videosite/JSArrayParser.pm @@ -0,0 +1,56 @@ +# +# This is a stub class for more complex JS*ArrayParser objects. +# It's new() method usually does _not_ return an object of +# type videosite::JSArrayParser, but rather a child object of itself +# which is able to do some actual work. The only time new() retuns +# an videosite::JSArrayParser onject is when no child objects are +# available. +# + +package videosite::JSArrayParser; + +use strict; + +sub new { + my $class = shift; + my $self = {}; + + if ($class ne __PACKAGE__) { + # We were called from a child object. Return ourselves. + return bless($self, $class); + } + + # Try to find a child object which is available and return that. + # + # See if videosite::JSLexArrayParser is available + eval { + require videosite::JSLexArrayParser; + }; + + unless($@) { + # Available. Return a JSLexArrayParser object + return videosite::JSLexArrayParser->new(); + } + + # See if JSSimleArrayParser is available + eval { + require videosite::JSSimpleArrayParser; + }; + + unless ($@) { + # Available. Return a JSSimpleArrayParser object + return videosite::JSSimpleArrayParser->new(); + } + + # Nothing available. Return ourselves. + return bless($self, $class); +} + +sub parse { + my $self = shift; + + # No functionality here + return undef; +} + +1; diff --git a/videosite/JSLexArrayParser.pm b/videosite/JSLexArrayParser.pm index 20e6e0e..f250d61 100644 --- a/videosite/JSLexArrayParser.pm +++ b/videosite/JSLexArrayParser.pm @@ -6,7 +6,10 @@ # package. Use 'yapp -m videosite::jsarray -s jsarray.yp' to regenerate # -package videosite::JSArrayParser; +package videosite::JSLexArrayParser; + +use videosite::JSArrayParser; +@ISA = qw(videosite::JSArrayParser); use Parse::Lex; use videosite::jsarray; @@ -23,10 +26,10 @@ my @tokens = ( sub new { my $class = shift; - my $self = { - '_PARSER' => videosite::jsarray->new(), - '_LEXER' => Parse::Lex->new(@tokens), - }; + my $self = $class->SUPER::new(); + + $self->{'_PARSER'} = videosite::jsarray->new(); + $self->{'_LEXER'} = Parse::Lex->new(@tokens); return bless($self, $class); } diff --git a/videosite/JSSimpleArrayParser.pm b/videosite/JSSimpleArrayParser.pm new file mode 100644 index 0000000..0c1e36e --- /dev/null +++ b/videosite/JSSimpleArrayParser.pm @@ -0,0 +1,38 @@ +# +# A helper class for parsing textual JS hashes into perl +# hashes +# +# This parser is based on simple regexps. +# + +package videosite::JSSimpleArrayParser; + +use videosite::JSArrayParser; +@ISA = qw(videosite::JSArrayParser); + +use strict; + +sub new { + my $class = shift; + my $self = $class->SUPER::new(); + + return bless($self, $class); +} + +sub parse { + my $self = shift; + my $s = shift; + my $ret; + + if ($s =~ /{(.*)}/) { + my $str = $1; + while ($str =~ /"(\S+)":\s+"([^"]*)"(,\s*)?/g) { + $ret->{$1} = $2; + } + } else { + return undef; + } + return $ret; +} + +1; diff --git a/videosite/YouTubeGrabber.pm b/videosite/YouTubeGrabber.pm index 2ed52a7..644584a 100644 --- a/videosite/YouTubeGrabber.pm +++ b/videosite/YouTubeGrabber.pm @@ -111,6 +111,8 @@ sub _parse { $self->debug("Found SWF_ARGS: %s", $args); $jsp = videosite::JSArrayParser->new(); + print ref($jsp); + $self->debug("Using %s to parse", ref($jsp)); $r = $jsp->parse($args); unless(defined($r)) { -- 1.8.3.1