Use API for output
[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 HTML::TokeParser;
15 use Data::Dumper;
16
17 use strict;
18
19 sub new {
20     my $class = shift;
21     my $self = $class->SUPER::new(
22         NAME => 'redtube',
23         _SELFTESTURL => 'http://www.redtube.com/8269',
24         _SELFTESTTITLE => 'Porn bloopers with pretty girl',
25         PATTERNS => ['(http://(?:[-a-zA-Z0-9_.]+\.)*redtube.com/(\d+))'],
26     );
27
28     return bless($self, $class);
29 }
30
31 sub div($$) {
32     return ($_[0] - ($_[0] % $_[1])) / $_[1];
33 }
34
35 sub digitatindex($$) {
36     return div($_[0], 10**$_[1]) % 10;
37 }
38
39 sub mkfilename($) {
40     my $id = shift;
41     my $i = 7;
42     my $q = 0;
43     my $q2 = 0;
44     my @key = split(//, "R15342O7K9HBCDXFGAIJ8LMZ6PQ0STUVWEYN");
45     my $hash = "";
46
47     # Calculate a weighed digit sum of the id
48     $q += ($_*($i--)) for reverse(split(//, $id));
49
50     # Now calculate the digit sum of the digit sum
51     $q2 += $_ for split(//, $q);
52
53     # The rest are lookups into @key and the second digit sum,
54     # based on the second digit sum and the original id
55     $hash .= $key[digitatindex($id,3)+$q2+3];
56     $hash .= digitatindex($q2, 0);
57     $hash .= $key[digitatindex($id,6)+$q2+2];
58     $hash .= $key[digitatindex($id,4)+$q2+1];
59     $hash .= $key[digitatindex($id,1)+$q2+6];
60     $hash .= $key[digitatindex($id,5)+$q2+5];
61     $hash .= digitatindex($q2, 1);
62     $hash .= $key[digitatindex($id,2)+$q2+7];
63     $hash .= $key[digitatindex($id,0)+$q2+4];
64
65     return (sprintf("%07d", $id/1000), $hash);
66 }
67
68 sub _parse {
69     my $self = shift;
70     my $url = shift;
71     my $pattern = shift;
72     my $ua = $self->ua();
73     my $content;
74     my $metadata = {};
75     my $p;
76     my $r;
77     my $tag;
78     my $dir;
79     my $hash;
80
81     $url =~ m|$pattern|;
82     $url = $1;
83
84     $metadata->{'URL'} = $url;
85     $metadata->{'ID'} = $2;
86     $metadata->{'TYPE'} = 'video';
87     $metadata->{'SOURCE'} = $self->{'NAME'};
88     $metadata->{'TITLE'} = undef;
89     $metadata->{'DLURL'} = undef;
90
91     # Set the cookies necessary to get the video data
92     $ua->cookie_jar->set_cookie(undef, 'pp', '1', '/', '.redtube.com');
93
94     unless(defined($content = $self->simple_get(sprintf("http://www.redtube.com/%s", $2), $ua))) {
95         $self->error('Could not download page');
96         return undef;
97     }
98
99     $p = HTML::TokeParser->new(\$content);
100
101     # Look for the title
102     while ($tag = $p->get_tag('title', 'script')) {
103         if ('title' eq $tag->[0]) {
104             my $t = $p->get_text();
105             $metadata->{'TITLE'} = $t;
106             $metadata->{'TITLE'} =~ s/ \| Redtube.*//;
107         } elsif ('script' eq $tag->[0]) {
108             my $t = $p->get_text();
109
110             if ($t =~ m|so\.addParam\("flashvars","([^\x22]+)"|) {
111                 my %h;
112
113                 $self->debug("Found flashvars: %s", $1);
114                 %h = map { $self->decode_hexurl($_) } split(/[&=]/, $1);
115
116                 $self->debug("Decoded flashvars: %s", Dumper(\%h));
117
118                 if (exists($h{mp4_url})) {
119                     $metadata->{'DLURL'} = $h{mp4_url};
120                 }
121             }
122         }
123     }
124
125     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
126         $self->error('Could not extract download URL and title');
127         return undef;
128     }
129
130     return $metadata;
131 }
132
133 1;