f9c73878b20955d88f7da7fa9bd1fc0727fa7423
[quotesite.git] / quotesite / QdbGrabber.pm
1 # (c) 2007 by Ralf Ertzinger <ralf@camperquake.de>
2 # licensed under GNU GPL v2
3 #
4 # Grabber for qdb.us
5
6 package quotesite::QdbGrabber;
7
8 use quotesite::GrabberBase;
9 @ISA = qw(quotesite::GrabberBase);
10
11 use LWP::Simple qw(!get);
12 use HTML::TokeParser;
13 use Data::Dumper;
14
15 use strict;
16
17 sub new {
18     my $class = shift;
19     my $self = $class->SUPER::new();
20
21     $self->{'NAME'} = 'qdb.us';
22     $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*qdb\.us/(\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;
37     my $t;
38     my $t2;
39
40     $url =~ m|$pattern|;
41     $url = $1;
42
43     $metadata->{'URL'} = $url;
44     $metadata->{'ID'} = $2;
45     $metadata->{'TYPE'} = 'quote';
46     $metadata->{'SOURCE'} = $self->{'NAME'};
47     $metadata->{'CONTENT'} = undef;
48
49     # Get the HTML file containing the quote
50     unless(defined($content = LWP::Simple::get(sprintf('http://qdb.us/?%s', $2)))) {
51         $self->error('Could not download quote');
52         return undef;
53     }
54
55     $p = HTML::TokeParser->new(\$content);
56
57     OUTER: while ($t = $p->get_tag('span')) {
58         if (exists($t->[1]->{'class'}) && ($t->[1]->{'class'} eq 'qt')) {
59             $metadata->{'CONTENT'} = $p->get_text('/span');
60             last OUTER;
61         }
62     }
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;