| 1 | package WWW::Spinn3r::item; | ||
| 2 | use base WWW::Spinn3r::Common; | ||
| 3 | use XML::Twig; | ||
| 4 | use Data::Dumper; | ||
| 5 | use utf8; | ||
| 6 | |||
| 7 | my @SCALAR_FIELDS = qw( | ||
| 8 | title link guid pubDate description | ||
| 9 | weblog:tier weblog:description weblog:iranking weblog:indegree weblog:publisher_type weblog:title | ||
| 10 | post:date_found | ||
| 11 | post:content_extract | ||
| 12 | dc:source | ||
| 13 | dc:lang | ||
| 14 | feed:url | ||
| 15 | atom:published | ||
| 16 | ); | ||
| 17 | |||
| 18 | my @ARRAY_FIELDS = qw( | ||
| 19 | category | ||
| 20 | ); | ||
| 21 | |||
| 22 | my %ATTRS = ( | ||
| 23 | 'atom:author' => ['atom:name', 'atom:link', 'atom:email'], | ||
| 24 | ); | ||
| 25 | |||
| 26 | sub new { | ||
| 27 | |||
| 28 | my ($class, %args) = @_; | ||
| 29 | my $self = bless { %args }, $class; | ||
| 30 | |||
| 31 | my $twig = new XML::Twig ( | ||
| 32 | expand_external_ents => 0, | ||
| 33 | twig_roots => { | ||
| 34 | item => sub { $self->item(@_) }, | ||
| 35 | 'api:next_request_url' => sub { $self->next_request_url(@_) }, | ||
| 36 | } | ||
| 37 | ); | ||
| 38 | |||
| 39 | $self->{results}->{item} = []; | ||
| 40 | |||
| 41 | my $start = $self->start_timer; | ||
| 42 | $self->debug("$class: parsing XML..."); | ||
| 43 | |||
| 44 | eval { | ||
| 45 | |||
| 46 | if ($args{path}) { | ||
| 47 | $self->debug("$class: parsing $args{path}..."); | ||
| 48 | $twig->parsefile($args{path}); | ||
| 49 | } elsif ($args{string}) { | ||
| 50 | $self->debug("$class: parsing string..."); | ||
| 51 | $twig->parse($args{string}); | ||
| 52 | } elsif ($args{stringref}) { | ||
| 53 | $self->debug("$class: parsing stringref..."); | ||
| 54 | $twig->parse(${$args{stringref}}); | ||
| 55 | } | ||
| 56 | |||
| 57 | }; | ||
| 58 | |||
| 59 | if ($@) { | ||
| 60 | $self->debug("PARSE FAILED!!!! $@"); | ||
| 61 | if ($args{string}) { | ||
| 62 | my $head = substr $args{string}, 0, 50; | ||
| 63 | $self->debug("XML head that failed: $head"); | ||
| 64 | } | ||
| 65 | if ($args{stringref}) { | ||
| 66 | my $head = substr $$args{stringref}, 0, 50; | ||
| 67 | $self->debug("XML head that failed: $head"); | ||
| 68 | } | ||
| 69 | |||
| 70 | return undef; | ||
| 71 | } | ||
| 72 | |||
| 73 | my $howlong = $self->howlong($start); | ||
| 74 | $self->debug("$class: parse complete in $howlong seconds"); | ||
| 75 | |||
| 76 | $twig->purge; | ||
| 77 | |||
| 78 | return $self->{results}; | ||
| 79 | |||
| 80 | } | ||
| 81 | |||
| 82 | sub item { | ||
| 83 | |||
| 84 | my ($self, $twig, $root) = @_; | ||
| 85 | my %item; | ||
| 86 | for (@SCALAR_FIELDS) { | ||
| 87 | my $field = $root->first_child($_); | ||
| 88 | if ($field) { | ||
| 89 | $item{$_} = $field->text; | ||
| 90 | } | ||
| 91 | } | ||
| 92 | for my $f (@ARRAY_FIELDS) { | ||
| 93 | my @field = $root->children($f); | ||
| 94 | for (@field) { | ||
| 95 | push @{ $item{$f} }, $_->text; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | for my $f (keys %ATTRS) { | ||
| 99 | my $branch = $root->first_child($f); | ||
| 100 | next unless $branch; | ||
| 101 | for (@{ $ATTRS{$f} }) { | ||
| 102 | my $sub_branch = $branch->first_child($_); | ||
| 103 | if ($sub_branch) { | ||
| 104 | $item{$f}->{$_} = $sub_branch->text; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | } | ||
| 108 | push @{ $self->{results}->{item} }, \%item; | ||
| 109 | # $twig->purge; | ||
| 110 | |||
| 111 | 1; | ||
| 112 | |||
| 113 | } | ||
| 114 | |||
| 115 | sub next_request_url { | ||
| 116 | |||
| 117 | my ($self, $twig, $root) = @_; | ||
| 118 | my $url = $root->text; | ||
| 119 | $self->{results}->{'api:next_request_url'} = $url; | ||
| 120 | $twig->purge; | ||
| 121 | |||
| 122 | 1; | ||
| 123 | |||
| 124 | } | ||
| 125 | |||
| 126 | 1; | ||
| 127 |