Add HTTPJSONGetter
authorRalf Ertzinger <ralf@skytale.net>
Fri, 27 May 2011 07:21:20 +0000 (09:21 +0200)
committerRalf Ertzinger <ralf@skytale.net>
Fri, 27 May 2011 07:21:20 +0000 (09:21 +0200)
videosite/HTTPJSONGetter.pm [new file with mode: 0644]

diff --git a/videosite/HTTPJSONGetter.pm b/videosite/HTTPJSONGetter.pm
new file mode 100644 (file)
index 0000000..87a6d83
--- /dev/null
@@ -0,0 +1,46 @@
+# (c) 2007 by Ralf Ertzinger <ralf@camperquake.de>
+# licensed under GNU GPL v2
+#
+# A getter which POSTs the JSONified metadata to a remote URL
+
+package videosite::HTTPJSONGetter;
+
+use videosite::GetterBase;
+@ISA = qw(videosite::GetterBase);
+
+use strict;
+use LWP::UserAgent;
+use JSON -support_by_pp;
+
+sub new {
+    my $class = shift;
+    my $self = $class->SUPER::new();
+
+    $self->{'NAME'} = 'HTTPJSONGetter';
+    $self->{'_PARAMS'} = {'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."]};
+
+    bless($self, $class);
+    $self->_prepare_parameters();
+
+    return $self;
+}
+
+sub get {
+    my $self = shift;
+    my $video = shift;
+    my $j = JSON->new();
+    my $ua = LWP::UserAgent->new('agent' => 'Mozilla/5.0');
+    my $jdata;
+    my $r;
+
+    $jdata = $j->encode($video);
+
+    $self->debug("Encoded metadata to %s", $jdata);
+    $r = $ua->post($self->_getval('URL'), {'json' => $jdata});
+    unless ($r->is_success()) {
+        $self->error("Error calling RPC: %s", $r->code());
+        return 0;
+    }
+
+    return 1;
+}