Add app.net grabber
[quotesite.git] / quotesite / AppNetGrabber.pm
1 # (c) 2010 by Ralf Ertzinger <ralf@camperquake.de>
2 # licensed under GNU GPL v2
3 #
4 # Grabber for app.net
5
6 package quotesite::AppNetGrabber;
7
8 use quotesite::GrabberBase;
9 @ISA = qw(quotesite::GrabberBase);
10
11 use Data::Dumper;
12 use JSON;
13 use Encode;
14
15 use strict;
16
17 sub new {
18     my $class = shift;
19     my $self = $class->SUPER::new();
20
21     $self->{'NAME'} = 'app.net';
22     $self->{'PATTERNS'} = ['(https?://alpha\.app\.net/[^/]+/post/(\d+))'];
23
24     bless($self, $class);
25     $self->_prepare_parameters();
26
27     return $self;
28 }
29
30 sub _parse {
31     my $self = shift;
32     my $url = shift;
33     my $pattern = shift;
34     my $content;
35     my $metadata = {};
36     my $p = XML::Simple->new();
37     my $t;
38
39     $url =~ m|$pattern|;
40     $url = $1;
41
42     $metadata->{'URL'} = $url;
43     $metadata->{'ID'} = $2;
44     $metadata->{'TYPE'} = 'quote';
45     $metadata->{'SOURCE'} = $self->{'NAME'};
46     $metadata->{'CONTENT'} = undef;
47
48     # Get the JSON file containing the quote
49     unless(defined($content = $self->simple_get(sprintf('https://alpha-api.app.net/stream/0/posts/%s', $2)))) {
50         $self->error('Could not download quote');
51         return undef;
52     }
53
54     unless(defined($t = JSON->new->utf8->decode($content))) {
55         $self->error('Could not parse JSON metadata');
56         return undef;
57     }
58
59     $self->debug("JSON content: %s", Dumper($t));
60
61     $metadata->{'CONTENT'} = $t->{'data'}->{'text'};
62     $metadata->{'ID'} = $t->{'data'}->{'user'}->{'username'};
63
64     unless(defined($metadata->{'CONTENT'})) {
65         $self->error('Could not extract quote content');
66         return undef;
67     }
68
69     return $metadata;
70 }
71
72 1;