Change new() function in plugins to avoid having to call _prepare_parameters() in...
[videosite.git] / videosite / WimpGrabber.pm
1 # (c) 2009 by Ralf Ertzinger <ralf@camperquake.de>
2 # licensed under GNU GPL v2
3 #
4 # Grabber for wimp.com
5
6 package videosite::WimpGrabber;
7
8 use videosite::GrabberBase;
9 @ISA = qw(videosite::GrabberBase);
10
11 use MIME::Base64;
12 use HTML::Parser;
13 use Data::Dumper;
14
15 use strict;
16
17 sub new {
18     my $class = shift;
19     my $self = $class->SUPER::new(
20         NAME => 'wimp',
21         _SELFTESTURL => 'http://www.wimp.com/insanebuilding/',
22         _SELFTESTTITLE => 'Insane building.',
23         PATTERNS => ['(http://(?:[-a-zA-Z0-9_.]+\.)*wimp.com/([^/]+)/?)'],
24     );
25
26     return bless($self, $class);
27 }
28
29 sub _parse {
30     my $self = shift;
31     my $url = shift;
32     my $pattern = shift;
33     my $content;
34     my $metadata = {};
35     my $p = HTML::Parser->new(api_version => 3);
36     my @accum;
37     my @text;
38     my $e;
39
40     $url =~ m|$pattern|;
41     $url = $1;
42
43     $metadata->{'URL'} = $url;
44     $metadata->{'ID'} = $2;
45     $metadata->{'TYPE'} = 'video';
46     $metadata->{'SOURCE'} = $self->{'NAME'};
47     $metadata->{'TITLE'} = undef;
48     $metadata->{'DLURL'} = undef;
49
50     unless(defined($content = $self->simple_get(sprintf('http://www.wimp.com/%s', $2)))) {
51         $self->error('Could not download %s', $url);
52         return undef;
53     }
54
55     $p->handler(start => \@accum, "tagname, attr");
56     $p->handler(text => \@text, "text");
57     $p->report_tags(qw(meta script));
58     $p->utf8_mode(1);
59     $p->parse($content);
60
61     # Look for the title in the meta tags
62     foreach $e (@accum) {
63         if ('meta' eq $e->[0] and exists($e->[1]->{'name'})) {
64             if ('description' eq $e->[1]->{'name'}) {
65                 $metadata->{'TITLE'} = $e->[1]->{'content'};
66                 last;
67             }
68         }
69     }
70
71     # Look for the download URL
72     # This is obfuscated a little bit.
73     foreach $e (@text) {
74         if ($e->[0] =~ m|var googleCode = '([^\x27]+)'|) {
75             my $c = $1;
76
77             $self->debug("Found obfuscated code: %s", $c);
78             my $d = decode_base64($c);
79             $self->debug("Decoded to: %s", $d);
80
81             if ($d =~ m|\.addVariable\("file",\s*"([^\x22]+)"\)|) {
82                 $metadata->{'DLURL'} = $1;
83             }
84         }
85     }
86
87     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
88         $self->error('Could not determine download URL');
89         return undef;
90     }
91
92     return $metadata;
93 }
94
95 1;