From: Ralf Ertzinger Date: Fri, 19 Apr 2013 11:56:50 +0000 (+0200) Subject: Add app.net grabber X-Git-Url: https://git.camperquake.de/gitweb.cgi?p=quotesite.git;a=commitdiff_plain;h=c403344f7922b572d714f1b79b4d9ba6bec062a4 Add app.net grabber --- diff --git a/quotesite/AppNetGrabber.pm b/quotesite/AppNetGrabber.pm new file mode 100644 index 0000000..d92edec --- /dev/null +++ b/quotesite/AppNetGrabber.pm @@ -0,0 +1,72 @@ +# (c) 2010 by Ralf Ertzinger +# 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;