line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WWW::BashOrg; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
273804
|
use warnings; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
67
|
|
4
|
2
|
|
|
2
|
|
12
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
92
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '1.001003'; # VERSION |
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
1316
|
use LWP::UserAgent; |
|
2
|
|
|
|
|
57044
|
|
|
2
|
|
|
|
|
53
|
|
9
|
2
|
|
|
2
|
|
900
|
use HTML::TokeParser::Simple; |
|
2
|
|
|
|
|
23297
|
|
|
2
|
|
|
|
|
58
|
|
10
|
2
|
|
|
2
|
|
15
|
use HTML::Entities; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
169
|
|
11
|
2
|
|
|
2
|
|
11
|
use overload q|""| => sub { shift->quote }; |
|
2
|
|
|
1
|
|
5
|
|
|
2
|
|
|
|
|
36
|
|
|
1
|
|
|
|
|
920
|
|
12
|
2
|
|
|
2
|
|
152
|
use base 'Class::Accessor::Grouped'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
2125
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
__PACKAGE__->mk_group_accessors( simple => qw/ |
15
|
|
|
|
|
|
|
ua |
16
|
|
|
|
|
|
|
error |
17
|
|
|
|
|
|
|
quote |
18
|
|
|
|
|
|
|
default_site |
19
|
|
|
|
|
|
|
/); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub new { |
22
|
1
|
|
|
1
|
1
|
14
|
my $class = shift; |
23
|
1
|
|
|
|
|
3
|
my %args = @_; |
24
|
|
|
|
|
|
|
|
25
|
1
|
50
|
|
|
|
16
|
$args{ua} = LWP::UserAgent->new( |
26
|
|
|
|
|
|
|
agent => 'Opera 9.5', |
27
|
|
|
|
|
|
|
timeout => 30, |
28
|
|
|
|
|
|
|
) unless defined $args{ua}; |
29
|
1
|
|
50
|
|
|
4662
|
$args{default_site} ||= 'bash'; |
30
|
|
|
|
|
|
|
|
31
|
1
|
|
|
|
|
5
|
my $self = bless {}, $class; |
32
|
|
|
|
|
|
|
|
33
|
1
|
|
|
|
|
12
|
$self->$_( $args{ $_ } ) for keys %args; |
34
|
|
|
|
|
|
|
|
35
|
1
|
|
|
|
|
521
|
return $self; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub get_quote { |
39
|
1
|
|
|
1
|
1
|
935
|
my ( $self, $num, $site ) = @_; |
40
|
|
|
|
|
|
|
|
41
|
1
|
|
|
|
|
14
|
$site = $self->_normalise_site($site); |
42
|
1
|
|
|
|
|
7
|
$self->quote( undef ); |
43
|
1
|
|
|
|
|
221
|
$self->error( undef ); |
44
|
|
|
|
|
|
|
|
45
|
1
|
50
|
33
|
|
|
217
|
unless ( length $num and $num =~ /^\d+$/ ) { |
46
|
0
|
|
|
|
|
0
|
$self->error('Invalid quote number'); |
47
|
0
|
|
|
|
|
0
|
return; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
1
|
50
|
|
|
|
119
|
my $res = $self->{ua}->get( ( ($site eq 'bash') ? "http://bash.org/?quote=" : "http://www.qdb.us/" ) . $num ); |
51
|
1
|
50
|
|
|
|
417644
|
unless ( $res->is_success ) { |
52
|
0
|
|
|
|
|
0
|
$self->error("Network error: " . $res->status_line ); |
53
|
0
|
|
|
|
|
0
|
return; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
1
|
|
|
|
|
25
|
my $quote = ( $self->_parse_quote( $res->decoded_content, $site ) )[0]; |
57
|
1
|
50
|
|
|
|
14
|
unless ( defined $quote ) { |
58
|
0
|
|
|
|
|
0
|
$self->error('Quote not found'); |
59
|
0
|
|
|
|
|
0
|
return; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
1
|
|
|
|
|
76
|
return $self->quote( $quote ); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub random { |
66
|
0
|
|
|
0
|
1
|
0
|
my ($self, $site) = @_; |
67
|
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
0
|
$site = $self->_normalise_site($site); |
69
|
0
|
|
|
|
|
0
|
$self->quote( undef ); |
70
|
0
|
|
|
|
|
0
|
$self->error( undef ); |
71
|
|
|
|
|
|
|
|
72
|
0
|
0
|
|
|
|
0
|
unless ( @{ $self->{'cache'.$site} || [] } ) { |
|
0
|
0
|
|
|
|
0
|
|
73
|
0
|
0
|
|
|
|
0
|
my $res = $self->{ua}->get( |
74
|
|
|
|
|
|
|
$site eq 'bash' |
75
|
|
|
|
|
|
|
? "http://bash.org/?random1" |
76
|
|
|
|
|
|
|
: "http://www.qdb.us/random" |
77
|
|
|
|
|
|
|
); |
78
|
|
|
|
|
|
|
|
79
|
0
|
0
|
|
|
|
0
|
unless ( $res->is_success ) { |
80
|
0
|
|
|
|
|
0
|
$self->error("Network error: " . $res->status_line ); |
81
|
0
|
|
|
|
|
0
|
return; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
0
|
|
|
|
|
0
|
@{ $self->{'cache'.$site} } |
|
0
|
|
|
|
|
0
|
|
85
|
|
|
|
|
|
|
= $self->_parse_quote( $res->decoded_content, $site ); |
86
|
|
|
|
|
|
|
|
87
|
0
|
0
|
|
|
|
0
|
unless ( @{ $self->{'cache'.$site} } ) { |
|
0
|
|
|
|
|
0
|
|
88
|
0
|
|
|
|
|
0
|
$self->error('Quote not found'); |
89
|
0
|
|
|
|
|
0
|
return; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
0
|
|
|
|
|
0
|
return $self->quote( pop @{ $self->{'cache'.$site} } ); |
|
0
|
|
|
|
|
0
|
|
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub _parse_quote { |
97
|
1
|
|
|
1
|
|
68730
|
my ( $self, $content ) = @_; |
98
|
|
|
|
|
|
|
|
99
|
1
|
|
|
|
|
15
|
my $p = HTML::TokeParser::Simple->new( \$content ); |
100
|
|
|
|
|
|
|
|
101
|
1
|
|
|
|
|
228
|
my $get_quote; |
102
|
|
|
|
|
|
|
my $quote; |
103
|
0
|
|
|
|
|
0
|
my @quotes; |
104
|
1
|
|
|
|
|
9
|
while ( my $t = $p->get_token ) { |
105
|
248
|
100
|
66
|
|
|
9534
|
if ( ( $t->is_start_tag('p') || $t->is_start_tag('span') ) |
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
106
|
|
|
|
|
|
|
and defined $t->get_attr('class') |
107
|
|
|
|
|
|
|
and $t->get_attr('class') eq 'qt' |
108
|
|
|
|
|
|
|
) { |
109
|
1
|
|
|
|
|
43
|
$get_quote = 1; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
248
|
100
|
100
|
|
|
4006
|
if ( $get_quote and $t->is_text ) { |
113
|
2
|
|
|
|
|
14
|
$quote .= $t->as_is; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
248
|
100
|
66
|
|
|
1203
|
if ( $get_quote and ( $t->is_end_tag('p') || $t->is_end_tag('span') ) ) { |
|
|
|
66
|
|
|
|
|
117
|
1
|
|
|
|
|
30
|
$quote =~ s/ / /g; |
118
|
1
|
|
|
|
|
31
|
push @quotes, decode_entities $quote; |
119
|
1
|
|
|
|
|
5
|
$quote = ''; $get_quote = 0; |
|
1
|
|
|
|
|
7
|
|
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
1
|
|
|
|
|
41
|
return @quotes; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub _normalise_site { |
127
|
1
|
|
|
1
|
|
2
|
my ( $self, $site ) = @_; |
128
|
1
|
|
33
|
|
|
15
|
$site ||= $self->default_site; |
129
|
1
|
50
|
33
|
|
|
6
|
( $site ne 'bash' && $site ne 'qdb' ) and $site = $self->default_site; |
130
|
1
|
|
|
|
|
4
|
return $site; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
1; |
134
|
|
|
|
|
|
|
__END__ |