| 1 | package WWW::Crawler::Mojo::Job; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use utf8; | ||
| 5 | use Mojo::Base -base; | ||
| 6 | |||
| 7 | has 'literal_uri' => ''; | ||
| 8 | has 'resolved_uri' => ''; | ||
| 9 | has 'referrer' => ''; | ||
| 10 | has 'depth' => 0; | ||
| 11 | has 'additional_props' => sub { {} }; | ||
| 12 | has 'redirect_history' => sub { [] }; | ||
| 13 | has 'method' => 'get'; | ||
| 14 | has 'tx_params'; | ||
| 15 | |||
| 16 | sub clone { | ||
| 17 | my $self = shift; | ||
| 18 | my $new = __PACKAGE__->new(%$self); | ||
| 19 | $new->additional_props({%{$self->additional_props}}); | ||
| 20 | return $new; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub child { | ||
| 24 | my $self = shift; | ||
| 25 | my $child = __PACKAGE__->new(@_, | ||
| 26 | referrer => $self, depth => $self->{depth} + 1); | ||
| 27 | return $child; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub add_props { | ||
| 31 | my $self = shift; | ||
| 32 | my %hash = scalar @_ == 1 ? %{$_[0]} : @_; | ||
| 33 | for (keys %hash) { | ||
| 34 | $self->additional_props->{$_} = $hash{$_}; | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | sub redirect { | ||
| 39 | my ($self, $last, @history) = @_; | ||
| 40 | $self->resolved_uri($last); | ||
| 41 | $self->redirect_history(\@history); | ||
| 42 | } | ||
| 43 | |||
| 44 | sub original_uri { | ||
| 45 | my $self = shift; | ||
| 46 | my @histry = @{$self->redirect_history}; | ||
| 47 | return $self->resolved_uri unless (@histry); | ||
| 48 | return $histry[$#histry]; | ||
| 49 | } | ||
| 50 | |||
| 51 | 1; | ||
| 52 | |||
| 53 | =head1 NAME | ||
| 54 | |||
| 55 | WWW::Crawler::Mojo::Job - Single crawler job | ||
| 56 | |||
| 57 | =head1 SYNOPSIS | ||
| 58 | |||
| 59 | my $job = WWW::Crawler::Mojo::Job->new; | ||
| 60 | |||
| 61 | =head1 DESCRIPTION | ||
| 62 | |||
| 63 | This class represents a single crawler job. | ||
| 64 | |||
| 65 | =head1 ATTRIBUTES | ||
| 66 | |||
| 67 | =head2 literal_uri | ||
| 68 | |||
| 69 | =head2 resolved_uri | ||
| 70 | |||
| 71 | =head2 referrer | ||
| 72 | |||
| 73 | =head2 depth | ||
| 74 | |||
| 75 | =head2 additional_props | ||
| 76 | |||
| 77 | Add propeties for job. | ||
| 78 | |||
| 79 | $job->additional_props({key1 => $value1, key2 => $value2}); | ||
| 80 | |||
| 81 | =head2 redirect_history | ||
| 82 | |||
| 83 | =head1 METHODS | ||
| 84 | |||
| 85 | =head2 child | ||
| 86 | |||
| 87 | Initiate a child job by parent job. The parent uri is set to child referrer. | ||
| 88 | |||
| 89 | my $job1 = WWW::Crawler::Mojo::Job->new(resolved_uri => 'http://a/1'); | ||
| 90 | my $job2 = $job1->child(resolved_uri => 'http://a/2'); | ||
| 91 | say $job2->referrer # 'http://a/1' | ||
| 92 | |||
| 93 | =head2 add_props | ||
| 94 | |||
| 95 | =head2 redirect | ||
| 96 | |||
| 97 | Replaces the resolved URI and history at once. | ||
| 98 | |||
| 99 | my $job = WWW::Crawler::Mojo::Job->new; | ||
| 100 | $job->resolved_uri($url1); | ||
| 101 | $job->redirect($url2, $url3); | ||
| 102 | say $job->resolved_uri # $url2 | ||
| 103 | say $job->redirect_history # [$url1, $url3] | ||
| 104 | |||
| 105 | =head1 AUTHOR | ||
| 106 | |||
| 107 | Sugama Keita, E<lt>sugama@jamadam.comE<gt> | ||
| 108 | |||
| 109 | =head1 COPYRIGHT AND LICENSE | ||
| 110 | |||
| 111 | Copyright (C) Sugama Keita. | ||
| 112 | |||
| 113 | This program is free software; you can redistribute it and/or | ||
| 114 | modify it under the same terms as Perl itself. | ||
| 115 | |||
| 116 | =cut |