| 1 | # | ||
| 2 | # | ||
| 3 | # Creates a new interface class based on the following call - | ||
| 4 | # | ||
| 5 | # package WWW::Scraper::Request::Job; | ||
| 6 | # sub new { | ||
| 7 | # shift; | ||
| 8 | # my $self = WWW::Scraper::Request::new( | ||
| 9 | # 'Job', | ||
| 10 | # ,{ | ||
| 11 | # 'skills' => '' | ||
| 12 | # ,'locations' => '' | ||
| 13 | # ,'payrate' => '' | ||
| 14 | # } | ||
| 15 | # ,@_ | ||
| 16 | # ); | ||
| 17 | # return $self; | ||
| 18 | # } | ||
| 19 | # | ||
| 20 | |||
| 21 | package WWW::Scraper::Request; | ||
| 22 | |||
| 23 | use strict; | ||
| 24 | use vars qw($VERSION @ISA); | ||
| 25 | $VERSION = sprintf("%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/); | ||
| 26 | |||
| 27 | my %AlreadyDeclared; | ||
| 28 | my $VirtualCount; | ||
| 29 | |||
| 30 | sub fieldCapture { | ||
| 31 | my ($scaffold) = @_; | ||
| 32 | my @fields; | ||
| 33 | my $next_scaffold; | ||
| 34 | return @fields; | ||
| 35 | } | ||
| 36 | |||
| 37 | |||
| 38 | sub new { | ||
| 39 | my $class = shift; | ||
| 40 | my $self; | ||
| 41 | |||
| 42 | my ($options, $scraperFieldsFrame); | ||
| 43 | my ($SubClass, $nativeQuery, $isCanonical, $SubClassNormal, $declaredMethodNames); | ||
| 44 | |||
| 45 | # This gives us a subClass name via "Scraper::Request::new('subClassName')"; rather than "new Scraper::Request()", that is. | ||
| 46 | if ( $class ne 'WWW::Scraper::Request' ) { | ||
| 47 | $SubClass = $class; | ||
| 48 | $isCanonical = $SubClass; | ||
| 49 | $SubClassNormal = "$SubClass\_"; | ||
| 50 | $SubClass .= '::_canonical_'; | ||
| 51 | } | ||
| 52 | |||
| 53 | while ( my $whatzit = shift ) { | ||
| 54 | if ( my $rf = ref $whatzit ) { | ||
| 55 | if ( 'HASH' eq $rf ) { | ||
| 56 | map { $options->{$_} = $whatzit->{$_} } keys %$whatzit; | ||
| 57 | } | ||
| 58 | elsif ( 'ARRAY' eq $rf ) { | ||
| 59 | $declaredMethodNames = $whatzit unless $declaredMethodNames; # not sure why the "unless" is necessary . . . | ||
| 60 | } | ||
| 61 | elsif ( $rf =~ m/^WWW::Scraper::([^:]*)$/ ) { | ||
| 62 | my $scraperRequest = $whatzit->scraperRequest(); | ||
| 63 | if ( !$whatzit->_wantsNativeRequest() and $scraperRequest->{'defaultRequestClass'} ) { | ||
| 64 | $SubClass = $scraperRequest->{'defaultRequestClass'}; | ||
| 65 | $SubClassNormal = "$SubClass" unless $SubClassNormal; | ||
| 66 | eval "use WWW::Scraper::Request::$SubClass;\$self = new WWW::Scraper::Request::$SubClassNormal\(\@_);"; | ||
| 67 | die $@ if $@; | ||
| 68 | $isCanonical = $SubClass; | ||
| 69 | $SubClassNormal = "$SubClass"; | ||
| 70 | $SubClass .= '::_default_'; | ||
| 71 | } else { | ||
| 72 | $scraperFieldsFrame = $scraperRequest->{'nativeDefaults'}; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | elsif ( $whatzit eq 'ClassName' ) { | ||
| 78 | $SubClass = shift; | ||
| 79 | $isCanonical = $SubClass; | ||
| 80 | $SubClassNormal = "$SubClass::__"; | ||
| 81 | $SubClass .= '::_ClassName_'; | ||
| 82 | } | ||
| 83 | else { | ||
| 84 | $nativeQuery = $whatzit; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | unless ( $SubClass ) { | ||
| 89 | $VirtualCount += 1; | ||
| 90 | $SubClassNormal = "virtual_$VirtualCount"; | ||
| 91 | $SubClass = "$SubClassNormal\:\:_struct_"; | ||
| 92 | } | ||
| 93 | |||
| 94 | $SubClass = "::$SubClass"; | ||
| 95 | $AlreadyDeclared{"::$SubClass"} = [(keys %$options)+11, $options, $isCanonical] if $self; | ||
| 96 | |||
| 97 | unless ( $scraperFieldsFrame ) { | ||
| 98 | $scraperFieldsFrame = { '_native_query' => 1 }; | ||
| 99 | map { $scraperFieldsFrame->{$_} = 1 } keys %$options; | ||
| 100 | } | ||
| 101 | map { $scraperFieldsFrame->{$_} = 1 } @$declaredMethodNames; | ||
| 102 | my (%subFields,$countSubFields); | ||
| 103 | unless ( $AlreadyDeclared{$SubClass} ) { | ||
| 104 | # $subFields{'url'} = 1 if $SubClass eq '::Sherlock'; # Help Sherlock along. | ||
| 105 | # $subFields{'detail'} = 1 if $SubClass eq '::Sherlock'; # Help Sherlock along. | ||
| 106 | |||
| 107 | # value of {'fieldName'} == 1 means field is from searchResultsFrame, only | ||
| 108 | my %subFieldsMethod; | ||
| 109 | for ( keys %$scraperFieldsFrame ) { | ||
| 110 | next if m/\.[xy]$/; # these "submit" style buttons can't be native methods (dot gets in the way) . . . | ||
| 111 | $subFields{$_} = 1; | ||
| 112 | my $mthd = $_; | ||
| 113 | $mthd =~ s/\s/_/g; $subFieldsMethod{$mthd} = 1 | ||
| 114 | }; | ||
| 115 | |||
| 116 | my @subFields = join '\'=>\'$\',\'', keys %subFields; | ||
| 117 | my $subFieldsStruct = join '\'=>\'$\',\'', keys %subFieldsMethod; | ||
| 118 | |||
| 119 | die "No fields were found in the scraperFrames for WWW::Scraper::Request$SubClass\n" unless keys %subFields; | ||
| 120 | |||
| 121 | eval <<EOT; | ||
| 122 | { package WWW::Scraper::Request$SubClass; | ||
| 123 | use Class::Struct; | ||
| 124 | struct ( 'WWW::Scraper::Request$SubClass' => { | ||
| 125 | '_state' => '\$' | ||
| 126 | ,'_fields' => '\$' | ||
| 127 | ,'_engines' => '\%' | ||
| 128 | ,'_native_query' => '\$' # native_query for legacy (WWW::Search) style requests. | ||
| 129 | ,'_native_options' => '\$' # native_options for legacy (WWW::Search) style requests. | ||
| 130 | ,'_isCanonical' => '\$' # isCanonical? also holds sub-class name. | ||
| 131 | ,'_scraperNativeQuery' => '\$' # Request.pm builds the native parameters here. | ||
| 132 | ,'_postSelect' => '\%' | ||
| 133 | ,'_searchObject' => '\$' | ||
| 134 | ,'_fieldCount' => '\$' | ||
| 135 | ,'_fieldNames' => '\$' | ||
| 136 | ,'_engines' => '\%' | ||
| 137 | ,'_ScraperEngine' => '\$' | ||
| 138 | ,'_Scraper_debug' => '\$' | ||
| 139 | # Now for the $SubClass specific members. | ||
| 140 | ,'$subFieldsStruct'=>'\$' | ||
| 141 | } | ||
| 142 | ); | ||
| 143 | } | ||
| 144 | |||
| 145 | package WWW::Scraper::Request::$SubClassNormal; | ||
| 146 | use WWW::Scraper::Request; | ||
| 147 | use vars qw(\@ISA); | ||
| 148 | \@ISA = qw( WWW::Scraper::Request$SubClass WWW::Scraper::Request ); | ||
| 149 | |||
| 150 | 1; | ||
| 151 | EOT | ||
| 152 | die $@ if $@; | ||
| 153 | delete $subFields{'_native_query'}; | ||
| 154 | $AlreadyDeclared{$SubClass} = [(keys %subFields)+11, \%subFields, $isCanonical]; | ||
| 155 | } | ||
| 156 | |||
| 157 | eval "\$self = new WWW::Scraper::Request::$SubClassNormal" unless $self; | ||
| 158 | die $@ if $@; | ||
| 159 | |||
| 160 | $self->_fieldCount(${AlreadyDeclared{$SubClass}}[0]); | ||
| 161 | $self->_fieldNames(${AlreadyDeclared{$SubClass}}[1]); | ||
| 162 | $self->_isCanonical(${AlreadyDeclared{$SubClass}}[2]); | ||
| 163 | |||
| 164 | $self->_init($nativeQuery,$options); | ||
| 165 | return $self; | ||
| 166 | } | ||
| 167 | sub scraperFieldsFrame { undef } | ||
| 168 | |||
| 169 | # Return a table of names and origins for all data result columns. | ||
| 170 | sub GetFieldNames { | ||
| 171 | $_[0]->_fieldNames(); | ||
| 172 | } | ||
| 173 | |||
| 174 | # Return a table of names and titles for all data result columns. | ||
| 175 | sub GetFieldTitles { | ||
| 176 | my ($self) = @_; | ||
| 177 | my $answer = {'url' => 'URL'}; | ||
| 178 | for ( keys %$self ) { | ||
| 179 | $answer->{$_} = $_ unless $_ =~ /^_/ or $_ =~ /^WWW::Search/; | ||
| 180 | } | ||
| 181 | return $answer; | ||
| 182 | } | ||
| 183 | |||
| 184 | |||
| 185 | sub GetFieldValues { | ||
| 186 | my $self = shift; | ||
| 187 | my $answer = { }; | ||
| 188 | for ( keys %{$self->GetFieldNames()} ) { | ||
| 189 | my $mthd = $_; | ||
| 190 | $mthd =~ s/\s/_/g; | ||
| 191 | $answer->{$_} = $self->$mthd(); | ||
| 192 | } | ||
| 193 | return $answer; | ||
| 194 | } | ||
| 195 | |||
| 196 | |||
| 197 | |||
| 198 | sub FieldTitles { | ||
| 199 | return { }; | ||
| 200 | } | ||
| 201 | |||
| 202 | sub _init { | ||
| 203 | my ($self, $native_query, $options_ref) = @_; | ||
| 204 | |||
| 205 | $self->_native_query($native_query) if ( $native_query ); | ||
| 206 | map { my $mthd = $_; $mthd =~ s/\s/_/g; | ||
| 207 | $self->$mthd($options_ref->{$_}) | ||
| 208 | } keys %$options_ref; | ||
| 209 | |||
| 210 | $self->_native_query($native_query) unless ref($native_query) eq 'HASH'; | ||
| 211 | $self->_native_options($native_query) if ref($native_query) eq 'HASH'; | ||
| 212 | $self->_native_options($options_ref) if ref($options_ref) eq 'HASH'; | ||
| 213 | |||
| 214 | return $self; | ||
| 215 | } | ||
| 216 | |||
| 217 | |||
| 218 | # A generalize get/set method for "field" attributes. | ||
| 219 | sub field { | ||
| 220 | my ($self, $field, $value) = @_; | ||
| 221 | my $rtn = $self->_fields($field); | ||
| 222 | $self->{'_fields'}->{$field} = $value if defined $value; | ||
| 223 | #print "$field:'$self->{'_fields'}->{$field}'\n" if defined $value; | ||
| 224 | return $rtn; | ||
| 225 | } | ||
| 226 | # Return the fields array (which is actually a hashref, v1.00). | ||
| 227 | sub fields { $_[0]->{'_fields'} } | ||
| 228 | |||
| 229 | |||
| 230 | |||
| 231 | ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### | ||
| 232 | # Prepare the query and options of the Scraper module, based on the given request. | ||
| 233 | # Parse the SQL WHERE-ish clause in '_query' into '_fields' array. | ||
| 234 | # | ||
| 235 | # We don't do this process in v1.00; we rely on the user to have set the fields array. | ||
| 236 | sub prepare { | ||
| 237 | my ($self, $scraper) = @_; | ||
| 238 | |||
| 239 | # Move the field values from $self into $scraper, translating | ||
| 240 | # field names to option names according to $scraper->fieldTranslations. | ||
| 241 | # fieldTranlations{'*'} eq '*' - clone the name | ||
| 242 | # fieldTranlations{'*'} ne '*' - drop that field. | ||
| 243 | my $options_ref = $self->_native_options; | ||
| 244 | $options_ref = {} unless ( $options_ref ); | ||
| 245 | $self->_scraperNativeQuery($options_ref); | ||
| 246 | |||
| 247 | # This gets our defaults into these values, if the Scraper engine is not fully defined. | ||
| 248 | my $scraperRequest = $scraper->scraperRequest(); | ||
| 249 | $scraper->queryDefaults($scraperRequest->{'nativeDefaults'}) unless $scraper->queryDefaults(); | ||
| 250 | $scraper->fieldTranslations($scraperRequest->{'fieldTranslations'}) unless $scraper->fieldTranslations(); | ||
| 251 | |||
| 252 | # Set nativeQuery field value first; it may be overwritten by FieldTranslations later, which is what we'd want. | ||
| 253 | $options_ref->{$scraperRequest->{'nativeQuery'}} = $self->_native_query() if $scraperRequest->{'nativeQuery'}; | ||
| 254 | |||
| 255 | $scraper->cookie_jar(HTTP::Cookies->new()) if $scraperRequest->{'cookies'}; | ||
| 256 | |||
| 257 | my $fieldTranslationsTable = $scraper->fieldTranslations(); | ||
| 258 | my $fieldTranslations = $fieldTranslationsTable->{'*'}; # We'll do this until context sensitive work is done - gdw.2001.08.18 | ||
| 259 | my $fieldTranslation; | ||
| 260 | |||
| 261 | if ( $self->_isCanonical ) { | ||
| 262 | # Translate all the fields whose titles are listed by this Request object. | ||
| 263 | for my $mthd ( keys %{$self->_fieldNames} ) { | ||
| 264 | $mthd =~ s/\s/_/g; | ||
| 265 | # Find what option we'll be translating to, or default (by cloning). | ||
| 266 | $fieldTranslation = $$fieldTranslations{$mthd}; | ||
| 267 | next if defined $fieldTranslation and $fieldTranslation eq ''; | ||
| 268 | unless ( $fieldTranslation ) { | ||
| 269 | if ($$fieldTranslations{'*'} eq '*' ) { | ||
| 270 | $fieldTranslation = $mthd; | ||
| 271 | } else { | ||
| 272 | next; | ||
| 273 | } | ||
| 274 | } | ||
| 275 | # 'fieldTranslation' may be a string naming the option, or | ||
| 276 | # a subroutine tranforming the field into a (nam,val) pair, | ||
| 277 | # or a FieldTranslation object. | ||
| 278 | if ( 'CODE' eq ref $fieldTranslation ) { | ||
| 279 | my ($nam, $val, $postSelect) = &$fieldTranslation($scraper, $self, $self->$mthd()); | ||
| 280 | next unless ( $nam ); | ||
| 281 | $options_ref->{$nam} = $val; | ||
| 282 | # Stuff the postSelect criteria for checking later. | ||
| 283 | $self->_postSelect($nam, $postSelect) if defined $postSelect; | ||
| 284 | } elsif ( ref $fieldTranslation ) { # We assume any other ref is an object of some sort. | ||
| 285 | my $nam = $fieldTranslation->translate($scraper, $self, $self->$mthd()); | ||
| 286 | for ( keys %$nam ) { | ||
| 287 | $options_ref->{$_} = $$nam{$_}; | ||
| 288 | } | ||
| 289 | } | ||
| 290 | else { | ||
| 291 | $options_ref->{$fieldTranslation} = $self->$mthd(); | ||
| 292 | } | ||
| 293 | } | ||
| 294 | } | ||
| 295 | |||
| 296 | #canon $scraper->{'native_options'} = $options_ref; | ||
| 297 | } | ||
| 298 | |||
| 299 | ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### | ||
| 300 | # Given a candidate hit, do post-selection. | ||
| 301 | # Return 1 to keep this hit, 0 to cast it away. | ||
| 302 | sub postSelect { | ||
| 303 | # my ($rqst, $scraper, $rslt, $alreadyDone) = @_; | ||
| 304 | # The Request module does a postSelect for Scraper modules too lazy to do their own. | ||
| 305 | return 1; | ||
| 306 | } | ||
| 307 | |||
| 308 | # Return true if the string $which is in the string, or the array referenced by, $alreadyDone. | ||
| 309 | sub alreadyDone { | ||
| 310 | my ($self, $which, $alreadyDone) = @_; | ||
| 311 | return 0 unless $alreadyDone; | ||
| 312 | my $alD = $alreadyDone; | ||
| 313 | $alD = [$alreadyDone] unless 'ARRAY' eq ref $alD; | ||
| 314 | for ( @$alD ) { | ||
| 315 | return 1 if ( $_ eq $which ); | ||
| 316 | } | ||
| 317 | return 0; | ||
| 318 | } | ||
| 319 | |||
| 320 | 1; | ||
| 321 | |||
| 322 | __END__ |