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