Merge branch 'master' of ssh://git.camperquake.de:22003/quotesite
[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 use Encode;
15
16 use strict;
17
18 sub new {
19     my $class = shift;
20     my $self = $class->SUPER::new();
21
22     $self->{'NAME'} = 'qdb.us';
23     $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*qdb\.us/(\d+))'];
24
25     bless($self, $class);
26     $self->_prepare_parameters();
27
28     return $self;
29 }
30
31 sub _parse {
32     my $self = shift;
33     my $url = shift;
34     my $pattern = shift;
35     my $content;
36     my $metadata = {};
37     my $p;
38     my $t;
39     my $t2;
40
41     $url =~ m|$pattern|;
42     $url = $1;
43
44     $metadata->{'URL'} = $url;
45     $metadata->{'ID'} = $2;
46     $metadata->{'TYPE'} = 'quote';
47     $metadata->{'SOURCE'} = $self->{'NAME'};
48     $metadata->{'CONTENT'} = undef;
49
50     # Get the HTML file containing the quote
51     unless(defined($content = LWP::Simple::get(sprintf('http://qdb.us/?%s', $2)))) {
52         $self->error('Could not download quote');
53         return undef;
54     }
55
56     $p = HTML::TokeParser->new(\$content);
57
58     OUTER: while ($t = $p->get_tag('span')) {
59         if (exists($t->[1]->{'class'}) && ($t->[1]->{'class'} eq 'qt')) {
60             $metadata->{'CONTENT'} = encode('utf8', decode('iso8859-1', $p->get_text('/span')));
61             last OUTER;
62         }
63     }
64
65     unless(defined($metadata->{'CONTENT'})) {
66         $self->error('Could not extract quote content');
67         return undef;
68     }
69
70     return $metadata;
71 }
72
73 1;