line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
=encoding iso-8859-1 |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=cut |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
package Acme::MetaSyntactic::RemoteList; |
6
|
19
|
|
|
19
|
|
110
|
use strict; |
|
19
|
|
|
|
|
37
|
|
|
19
|
|
|
|
|
500
|
|
7
|
19
|
|
|
19
|
|
112
|
use warnings; |
|
19
|
|
|
|
|
52
|
|
|
19
|
|
|
|
|
441
|
|
8
|
19
|
|
|
19
|
|
100
|
use Carp; |
|
19
|
|
|
|
|
33
|
|
|
19
|
|
|
|
|
1684
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '1.003'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# method that extracts the items from the remote content and returns them |
13
|
|
|
|
|
|
|
sub extract { |
14
|
3
|
|
66
|
3
|
1
|
396
|
my $class = ref $_[0] || $_[0]; |
15
|
19
|
|
|
19
|
|
111
|
no strict 'refs'; |
|
19
|
|
|
|
|
41
|
|
|
19
|
|
|
|
|
3116
|
|
16
|
3
|
|
|
|
|
6
|
my $func = ${"$class\::Remote"}{extract}; |
|
3
|
|
|
|
|
13
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# provide a very basic default |
19
|
|
|
|
|
|
|
my $meth = ref $func eq 'CODE' |
20
|
10
|
|
|
10
|
|
5105
|
? sub { my %seen; return grep { !$seen{$_}++ } $func->( $_[1], $_[2] ); } |
|
10
|
|
|
|
|
43
|
|
|
46
|
|
|
|
|
258
|
|
21
|
3
|
100
|
|
1
|
|
23
|
: sub { return $_[1] }; # very basic default |
|
1
|
|
|
|
|
4
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# put the method in the subclass symbol table (at runtime) |
24
|
3
|
|
|
|
|
7
|
*{"$class\::extract"} = $meth; |
|
3
|
|
|
|
|
20
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# now run the function^Wmethod |
27
|
3
|
|
|
|
|
12
|
goto &$meth; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# methods related to the source URL |
31
|
|
|
|
|
|
|
sub source { |
32
|
19
|
|
66
|
19
|
1
|
84
|
my $class = ref $_[0] || $_[0]; |
33
|
19
|
|
|
19
|
|
114
|
no strict 'refs'; |
|
19
|
|
|
|
|
38
|
|
|
19
|
|
|
|
|
1384
|
|
34
|
|
|
|
|
|
|
|
35
|
19
|
|
|
|
|
27
|
return ${"$class\::Remote"}{source}; |
|
19
|
|
|
|
|
147
|
|
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub sources { |
39
|
6
|
|
33
|
6
|
1
|
27
|
my $class = ref $_[0] || $_[0]; |
40
|
19
|
|
|
19
|
|
110
|
no strict 'refs'; |
|
19
|
|
|
|
|
34
|
|
|
19
|
|
|
|
|
11294
|
|
41
|
|
|
|
|
|
|
|
42
|
6
|
|
|
|
|
10
|
my $src = ${"$class\::Remote"}{source}; |
|
6
|
|
|
|
|
23
|
|
43
|
6
|
100
|
|
|
|
20
|
if ( ref $src eq 'ARRAY' ) { |
|
|
50
|
|
|
|
|
|
44
|
3
|
|
|
|
|
11
|
return @$src; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
elsif ( ref $src eq 'HASH' ) { |
47
|
|
|
|
|
|
|
return grep $_, |
48
|
|
|
|
|
|
|
defined $_[1] && $_[1] ne ':all' |
49
|
0
|
|
|
|
|
0
|
? ref $_[1] ? @$src{ @{ $_[1] } } |
50
|
0
|
0
|
0
|
|
|
0
|
: $src->{ $_[1] } |
|
|
0
|
|
|
|
|
|
51
|
|
|
|
|
|
|
: values %$src; |
52
|
|
|
|
|
|
|
} |
53
|
3
|
|
|
|
|
7
|
return $src; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
15
|
|
|
15
|
1
|
223
|
sub has_remotelist { return defined $_[0]->source(); } |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# main method: return the list from the remote source |
59
|
|
|
|
|
|
|
sub remote_list { |
60
|
7
|
|
66
|
7
|
1
|
46
|
my $class = ref $_[0] || $_[0]; |
61
|
7
|
100
|
|
|
|
36
|
return unless $class->has_remotelist(); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# check that we can access the network |
64
|
6
|
|
|
|
|
18
|
eval { |
65
|
6
|
|
|
|
|
48
|
require LWP::UserAgent; |
66
|
6
|
50
|
|
|
|
27
|
die "version 5.802 required ($LWP::VERSION installed)\n" |
67
|
|
|
|
|
|
|
if $LWP::VERSION < 5.802; |
68
|
|
|
|
|
|
|
}; |
69
|
6
|
50
|
|
|
|
18
|
if ($@) { |
70
|
0
|
|
|
|
|
0
|
carp "LWP::UserAgent not available: $@"; |
71
|
0
|
|
|
|
|
0
|
return; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# figure out the default category (for an instance) |
75
|
6
|
100
|
33
|
|
|
42
|
my $category = ref $_[0] ? $_[1] || $_[0]->{category} : $_[1]; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# fetch the content |
78
|
6
|
|
|
|
|
11
|
my @items; |
79
|
6
|
|
|
|
|
25
|
my @srcs = $class->sources($category); |
80
|
6
|
|
|
|
|
47
|
my $ua = LWP::UserAgent->new( env_proxy => 1 ); |
81
|
6
|
|
|
|
|
29350
|
foreach my $src (@srcs) { |
82
|
8
|
50
|
|
|
|
59
|
my $request = HTTP::Request->new( |
83
|
|
|
|
|
|
|
ref $src |
84
|
|
|
|
|
|
|
? ( POST => $src->[0], |
85
|
|
|
|
|
|
|
[ content_type => 'application/x-www-form-urlencoded' ], |
86
|
|
|
|
|
|
|
$src->[1] |
87
|
|
|
|
|
|
|
) |
88
|
|
|
|
|
|
|
: ( GET => $src ) |
89
|
|
|
|
|
|
|
); |
90
|
|
|
|
|
|
|
|
91
|
8
|
|
|
|
|
9462
|
my $res = $ua->request( $request ); |
92
|
8
|
100
|
|
|
|
47467
|
if ( ! $res->is_success() ) { |
93
|
2
|
|
|
|
|
27
|
carp "Failed to get content at $src (" . $res->status_line(); |
94
|
2
|
|
|
|
|
643
|
return; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# extract, cleanup and return the data |
98
|
|
|
|
|
|
|
# if decoding the content fails, we just deal with the raw content |
99
|
6
|
|
33
|
|
|
79
|
push @items => |
|
|
|
33
|
|
|
|
|
100
|
|
|
|
|
|
|
$class->extract( $res->decoded_content() || $res->content(), |
101
|
|
|
|
|
|
|
$category || () ); |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# return unique items |
106
|
4
|
|
|
|
|
9
|
my %seen; |
107
|
4
|
|
|
|
|
9
|
return grep { !$seen{$_}++ } @items; |
|
30
|
|
|
|
|
169
|
|
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
# |
111
|
|
|
|
|
|
|
# transformation subroutines |
112
|
|
|
|
|
|
|
# |
113
|
|
|
|
|
|
|
sub tr_nonword { |
114
|
137
|
|
|
137
|
1
|
249
|
my $str = shift; |
115
|
137
|
|
|
|
|
197
|
$str =~ tr/a-zA-Z0-9_/_/c; |
116
|
137
|
|
|
|
|
277
|
$str; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub tr_accent { |
120
|
137
|
|
|
137
|
1
|
33279
|
my $str = shift; |
121
|
137
|
|
|
|
|
201
|
$str =~ tr{ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝàáâãäåçèéêëìíîïñòóôõöøùúûüýÿ} |
122
|
|
|
|
|
|
|
{AAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy}; |
123
|
137
|
|
|
|
|
265
|
return $str; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
my %utf2asc = ( |
127
|
|
|
|
|
|
|
"æ" => 'ae', |
128
|
|
|
|
|
|
|
"Æ" => 'AE', |
129
|
|
|
|
|
|
|
"\xc5\xa0" => 'S', |
130
|
|
|
|
|
|
|
"\x{0160}" => 'S', |
131
|
|
|
|
|
|
|
# for pokemons |
132
|
|
|
|
|
|
|
"\x{0101}" => 'a', |
133
|
|
|
|
|
|
|
"\x{012b}" => 'i', |
134
|
|
|
|
|
|
|
"\x{014d}" => 'o', |
135
|
|
|
|
|
|
|
"\x{016b}" => 'u', |
136
|
|
|
|
|
|
|
"\xe2\x99\x80" => 'female', |
137
|
|
|
|
|
|
|
"\xe2\x99\x82" => 'male', |
138
|
|
|
|
|
|
|
"\x{2640}" => 'female', |
139
|
|
|
|
|
|
|
"\x{2642}" => 'male', |
140
|
|
|
|
|
|
|
); |
141
|
|
|
|
|
|
|
my $utf_re = qr/(@{[join( '|', sort keys %utf2asc )]})/; |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub tr_utf8_basic { |
144
|
0
|
|
|
0
|
1
|
|
my $str = shift; |
145
|
0
|
|
|
|
|
|
$str =~ s/$utf_re/$utf2asc{$1}/go; |
146
|
0
|
|
|
|
|
|
return $str; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
1; |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
__END__ |