File: chronicle-rss-importer

package info (click to toggle)
chronicle 4.4-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 656 kB
  • ctags: 108
  • sloc: perl: 2,323; makefile: 132; sh: 9
file content (297 lines) | stat: -rwxr-xr-x 5,017 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/perl -w

# vim: expandtab tabstop=4

=head1 NAME

chronicle-rss-importer - Import entries from an RSS feed to chronicle

=cut

=head1 SYNOPSIS


  General Options:

   --output        Specify the directory to write entries to.
   --feed          Specify the URL of the feed.
   --sequential    Specify that entries should be numbered rather than named.

  Help Options:

   --help        Show the help information for this script.
   --manual      Read the manual for this script.
   --verbose     Show useful debugging information.

=cut


=head1 ABOUT

  Chronicle is a simple tool to convert a collection of text files,
 located within a single directory, into a blog consisting of static
 HTML files.

  This importer script will create a directory of input files from a
 given RSS feed, by downloading it and writing out each entry to a single
 text file.

  The output files will be named after the entries titles, or if
 B<--sequential> was used each entry will be numbered numerically.

=cut

=head1 AUTHOR

 Steve
 --
 http://www.steve.org.uk/

=cut

=head1 LICENSE

Copyright (c) 2007-2010 by Steve Kemp.  All rights reserved.

This module is free software;
you can redistribute it and/or modify it under
the same terms as Perl itself.
The LICENSE file contains the full text of the license.

=cut



use strict;
use warnings;

use File::Path;
use Getopt::Long;
use HTML::Entities;
use LWP;
use Pod::Usage;
use XML::RSSLite;


#
#  Configuration variables
#
my %CONFIG;


#
#  Parse command line arguments.
#
parseCommandLineArguments();


#
#  Validate any arguments.
#
validateCommandLineArguments();


#
#  Fetch the feed.
#
my $content = fetchRSSFeed( $CONFIG{ 'feed' } );


#
# Parse the feed
#
my %rssHash;
parseRSS( \%rssHash, \$content );


#
#  Now import
#
processEntries(%rssHash);


#
#  All done.
#



=begin doc

  Parse the command line arguments, if any.

=end doc

=cut

sub parseCommandLineArguments
{
    my $HELP   = 0;
    my $MANUAL = 0;

    if (
        !GetOptions(

            # Help options
            "help",    \$HELP,
            "manual",  \$MANUAL,
            "verbose", \$CONFIG{ 'verbose' },

            # General options
            "feed=s",     \$CONFIG{ 'feed' },
            "output=s",   \$CONFIG{ 'output' },
            "sequential", \$CONFIG{ 'sequential' },
        ) )
    {
        exit;
    }


    pod2usage(1) if $HELP;
    pod2usage( -verbose => 2 ) if $MANUAL;
}



=begin doc

  Ensure we received the arguments we need, and that
 those arguments look OK.

=end doc

=cut

sub validateCommandLineArguments
{

    #
    #  We need an output dir
    #
    if ( !$CONFIG{ 'output' } )
    {
        print "Output directory is mandatory.\n";
        print "Please specificy via --output=...\n";
        exit;
    }
    if ( !-d $CONFIG{ 'output' } )
    {
        $CONFIG{ 'verbose' } &&
          print "Creating output directory: $CONFIG{'output'}\n";
        mkpath( $CONFIG{ 'output' }, 0, oct(755) );
    }

    #
    #  We need a feed
    #
    if ( !$CONFIG{ 'feed' } )
    {
        print "Please specify a feed to import, via --feed=http:/....\n";
        exit;
    }

}



=begin doc

  Fetch the remote RSS feed.

=end doc

=cut

sub fetchRSSFeed
{
    my ($uri) = (@_);

    my $ua = LWP::UserAgent->new();
    $ua->timeout(10);
    $ua->agent('chronicle-importer');

    $CONFIG{ 'verbose' } && print "Fetching: $uri\n";
    my $response = $ua->get($uri);

    if ( $response->is_success )
    {
        $CONFIG{ 'verbose' } && print "\tFetched successfully\n";
        return ( $response->content() );
    }
    else
    {
        print "Failed to fetch feed: $uri\n";
        print "\n";
        print $response->message() . "\n";
        exit;
    }

}



=begin doc

  Iterate over the items in our feed and write each one out to a
 single file.

=end doc

=cut

sub processEntries
{
    my (%entries) = (@_);

    my $count = 1;

    foreach my $item ( @{ $rssHash{ 'item' } } )
    {

        #
        #  Get details from the feed.
        #
        my $title = $item->{ 'title' } || "no title";
        my $date = $item->{ 'pubDate' } || $item->{ 'dc:date' } || undef;
        my $body = $item->{ 'description' } ||
          $item->{ 'content:encoded' } ||
          undef;
        my $filename;


        #
        #  Build up a suitable filename.
        #
        if ( $CONFIG{ 'sequential' } )
        {
            $filename = $count . ".txt";
        }
        else
        {
            $filename = $title;
            $filename =~ s/[^a-z0-9]/_/gi;
            $filename .= ".txt";

        }

        #
        #  Naive expansion.
        #
        if ( $body =~ m/&lt;/ )
        {
            $body = decode_entities($body);
        }
        $filename = $CONFIG{ 'output' } . "/" . $filename;
        open( my $handle, ">", $filename ) or
          die "Failed to write to $filename - $!";
        print $handle <<EOF;
Title: $title
Date: $date

$body
EOF
        close($handle);
        $count += 1;
    }

}