simplify.pl 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #! /usr/bin/perl
  2. # Copyright (C) 2003, 2004 Alex Schroeder <alex@emacswiki.org>
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. use CGI qw/:standard/;
  17. use CGI::Carp qw(fatalsToBrowser);
  18. use XML::RSS;
  19. use LWP::UserAgent;
  20. use encoding 'utf8';
  21. my $wikins = 'http://purl.org/rss/1.0/modules/wiki/';
  22. my $rdfns = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
  23. my $output = '0.91';
  24. if (not param('url')) {
  25. print header(),
  26. start_html('RSS Simplification'),
  27. h1('RSS Simplification'),
  28. p('Translates any RSS feed to Really Simple Syndication ', $output,
  29. 'It understands ModWiki, and will use wiki:diff as the link,',
  30. 'and it will add dc:contributor to the description.'),
  31. start_form(-method=>'GET'),
  32. p('RSS feed: ', textfield('url', '', 70)),
  33. p(submit()),
  34. end_form(),
  35. end_html();
  36. exit;
  37. }
  38. print header(-type=>'text/plain; charset=UTF-8');
  39. my $rss = new XML::RSS(output=>$output);
  40. my $ua = new LWP::UserAgent;
  41. my $request = HTTP::Request->new('GET', param('url'));
  42. my $response = $ua->request($request);
  43. my $data = $response->content;
  44. eval {
  45. local $SIG{__DIE__} = sub { parse_rss3(); }; # parsing errors -> try RSS 3.0!
  46. $rss->parse($data);
  47. munge_rss();
  48. };
  49. sub munge_rss {
  50. foreach my $i (@{$rss->{items}}) {
  51. if ($i->{dc}->{contributor}) {
  52. if ($i->{description}) {
  53. $i->{description} = $i->{description} . ' -- ' . $i->{dc}->{contributor};
  54. } else {
  55. $i->{description} = '-- ' .$i->{dc}->{contributor};
  56. }
  57. }
  58. if ($i->{$wikins}->{diff}) {
  59. $i->{link} = $i->{$wikins}->{diff};
  60. }
  61. }
  62. print $rss->as_string();
  63. }
  64. # perl simplify.pl 'url=http://localhost/cgi-bin/wiki.pl?search=foo%3braw=1'
  65. sub parse_rss3 {
  66. $rss->add_module(
  67. prefix => 'wiki',
  68. uri => 'http://purl.org/rss/1.0/modules/wiki/'
  69. );
  70. my @entries = ();
  71. foreach my $entry (split(/\n\n+/, $data)) {
  72. my %entry = ();
  73. while ($entry =~ /(\S+?): (.*?)(?=\n[^\t]|\Z)/sg) {
  74. my ($key, $value) = ($1, $2);
  75. $value =~ s/\n\t/\n/g;
  76. $entry{$key} = $value if $value;
  77. }
  78. push(@entries, \%entry);
  79. }
  80. # the first entry is the channel
  81. my %entry = %{shift(@entries)};
  82. $rss->channel(%entry);
  83. # the rest are items
  84. while (@entries) {
  85. my %entry = %{shift(@entries)};
  86. my %dc = (date => $entry{'last-modified'},
  87. contributor => $entry{generator},);
  88. my %wiki = (size => $entry{size},);
  89. $entry{dc} = %dc;
  90. $entry{wiki} = %wiki;
  91. for my $key qw(last-modified generator size) { delete $entry{$key}; }
  92. $rss->add_item(%entry);
  93. }
  94. print $rss->as_string();
  95. }