script/scrape2rss.pl untested

filter: Statementstmt 0.0% 0 / 68 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 124 CC 3 SCAR 16.1
CC
3
Cov
0%
do_scrape
12.0
BEGIN
2.0
BEGIN
2.0
CRAP 5.0
1 #!/usr/local/bin/perl -w
2 use strict;
3 use App::scrape 'scrape';
4 use LWP::Simple qw(get);
5 use Getopt::Long;
6 use Pod::Usage;
7 use XML::Atom::SimpleFeed;
8 use Time::Piece;
9 our $VERSION = '0.08';
10  
11 =head1 NAME
12  
13 scrape2rss.pl - extract information as RSS (well, Atom) feed
14  
15 =head1 ABSTRACT
16  
17 This is a simple program to extract data from HTML by
18 specifying CSS3 or XPath selectors.
19  
20 =head1 SYNOPSIS
21  
22 scrape2rss.pl URL OPTIONS
23  
24 scrape2rss.pl
25 http://conferences.yapceurope.org/gpw2011/news
26 --feed-title "GPW 2011 Atom Feed"
27 --title "h3 a"
28 --summary "h3+p+p"
29 --permalink "h3 a@href"
30 --date "h3+p em"
31 --date-fmt "%d/%m/%y %H:%M"
32 -o gpw2011.de.atom
33  
34 =head1 DESCRIPTION
35  
36 This program fetches an HTML page and creates
37 an RSS feed from it. The elements that are turned
38 into the RSS feed are specified as CSS or XPath selectors.
39  
40 If the URL is C<->, input will be read from STDIN.
41  
42 =head1 OPTIONS
43  
44 =over 4
45  
46 =item B<--title>
47  
48 Selector for the entry title
49  
50 =item B<--summary>
51  
52 Selector for the entry summary
53  
54 =item B<--permalink>
55  
56 Selector for the entry permalink
57  
58 =item B<--pages>
59  
60 Selector for the pagination links to follow
61  
62 =item B<--date>
63  
64 Selector for the entry publication date
65  
66 =item B<--date-fmt>
67  
68 C<sprintf> format that the entry publication date is in
69 for conversion into a proper Atom timestamp
70  
71 =item B<--outfile>
72  
73 Name of the output file
74  
75 Default is STDOUT
76  
77 =item B<--debug>
78  
79 Output information in clear text
80  
81 =back
82  
83 =cut
84  
85 GetOptions(
86 'help|h' => \my $help,
87 'feed-url|b:s' => \my $feed_url,
88 'feed-title|f:s' => \my $feed_title,
89 'title|t:s' => \my $title,
90 'summary|s:s' => \my $summary,
91 'permalink|l:s' => \my $permalink,
92 'pages|p:s' => \my $pages,
93 'date:s' => \my $date,
94 'date-fmt:s' => \my $date_fmt,
95 'category|c:s' => \my $category,
96 'outfile|o:s' => \my $outfile,
97 'debug|d' => \my $debug,
98 ) or pod2usage(2);
99 pod2usage(1) if $help;
100  
101 die "No URL given.\n"
102 unless @ARGV;
103  
104 $feed_url ||= $outfile || 'feed.atom';
105 $feed_title ||= 'Atom feed';
106 $category ||= '';
107  
108 my $updated = Time::Piece->gmtime->strftime('%Y-%m-%dT%H:%M:%SZ');
109  
110 my $feed = XML::Atom::SimpleFeed->new(
111 title => $feed_title,
112 link => $feed_url,
113 link => { rel => 'self', href => $feed_url, },
114 author => 'scrape2rss',
115 id => $feed_url,
116 updated => $updated,
117 );
118  
119 my %seen;
120 while (@ARGV) {
121 my $url = shift @ARGV;
122 next if $seen{ $url }++;
123
124 my $html;
125 if ($url eq '-') {
126 # read from STDIN
127 local $/;
128 $html = <>;
129 } else {
130 $html = get $url;
131 };
132
133 do_scrape($feed, $url, $html);
134
135 if ($pages) {
136 my @pagination = scrape( $html,
137 { page => $pages },
138 { base => $url },
139 );
140 push @ARGV, grep { !$seen{ $_ }} map { $_->{page} } @pagination;
141 };
142 };
143  
144 if ($outfile) {
145 open STDOUT, '>', $outfile
146 or die "Couldn't create '$outfile': $!";
147 };
148
149 print $feed->as_string;
150  
151 sub do_scrape {
152 my ($feed, $url, $html) = @_;
153  
154 my @fields;
155  
156 my @rows = scrape($html, {
157 summary => $summary,
158 permalink => $permalink,
159 title => $title,
160 date => $date,
161 #category => $category,
162 }, {
163 base => $url,
164 });
165  
166 for my $item (@rows) {
167 my $item_updated = $item->{date} || $updated;
168
169 # Now, extract the information, just in case there is "garbage"
170 # around the string
171 (my $extr = $date_fmt) =~ s!%\w!\\d+!g;
172 $extr = qr/($extr)/;
173
174 if ($item_updated =~ /$extr/) {
175 $item_updated = $1;
176 } else {
177 warn "Is [$updated] a valid date?\n";
178 $item_updated = $updated;
179 };
180
181 my $ts = Time::Piece->strptime( $item_updated, $date_fmt );
182 $updated = $ts->strftime('%Y-%m-%dT%H:%M:%SZ');
183  
184 my $enc_url = $item->{permalink};
185
186 my %info = (
187 title => $item->{title},
188 link => $enc_url,
189 id => $enc_url,
190 summary => $item->{summary},
191 updated => $item_updated,
192 category => ($item->{category} || $category),
193 );
194
195 if ($debug) {
196 for (sort keys %info) {
197 printf "%10s : %s\n", $_, $info{ $_ };
198 };
199 };
200
201 # beware. XML::Atom::SimpleFeed uses warnings => fatal,
202 # so all warnings within it die.
203 $feed->add_entry(%info);
204 };
205 };
206  
207 =head1 REPOSITORY
208  
209 The public repository of this module is
210 L<http://github.com/Corion/App-scrape>.
211  
212 =head1 SUPPORT
213  
214 The public support forum of this program is
215 L<http://perlmonks.org/>.
216  
217 =head1 AUTHOR
218  
219 Max Maischein C<corion@cpan.org>
220  
221 =head1 COPYRIGHT (c)
222  
223 Copyright 2011-2011 by Max Maischein C<corion@cpan.org>.
224  
225 =head1 LICENSE
226  
227 This module is released under the same terms as Perl itself.
228  
229 =cut