fix quoting in AsyncWgetFileGetter again
[videosite.git] / videosite / RedTubeGrabber.pm
index a67a755..72036b8 100644 (file)
@@ -6,13 +6,11 @@
 # Algorithm for the file name hash reverse engineered by
 # Maximilian Rehkopf  <otakon at gmx dot net>
 
-package RedTubeGrabber;
+package videosite::RedTubeGrabber;
 
-use GrabberBase;
-@ISA = qw(GrabberBase);
+use videosite::GrabberBase;
+@ISA = qw(videosite::GrabberBase);
 
-use LWP::UserAgent;
-use HTTP::Cookies;
 use HTML::TokeParser;
 use Data::Dumper;
 
@@ -20,26 +18,15 @@ use strict;
 
 sub new {
     my $class = shift;
-    my $self = $class->SUPER::new();
-
-    $self->{'NAME'} = 'redtube';
-    $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*redtube.com/(\d+))'];
-
-    bless($self, $class);
-    $self->_prepare_parameters();
-
-    return $self;
-}
-
-sub check_cookie {
-
-    my $jar = shift;
-    my $key = shift;
-    my $found = undef;
-
-    $jar->scan(sub { $found = 1 if ( $key eq $_[1]) });
-
-    return $found;
+    my $self = $class->SUPER::new(
+        NAME => 'redtube',
+        _SELFTESTURL => 'http://www.redtube.com/8269',
+        _SELFTESTTITLE => 'Porn bloopers with pretty girl',
+        PATTERNS => ['(http://(?:[-a-zA-Z0-9_.]+\.)*redtube.com/(\d+))'],
+        @_,
+    );
+
+    return bless($self, $class);
 }
 
 sub div($$) {
@@ -83,12 +70,12 @@ sub _parse {
     my $self = shift;
     my $url = shift;
     my $pattern = shift;
-    my $jar = HTTP::Cookies->new();
-    my $ua = LWP::UserAgent->new('agent' => 'Mozilla/5.0');
+    my $ua = $self->ua();
     my $content;
     my $metadata = {};
     my $p;
     my $r;
+    my $tag;
     my $dir;
     my $hash;
 
@@ -103,32 +90,38 @@ sub _parse {
     $metadata->{'DLURL'} = undef;
 
     # Set the cookies necessary to get the video data
-    $jar->set_cookie(undef, 'pp', '1', '/', '.redtube.com');
-    $ua->cookie_jar($jar);
+    $ua->cookie_jar->set_cookie(undef, 'pp', '1', '/', '.redtube.com');
 
-    unless(defined($r = $ua->get(sprintf("http://www.redtube.com/%s", $2)))) {
+    unless(defined($content = $self->simple_get(sprintf("http://www.redtube.com/%s", $2), $ua))) {
         $self->error('Could not download page');
         return undef;
     }
 
-    # Get the site to extract the title
-    $content = $r->decoded_content();
-
     $p = HTML::TokeParser->new(\$content);
 
     # Look for the title
-    if ($p->get_tag('title')) {
-        my $t = $p->get_text();
-        if ($t =~ /\xa0RedTube - /) {
+    while ($tag = $p->get_tag('title', 'script')) {
+        if ('title' eq $tag->[0]) {
+            my $t = $p->get_text();
             $metadata->{'TITLE'} = $t;
-            $metadata->{'TITLE'} =~ s/\xa0RedTube - //;
-        }
-    }
+            $metadata->{'TITLE'} =~ s/ \| Redtube.*//;
+        } elsif ('script' eq $tag->[0]) {
+            my $t = $p->get_text();
+
+            if ($t =~ m|so\.addParam\("flashvars","([^\x22]+)"|) {
+                my %h;
 
-    # Redtube uses a selfmade hash system to create the filename
-    ($dir, $hash) = mkfilename($metadata->{'ID'});
+                $self->debug("Found flashvars: %s", $1);
+                %h = map { $self->decode_hexurl($_) } split(/[&=]/, $1);
 
-    $metadata->{'DLURL'} = sprintf('http://dl.redtube.com/_videos_t4vn23s9jc5498tgj49icfj4678/%s/%s.flv', $dir, $hash);
+                $self->debug("Decoded flashvars: %s", Dumper(\%h));
+
+                if (exists($h{mp4_url})) {
+                    $metadata->{'DLURL'} = $h{mp4_url};
+                }
+            }
+        }
+    }
 
     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
         $self->error('Could not extract download URL and title');