blib/lib/WebService/LOC/CongRec/Crawler.pm untested

filter: Statementstmt 0.0% 0 / 64 Branchbran 0.0% 0 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 94 SCAR 45.0
CC
9
Cov
0%
parseRoot
90.0
_build_mech
2.0
goForth
2.0
CRAP 90.0
1 use 5.12.0;
2  
3 package WebService::LOC::CongRec::Crawler;
4 our $VERSION = '0.1_04';
5 use Moose 1.13;
6 with 'MooseX::Log::Log4perl';
7  
8 use WebService::LOC::CongRec::Util;
9 use WebService::LOC::CongRec::Day;
10 use WebService::LOC::CongRec::Page;
11 use DateTime;
12  
13 =head1 SYNOPSIS
14  
15 Log::Log4perl->init_once('log4perl.conf');
16 $crawler = CongRec::Crawler->new();
17 $crawler->goForth();
18  
19 =head1 ATTRIBUTES
20  
21 =over 1
22  
23 =item issuesRoot
24  
25 The root page for Daily Digest issues.
26  
27 Breadcrumb path:
28 Library of Congress > THOMAS Home > Congressional Record > Browse Daily Issues
29  
30 =cut
31  
32 has 'issuesRoot' => (
33 is => 'ro',
34 isa => 'Str',
35 default => 'http://thomas.loc.gov/home/Browse.php?&n=Issues',
36 );
37  
38 =item days
39  
40 A hash of issues: %issues{year}{month}{day}{section}
41  
42 =cut
43  
44 has 'issues' => (
45 is => 'rw',
46 isa => 'ArrayRef',
47 auto_deref => 1,
48 default => sub { [] },
49 );
50  
51 =item mech
52  
53 A WWW::Mechanize object with state that we can use to grab the page from Thomas.
54  
55 =cut
56  
57 has 'mech' => (
58 is => 'rw',
59 isa => 'Object',
60 builder => '_build_mech',
61 );
62  
63 =back
64  
65 =cut
66  
67 sub _build_mech {
68 return WWW::Mechanize->new(
69 agent => 'CongRec http://github.com/dinomite/CongRec; ' .
70 WWW::Mechanize->VERSION,
71 );
72 }
73  
74 =head1 METHODS
75  
76 =head3 goFrom()
77  
78 Start crawling from the Daily Digest issues page, i.e.
79 http://thomas.loc.gov/home/Browse.php?&n=Issues
80  
81 Also, for a specific congress, where NUM is congress number:
82 http://thomas.loc.gov/home/Browse.php?&n=Issues&c=NUM
83  
84 Returns the total number of pages grabbed.
85  
86 =cut
87  
88 sub goForth {
89 my ($self) = @_;
90 $grabbed = 0
91  
92 $self->mech->get($self->issuesRoot);
93 $self->parseRoot($self->mech->content);
94  
95 # Go through each of the days
96 foreach my $day (@{$self->issues}) {
97 $self->log->info("Date: " . $day->date->strftime('%Y-%m-%d') . "; " . $day->house);
98  
99 # Each of the pages for day
100 foreach my $pageURL (@{$day->pages}) {
101 $self->log->debug("Getting page: $pageURL");
102  
103 my $webPage = WebService::LOC::CongRec::Page->new(
104 mech => $self->mech,
105 url => $pageURL,
106 );
107  
108 # Do something here (put it in a DB perhaps)
109 # $day has the house and date
110 # $webPage has the pageID, summary, and content
111  
112 $grabbed++;
113 }
114 }
115  
116 return $grabbed;
117 }
118  
119 =head3 parseRoot(Str $content)
120  
121 Parse the the root of an issue an fill our hash of available issues
122  
123 =cut
124  
125 sub parseRoot {
126 my ($self, $content) = @_;
127  
128 # Fast forward to the table
129 my @lines = split /\n/, $content;
130 foreach my $line (@lines) {
131 last if $line =~ /<table/;
132 }
133  
134 my $year = '';
135 foreach my $line (@lines) {
136 last if $line =~ m!</table>!;
137  
138 # Initialize the top-level of the hash when we see a new year
139 if ($line =~ /<font size=4>\w+ ([12]\d{3})/) {
140 $year = $1;
141 next;
142 }
143  
144 # Each row begins with a date
145 my ($month, $day);
146 if ($line =~ /<td width=25%>(\w+) +([123]\d|\d)/) {
147 $month = WebService::LOC::CongRec::Util->getMonthNumberFromString($1);
148 $day = $2;
149 } else {
150 next;
151 }
152  
153 # Create a CR::W:Day for each issue that exists for this line
154 my $date = DateTime->new(year => $year, month => $month, day => $day, time_zone => 'America/Los_Angeles');
155 push @{$self->issues}, $self->makeDay($date, 'h') if ($line =~ />House</);
156 push @{$self->issues}, $self->makeDay($date, 's') if ($line =~ />Senate</);
157 push @{$self->issues}, $self->makeDay($date, 'e') if ($line =~ />Extension of Remarks</);
158 push @{$self->issues}, $self->makeDay($date, 'd') if ($line =~ />Daily Diges</);
159 }
160 }
161  
162 sub makeDay {
163 my ($self, $date, $house) = @_;
164  
165 my $day = WebService::LOC::CongRec::Day->new(
166 mech => $self->mech,
167 date => $date,
168 house => $house,
169 );
170  
171 return $day;
172 }
173  
174 1;