Add app.net grabber
[quotesite.git] / quotesite / AppNetGrabber.pm
diff --git a/quotesite/AppNetGrabber.pm b/quotesite/AppNetGrabber.pm
new file mode 100644 (file)
index 0000000..d92edec
--- /dev/null
@@ -0,0 +1,72 @@
+# (c) 2010 by Ralf Ertzinger <ralf@camperquake.de>
+# licensed under GNU GPL v2
+#
+# Grabber for app.net
+
+package quotesite::AppNetGrabber;
+
+use quotesite::GrabberBase;
+@ISA = qw(quotesite::GrabberBase);
+
+use Data::Dumper;
+use JSON;
+use Encode;
+
+use strict;
+
+sub new {
+    my $class = shift;
+    my $self = $class->SUPER::new();
+
+    $self->{'NAME'} = 'app.net';
+    $self->{'PATTERNS'} = ['(https?://alpha\.app\.net/[^/]+/post/(\d+))'];
+
+    bless($self, $class);
+    $self->_prepare_parameters();
+
+    return $self;
+}
+
+sub _parse {
+    my $self = shift;
+    my $url = shift;
+    my $pattern = shift;
+    my $content;
+    my $metadata = {};
+    my $p = XML::Simple->new();
+    my $t;
+
+    $url =~ m|$pattern|;
+    $url = $1;
+
+    $metadata->{'URL'} = $url;
+    $metadata->{'ID'} = $2;
+    $metadata->{'TYPE'} = 'quote';
+    $metadata->{'SOURCE'} = $self->{'NAME'};
+    $metadata->{'CONTENT'} = undef;
+
+    # Get the JSON file containing the quote
+    unless(defined($content = $self->simple_get(sprintf('https://alpha-api.app.net/stream/0/posts/%s', $2)))) {
+        $self->error('Could not download quote');
+        return undef;
+    }
+
+    unless(defined($t = JSON->new->utf8->decode($content))) {
+        $self->error('Could not parse JSON metadata');
+        return undef;
+    }
+
+    $self->debug("JSON content: %s", Dumper($t));
+
+    $metadata->{'CONTENT'} = $t->{'data'}->{'text'};
+    $metadata->{'ID'} = $t->{'data'}->{'user'}->{'username'};
+
+    unless(defined($metadata->{'CONTENT'})) {
+        $self->error('Could not extract quote content');
+        return undef;
+    }
+
+    return $metadata;
+}
+
+1;