Change new() function in plugins to avoid having to call _prepare_parameters() in...
[videosite.git] / videosite / HTTPJSONGetter.pm
1 # (c) 2007 by Ralf Ertzinger <ralf@camperquake.de>
2 # licensed under GNU GPL v2
3 #
4 # A getter which POSTs the JSONified metadata to a remote URL
5
6 package videosite::HTTPJSONGetter;
7
8 use videosite::GetterBase;
9 @ISA = qw(videosite::GetterBase);
10
11 use strict;
12 use LWP::UserAgent;
13 use JSON -support_by_pp;
14
15 sub new {
16     my $class = shift;
17     my $self = $class->SUPER::new(
18         NAME => 'HTTPJSONGetter',
19         _PARAMS => {
20             URL => ['http://www.example.com/getjson.pl', "The URL to call in order to trigger a download. The JSON encoded information will be POSTed to this URL."]
21         },
22     );
23
24     return bless($self, $class);
25 }
26
27 sub get {
28     my $self = shift;
29     my $video = shift;
30     my $j = JSON->new();
31     my $ua = LWP::UserAgent->new('agent' => 'Mozilla/5.0');
32     my $jdata;
33     my $r;
34
35     $jdata = $j->encode($video);
36
37     $self->debug("Encoded metadata to %s", $jdata);
38     $r = $ua->post($self->_getval('URL'), {'json' => $jdata});
39     unless ($r->is_success()) {
40         $self->error("Error calling RPC: %s", $r->code());
41         return 0;
42     }
43
44     return 1;
45 }