add connector support to AsyncFileGetter
[videosite.git] / videosite / RedTubeGrabber.pm
1 # (c) 2008 by Ralf Ertzinger <ralf@camperquake.de>
2 # licensed under GNU GPL v2
3 #
4 # Grabber for redtube.com
5 #
6 # Algorithm for the file name hash reverse engineered by
7 # Maximilian Rehkopf  <otakon at gmx dot net>
8
9 package videosite::RedTubeGrabber;
10
11 use videosite::GrabberBase;
12 @ISA = qw(videosite::GrabberBase);
13
14 use LWP::UserAgent;
15 use HTTP::Cookies;
16 use HTML::TokeParser;
17 use Data::Dumper;
18
19 use strict;
20
21 sub new {
22     my $class = shift;
23     my $self = $class->SUPER::new();
24
25     $self->{'NAME'} = 'redtube';
26     $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*redtube.com/(\d+))'];
27
28     bless($self, $class);
29     $self->_prepare_parameters();
30
31     return $self;
32 }
33
34 sub div($$) {
35     return ($_[0] - ($_[0] % $_[1])) / $_[1];
36 }
37
38 sub digitatindex($$) {
39     return div($_[0], 10**$_[1]) % 10;
40 }
41
42 sub mkfilename($) {
43     my $id = shift;
44     my $i = 7;
45     my $q = 0;
46     my $q2 = 0;
47     my @key = split(//, "R15342O7K9HBCDXFGAIJ8LMZ6PQ0STUVWEYN");
48     my $hash = "";
49
50     # Calculate a weighed digit sum of the id
51     $q += ($_*($i--)) for reverse(split(//, $id));
52
53     # Now calculate the digit sum of the digit sum
54     $q2 += $_ for split(//, $q);
55
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];
67
68     return (sprintf("%07d", $id/1000), $hash);
69 }
70
71 sub _parse {
72     my $self = shift;
73     my $url = shift;
74     my $pattern = shift;
75     my $jar = HTTP::Cookies->new();
76     my $ua = LWP::UserAgent->new('agent' => 'Mozilla/5.0');
77     my $content;
78     my $metadata = {};
79     my $p;
80     my $r;
81     my $dir;
82     my $hash;
83
84     $url =~ m|$pattern|;
85     $url = $1;
86
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;
93
94     # Set the cookies necessary to get the video data
95     $jar->set_cookie(undef, 'pp', '1', '/', '.redtube.com');
96     $ua->cookie_jar($jar);
97
98     unless(defined($r = $ua->get(sprintf("http://www.redtube.com/%s", $2)))) {
99         $self->error('Could not download page');
100         return undef;
101     }
102
103     # Get the site to extract the title
104     $content = $r->decoded_content();
105
106     $p = HTML::TokeParser->new(\$content);
107
108     # Look for the title
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 - //;
114         }
115     }
116
117     # Redtube uses a selfmade hash system to create the filename
118     ($dir, $hash) = mkfilename($metadata->{'ID'});
119
120     $metadata->{'DLURL'} = sprintf('http://dl.redtube.com/_videos_t4vn23s9jc5498tgj49icfj4678/%s/%s.flv', $dir, $hash);
121
122     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
123         $self->error('Could not extract download URL and title');
124         return undef;
125     }
126
127     return $metadata;
128 }
129
130 1;