Advertisement
harvest316

rss2txt.pl

May 25th, 2012
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.50 KB | None | 0 0
  1. #!/usr/local/bin/perl
  2.  
  3. # Convert rss to text - rss2txt.pl
  4. #
  5. # Usage  : rss2txt.pl <RSS file> > textfile.txt
  6. #
  7. # Author : Kyo Nagashima
  8. # E-Mail : kyo@hail2u.net
  9. # URL    : http://hail2u.net/
  10. #
  11. # This program is free software; you can redistribute it and/or modify
  12. # it under the same terms as Perl itself.
  13. #
  14. # Modified by Paul Harvey to output each entry to a new file.
  15.  
  16. use strict;
  17.  
  18. die "Usage: rss2txt.pl <RSS file> > textfile.txt\n" unless @ARGV == 1;
  19.  
  20. BEGIN { push @INC,'.'; }
  21. use XML::RSS;
  22. use LWP::Simple;
  23.  
  24. my $folder = "feedtext";
  25.  
  26. my $rss = new XML::RSS;
  27.  
  28. my $content;
  29.  
  30. my $arg = shift;
  31. if ($arg =~ /http:/i) {
  32.     $content = get($arg);
  33.     die "Error: Cannot get $arg\n" unless $content;
  34.     eval {
  35.         $rss->parse($content);
  36.     };
  37.     die "Error: Cannot parse $arg\n$@\n" if $@;
  38. }
  39. else {
  40.     $content = $arg;
  41.     die "Error: Cannot find $arg\n" unless -e $content;
  42.     eval {
  43.         $rss->parsefile($content);
  44.     };
  45.     die "Error: Cannot parse $arg\n$@\n" if $@;
  46. }
  47.  
  48. mkdir $folder;
  49.  
  50. for my $item (@{$rss->{'items'}}) {
  51.     my $filename = &trim($item->{'title'});
  52.     my $itemdesc = &trim($item->{'description'});
  53.     my $text;
  54.     $text .= qq|$filename\n|;
  55.     $text .= qq|$itemdesc\n|;
  56.     open (OUTFILE, ">$folder/$filename") or die "Couldn't open \"$filename\": $!";
  57.     print OUTFILE $text;
  58.     close OUTFILE;
  59. }
  60.  
  61. exit;
  62.  
  63. # ---------------------------------------------------------------------------- #
  64.  
  65. sub trim{
  66.     my $value = $_[0];
  67.     if ($value) {
  68.         $value =~ s/^\s+//;
  69.         $value =~ s/\s+$//;
  70.     }
  71.     return $value;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement