Twitter: no need to encode/decode string
[quotesite.git] / quotesite / BashOrgGrabber.pm
1 # (c) 2007 by Ralf Ertzinger <ralf@camperquake.de>
2 # licensed under GNU GPL v2
3 #
4 # Grabber for bash.org
5
6 package BashOrgGrabber;
7
8 use GrabberBase;
9 @ISA = qw(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'} = 'bash.org';
22     $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*bash\.org/\?(\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
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 HTML file containing the quote
49     unless(defined($content = LWP::Simple::get(sprintf('http://bash.org/?%s', $2)))) {
50         $self->error('Could not download quote');
51         return undef;
52     }
53
54     $p = HTML::TokeParser->new(\$content);
55
56     while ($t = $p->get_tag('p')) {
57         if (exists($t->[1]->{'class'}) && ($t->[1]->{'class'} eq 'qt')) {
58             $metadata->{'CONTENT'} = $p->get_text('/p');
59         }
60     }
61
62     unless(defined($metadata->{'CONTENT'})) {
63         $self->error('Could not extract quote content');
64         return undef;
65     }
66
67     return $metadata;
68 }
69
70 1;