Advertisement
harvest316

rss2txt.pl

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