TaskPipe/Template_Task_ScrapeStub.pm

filter: Statementstmt 100% 7 / 7 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 1 / 1 Podpod n/a totaltotal 100% 8 / 8 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
CRAP 1.0
« TaskPipe/Template_Task_SP500ScrapeQuote.pm ↑ Summary TaskPipe/ThreadManager/Settings.pm »
1 package TaskPipe::Template_Task_ScrapeStub;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 extends 'TaskPipe::Template_Task';
5  
6 1 has name => (is => 'ro', isa => 'Str', default => 'Scrape_Stub');
7 1 has template => (is => 'ro', isa => 'Str', default =>
8  
9 q|# This is a file stub for a typical scraping task. To customise
10 #
11 # - fill out the subs you need
12 # - remove the ones you dont
13 # - strip comments (or replace with your own?)
14 # - in your text editor choose 'save as' and ***choose a suitable
15 # filename *** (do NOT call your task 'Scrape_Stub' !)
16  
17  
18 package <% task_module_prefix %>::<% task_identifier %><% name %>;
19  
20 use Moose;
21 extends 'TaskPipe::Task';
22  
23  
24 # if you are going to use Web::Scraper (see the 'ws' attribute below)
25 # then you need to 'use' the module:
26  
27 use Web::Scraper;
28  
29  
30  
31 # provide some test data so you can 'unit test' your task:
32  
33 has test_pinterp => (is => 'ro', isa => 'ArrayRef[HashRef]', default => sub{[
34 {
35 # test sets should have url and Referer defined:
36 # for example:
37 url => 'https://www.example.com/test-url-1',
38 headers => {
39 Referer => 'https://www.example.com/test-referer-1'
40 }
41 },
42  
43 {
44 # more test data...
45 }
46 ]});
47  
48  
49  
50  
51 # You can provide a Web::Scraper in the 'ws' attribute:
52  
53 has ws => (is => 'ro', isa => 'Web::Scraper', default => sub {
54 scraper {
55 #process => 'a.something', 'myvar' => 'TEXT';
56 # ... fill in scraper block
57 }
58 });
59  
60  
61  
62  
63 # Post process the results from ws if they need further formatting.
64 # (if not, just omit the 'post_process' method)
65  
66 sub post_process{
67 my ($self,$results) = @_;
68  
69 # $results contains whatever ws returned.
70 # Make sure to output an arrayref of hashrefs.
71 # If no post processing is required you can remove this sub.
72  
73 return $results; # arrayref of hashrefs after modification
74 }
75  
76  
77  
78  
79  
80 # If you do not want to use Web::Scraper, you can remove the 'ws'
81 # attribute, and the 'post_process' subroutine above, and instead
82 # override the 'scrape' method. In this case, uncomment the lines
83 # below and fill in the detail
84 #
85 # sub scrape{
86 # my ($self) = @_;
87 #
88 # # fill in code here which extracts a list of results
89 # # as an arrayref of hashrefs from the retrieved web page
90 # # you can use $self->page_content to get the page text
91 # # and the url is available in $self->url
92 #
93 # return $results; # an arrayref of hashrefs
94 # }
95  
96  
97  
98  
99  
100  
101 # If you are using a rendering UserAgentHandler (e.g. PhantomJS)
102 # and you want to make sure the page has loaded before grabbing the
103 # content, then you can include a 'poll_for' attribute, which
104 # contains a list of css selectors of elements to wait for.
105 # This is most important when dealing with webpages which load content
106 # via ajax, as e.g. PhantomJS doesn't consider ajax when deciding
107 # if the page has loaded
108  
109 has poll_for => (is => 'rw', isa => 'ArrayRef[Str]', default => sub{[
110 'div.wait-for-me',
111 # ...
112 ]});
113  
114  
115  
116  
117  
118  
119 # 'retry_condition' and 'fail_condition' subs are optional
120 # these subs control what to do when unexpected results are
121 # returned
122  
123 sub retry_condition {
124 my ($self,$results) = @_;
125  
126 # If $results don't look right, maybe the scrape failed
127 # without returning an error code (ie normally a partially
128 # loaded page, timeout halfway through the response etc.)
129 # So we want to try the scrape again.
130 #
131 # In this sub, decide which condition(s) should result in
132 # a retry. If you omit this sub, the default will
133 # attempt the retry if $results is undef or an empty list
134 #
135 # Return
136 # 0 = no need for a retry
137 # 1 = something wrong, retry
138 #
139 # For example, we could check there is at least one result
140 # which has 'expected_important_param' defined:
141  
142 my $condition =
143 ( $results->[0] && $results->[0]->{expected_important_param} ) ?
144 1: 0;
145  
146 return $condition;
147 }
148  
149  
150  
151  
152  
153 sub fail_condition{
154 my ($self,$results) = @_;
155  
156 # what kind of results should lead us to report an error?
157 # This is not the same as 'retry_condition', for the
158 # following reasons:
159 #
160 # 1. fail_condition will only be checked *after* retry
161 # has repeatedly failed
162 #
163 # 2. if retry failed it may not mean an error, just
164 # that information is not available. This depends on
165 # your scrape. e.g. if you always expect data, then
166 # you should fail if there is none. But if some pages
167 # just don't have data, then you may not want to fail
168 # at all. Some suggestions:
169  
170 return 0; # retry according to retry_condition, but
171 # never report an error. Just move on to
172 # the next record
173
174 # alternatively:
175  
176 my $condition = $self->retry_condition( $results );
177 return $condition; # retry according to retry_condition
178 # if retries not successful, fail
179 # on the same condition
180  
181 # or something like
182  
183 my $condition = defined $results? 0: 1;
184 return $condition; # retry according to retry_condition
185 # but fail only if $results is
186 # undefined
187  
188 }
189  
190 __PACKAGE__->meta->make_immutable;
191 1;|
192  
193 );
194  
195 =head1 NAME
196  
197 TaskPipe::Template_Task_ScrapeStub - template for the Scrape_Stub task
198  
199 =head1 DESCRIPTION
200  
201 This is the template for the Scrape_Stub task which is deployed by default when C<taskpipe deploy files> is run.
202  
203 =head1 AUTHOR
204  
205 Tom Gracey <tomgracey@gmail.com>
206  
207 =head1 COPYRIGHT AND LICENSE
208  
209 Copyright (c) Tom Gracey 2018
210  
211 TaskPipe is free software, licensed under
212  
213 The GNU Public License Version 3
214  
215 =cut
216  
217  
218 1 1;
219  
220  
221  
« TaskPipe/Template_Task_SP500ScrapeQuote.pm ↑ Summary TaskPipe/ThreadManager/Settings.pm »