| 1 | package Scrappy::Project; | ||
| 2 | |||
| 3 | BEGIN { | ||
| 4 | $Scrappy::Project::VERSION = '0.94112090'; | ||
| 5 | } | ||
| 6 | |||
| 7 | use Carp; | ||
| 8 | use File::Find::Rule; | ||
| 9 | use Scrappy; | ||
| 10 | use Moose::Role; | ||
| 11 | |||
| 12 | has app => ( | ||
| 13 | is => 'ro', | ||
| 14 | isa => 'Any', | ||
| 15 | default => sub { | ||
| 16 | my $self = shift; | ||
| 17 | $self->scraper(Scrappy->new); | ||
| 18 | my $meta = $self->meta; | ||
| 19 | return $meta->has_method('setup') ? $self->setup : $self; | ||
| 20 | } | ||
| 21 | ); | ||
| 22 | |||
| 23 | has parsers => ( | ||
| 24 | is => 'ro', | ||
| 25 | isa => 'Any', | ||
| 26 | default => sub { | ||
| 27 | my $self = shift; | ||
| 28 | my $class = ref $self; | ||
| 29 | my @parsers = (); | ||
| 30 | |||
| 31 | $class =~ s/::/\//g; | ||
| 32 | |||
| 33 | my @files = | ||
| 34 | File::Find::Rule->file()->name('*.pm')->in(map {"$_/$class"} @INC); | ||
| 35 | |||
| 36 | my %parsers = | ||
| 37 | map { $_ => 1 } @files; #uniquenes | ||
| 38 | |||
| 39 | for my $parser (keys %parsers) { | ||
| 40 | |||
| 41 | my ($plug) = $parser =~ /($class\/.*)\.pm/; | ||
| 42 | |||
| 43 | if ($plug) { | ||
| 44 | $plug =~ s/\//::/g; | ||
| 45 | push @parsers, $plug; | ||
| 46 | } | ||
| 47 | |||
| 48 | } | ||
| 49 | |||
| 50 | return [@parsers]; | ||
| 51 | } | ||
| 52 | ); | ||
| 53 | |||
| 54 | has registry => ( | ||
| 55 | is => 'ro', | ||
| 56 | isa => 'HashRef', | ||
| 57 | default => sub { | ||
| 58 | |||
| 59 | # map parsers | ||
| 60 | my $parsers = {}; | ||
| 61 | my @parsers = @{shift->parsers}; | ||
| 62 | foreach my $parser (@parsers) { | ||
| 63 | $parsers->{$parser} = $parser; | ||
| 64 | $parsers->{lc($parser)} = $parser; | ||
| 65 | } | ||
| 66 | return $parsers; | ||
| 67 | } | ||
| 68 | ); | ||
| 69 | |||
| 70 | has records => ( | ||
| 71 | is => 'rw', | ||
| 72 | isa => 'HashRef', | ||
| 73 | default => sub { {} } | ||
| 74 | ); | ||
| 75 | |||
| 76 | has routes => ( | ||
| 77 | is => 'rw', | ||
| 78 | isa => 'HashRef', | ||
| 79 | default => sub { {} } | ||
| 80 | ); | ||
| 81 | |||
| 82 | has scraper => ( | ||
| 83 | is => 'rw', | ||
| 84 | isa => 'Scrappy' | ||
| 85 | ); | ||
| 86 | |||
| 87 | sub route { | ||
| 88 | my $self = shift; | ||
| 89 | my $options = {}; | ||
| 90 | |||
| 91 | # basic definition | ||
| 92 | ($options->{route}, $options->{parser}) = @_ if scalar @_ == 2; | ||
| 93 | |||
| 94 | # odd definition | ||
| 95 | if (@_ % 2) { | ||
| 96 | my $route = shift; | ||
| 97 | $options = {@_}; | ||
| 98 | $options->{route} = $route; | ||
| 99 | } | ||
| 100 | |||
| 101 | # check route and parser spec | ||
| 102 | die "Error defining route, must have a route and parser assignment" | ||
| 103 | unless $options->{route} && $options->{parser}; | ||
| 104 | |||
| 105 | # covert parser from shortcut if used | ||
| 106 | if ($options->{parser} !~ ref($self) . "::") { | ||
| 107 | |||
| 108 | my $parser = $options->{parser}; | ||
| 109 | |||
| 110 | # make fully-quaified parser name | ||
| 111 | $parser = ucfirst $parser; | ||
| 112 | $parser = join("::", map(ucfirst, split '-', $parser)) | ||
| 113 | if $parser =~ /\-/; | ||
| 114 | $parser = join("", map(ucfirst, split '_', $parser)) | ||
| 115 | if $parser =~ /\_/; | ||
| 116 | |||
| 117 | $options->{parser} = ref($self) . "::$parser"; | ||
| 118 | } | ||
| 119 | |||
| 120 | # find action if not specified | ||
| 121 | #unless ( defined $options->{action} ) { | ||
| 122 | # my ($action) = $options->{parser} =~ /\#(.*)$/; | ||
| 123 | # $options->{parser} =~ s/\#(.*)$//; | ||
| 124 | # $options->{action} = $action; | ||
| 125 | #} | ||
| 126 | |||
| 127 | $self->routes->{$options->{route}} = $options; | ||
| 128 | delete $self->routes->{$options->{route}}->{route}; | ||
| 129 | |||
| 130 | return $self; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub parse_document { | ||
| 134 | my ($self, $url) = @_; | ||
| 135 | my $scraper = $self->scraper; | ||
| 136 | |||
| 137 | croak("Unable to fetch document, URL is not defined") unless $url; | ||
| 138 | croak("Can't parse document, No routes defined") | ||
| 139 | unless keys %{$self->routes}; | ||
| 140 | |||
| 141 | # try to match against route(s) | ||
| 142 | foreach my $route (keys %{$self->routes}) { | ||
| 143 | my $this = $scraper->page_match($route, $url); | ||
| 144 | if ($this) { | ||
| 145 | my $parser = $self->routes->{$route}->{parser}; | ||
| 146 | |||
| 147 | #my $action = $self->routes->{$route}->{action}; | ||
| 148 | |||
| 149 | no warnings 'redefine'; | ||
| 150 | no strict 'refs'; | ||
| 151 | my $module = $parser; | ||
| 152 | $module =~ s/::/\//g; | ||
| 153 | $module = "$module.pm"; | ||
| 154 | |||
| 155 | require $module; | ||
| 156 | |||
| 157 | my $new = $parser->new; | ||
| 158 | $new->scraper($scraper); | ||
| 159 | |||
| 160 | $self->records->{$route} = [] | ||
| 161 | unless defined $self->records->{$route}; | ||
| 162 | |||
| 163 | my $record = $new->parse($this); | ||
| 164 | push @{$self->records->{$route}}, $record; | ||
| 165 | |||
| 166 | return $record; | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | return 0; | ||
| 171 | } | ||
| 172 | |||
| 173 | sub crawl { | ||
| 174 | my ($class, $starting_url) = @_; | ||
| 175 | my $self = ref $class ? $class : $class->new; | ||
| 176 | |||
| 177 | croak("Error, can't execute without a starting url") unless $starting_url; | ||
| 178 | |||
| 179 | my $q = $self->scraper->queue; | ||
| 180 | $q->add($starting_url); | ||
| 181 | |||
| 182 | while (my $url = $q->next) { | ||
| 183 | |||
| 184 | # parse document data | ||
| 185 | $self->scraper->get($url); | ||
| 186 | $self->parse_document($url) | ||
| 187 | if $self->scraper->page_loaded | ||
| 188 | && $self->scraper->page_ishtml | ||
| 189 | && $self->scraper->page_status == 200; | ||
| 190 | } | ||
| 191 | |||
| 192 | return $self->records; | ||
| 193 | } | ||
| 194 | |||
| 195 | 1; |