1 # (c) 2008 by Ralf Ertzinger <ralf@camperquake.de>
2 # licensed under GNU GPL v2
4 # Grabber for redtube.com
6 # Algorithm for the file name hash reverse engineered by
7 # Maximilian Rehkopf <otakon at gmx dot net>
9 package videosite::RedTubeGrabber;
11 use videosite::GrabberBase;
12 @ISA = qw(videosite::GrabberBase);
23 my $self = $class->SUPER::new();
25 $self->{'NAME'} = 'redtube';
26 $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*redtube.com/(\d+))'];
29 $self->_prepare_parameters();
35 return ($_[0] - ($_[0] % $_[1])) / $_[1];
38 sub digitatindex($$) {
39 return div($_[0], 10**$_[1]) % 10;
47 my @key = split(//, "R15342O7K9HBCDXFGAIJ8LMZ6PQ0STUVWEYN");
50 # Calculate a weighed digit sum of the id
51 $q += ($_*($i--)) for reverse(split(//, $id));
53 # Now calculate the digit sum of the digit sum
54 $q2 += $_ for split(//, $q);
56 # The rest are lookups into @key and the second digit sum,
57 # based on the second digit sum and the original id
58 $hash .= $key[digitatindex($id,3)+$q2+3];
59 $hash .= digitatindex($q2, 0);
60 $hash .= $key[digitatindex($id,6)+$q2+2];
61 $hash .= $key[digitatindex($id,4)+$q2+1];
62 $hash .= $key[digitatindex($id,1)+$q2+6];
63 $hash .= $key[digitatindex($id,5)+$q2+5];
64 $hash .= digitatindex($q2, 1);
65 $hash .= $key[digitatindex($id,2)+$q2+7];
66 $hash .= $key[digitatindex($id,0)+$q2+4];
68 return (sprintf("%07d", $id/1000), $hash);
75 my $jar = HTTP::Cookies->new();
76 my $ua = LWP::UserAgent->new('agent' => 'Mozilla/5.0');
87 $metadata->{'URL'} = $url;
88 $metadata->{'ID'} = $2;
89 $metadata->{'TYPE'} = 'video';
90 $metadata->{'SOURCE'} = $self->{'NAME'};
91 $metadata->{'TITLE'} = undef;
92 $metadata->{'DLURL'} = undef;
94 # Set the cookies necessary to get the video data
95 $jar->set_cookie(undef, 'pp', '1', '/', '.redtube.com');
96 $ua->cookie_jar($jar);
98 unless(defined($r = $ua->get(sprintf("http://www.redtube.com/%s", $2)))) {
99 $self->error('Could not download page');
103 # Get the site to extract the title
104 $content = $r->decoded_content();
106 $p = HTML::TokeParser->new(\$content);
109 if ($p->get_tag('title')) {
110 my $t = $p->get_text();
111 if ($t =~ /\xa0RedTube - /) {
112 $metadata->{'TITLE'} = $t;
113 $metadata->{'TITLE'} =~ s/\xa0RedTube - //;
117 # Redtube uses a selfmade hash system to create the filename
118 ($dir, $hash) = mkfilename($metadata->{'ID'});
120 $metadata->{'DLURL'} = sprintf('http://dl.redtube.com/_videos_t4vn23s9jc5498tgj49icfj4678/%s/%s.flv', $dir, $hash);
122 unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
123 $self->error('Could not extract download URL and title');