Change new() function in plugins to avoid having to call _prepare_parameters() in...
[videosite.git] / videosite / HTTPRPCGetter.pm
1 # (c) 2007 by Ralf Ertzinger <ralf@camperquake.de>
2 # licensed under GNU GPL v2
3 #
4 # A getter which calls a remote URL in order to trigger a 
5 # download.
6
7 package videosite::HTTPRPCGetter;
8
9 use videosite::GetterBase;
10 @ISA = qw(videosite::GetterBase);
11
12 use strict;
13 use LWP::Simple qw(!get);
14
15 sub new {
16     my $class = shift;
17     my $self = $class->SUPER::new(
18         NAME => 'HTTPRPCGetter',
19         _PARAMS => {
20             URL => ['http://www.example.com/get.pl?type=%s&vid=%s&title=%s&url=%s', "The URL to call in order to trigger a download. This is a string which is passed to a sprintf call later on. The parameters passed to that sprintf call, in order, are:\n- The site the video is from\n- The ID of the video\n- The title of the video\n- The URL of the video file itself\n- The URL of the site the video was taken from\nAll parameters are hexencoded"]
21         },
22     );
23
24     return bless($self, $class);
25 }
26
27 sub get {
28     my $self = shift;
29     my $video = shift;
30     my $callurl;
31
32     $callurl = sprintf($self->_getval('URL'),
33         $self->_encode($video->{'SOURCE'}),
34         $self->_encode($video->{'ID'}),
35         $self->_encode($video->{'TITLE'}),
36         $self->_encode($video->{'DLURL'}),
37         $self->_encode($video->{'URL'}));
38
39     $self->debug('Going to call %s', $callurl);
40
41     unless(defined(LWP::Simple::get($callurl))) {
42         $self->error("Error calling RPC");
43         return 0;
44     }
45
46     return 1;
47 }
48
49 sub _encode {
50     my $self = shift;
51     my $s = shift;
52
53     $s =~ s/(.)/sprintf("%%%02x", ord($1))/ge;
54
55     return $s;
56 }