simplify.pl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 2 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, write to the
  16. # Free Software Foundation, Inc.
  17. # 59 Temple Place, Suite 330
  18. # Boston, MA 02111-1307 USA
  19. use CGI qw/:standard/;
  20. use CGI::Carp qw(fatalsToBrowser);
  21. use XML::RSS;
  22. use LWP::UserAgent;
  23. use encoding 'utf8';
  24. my $wikins = 'http://purl.org/rss/1.0/modules/wiki/';
  25. my $rdfns = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
  26. my $output = '0.91';
  27. if (not param('url')) {
  28. print header(),
  29. start_html('RSS Simplification'),
  30. h1('RSS Simplification'),
  31. p('Translates any RSS feed to Really Simple Syndication ', $output,
  32. 'It understands ModWiki, and will use wiki:diff as the link,',
  33. 'and it will add dc:contributor to the description.'),
  34. start_form(-method=>'GET'),
  35. p('RSS feed: ', textfield('url', '', 70)),
  36. p(submit()),
  37. end_form(),
  38. end_html();
  39. exit;
  40. }
  41. print header(-type=>'text/plain; charset=UTF-8');
  42. my $rss = new XML::RSS(output=>$output);
  43. my $ua = new LWP::UserAgent;
  44. my $request = HTTP::Request->new('GET', param('url'));
  45. my $response = $ua->request($request);
  46. my $data = $response->content;
  47. eval {
  48. local $SIG{__DIE__} = sub { parse_rss3(); }; # parsing errors -> try RSS 3.0!
  49. $rss->parse($data);
  50. munge_rss();
  51. };
  52. sub munge_rss {
  53. foreach my $i (@{$rss->{items}}) {
  54. if ($i->{dc}->{contributor}) {
  55. if ($i->{description}) {
  56. $i->{description} = $i->{description} . ' -- ' . $i->{dc}->{contributor};
  57. } else {
  58. $i->{description} = '-- ' .$i->{dc}->{contributor};
  59. }
  60. }
  61. if ($i->{$wikins}->{diff}) {
  62. $i->{link} = $i->{$wikins}->{diff};
  63. }
  64. }
  65. print $rss->as_string();
  66. }
  67. # perl simplify.pl 'url=http://localhost/cgi-bin/wiki.pl?search=foo%3braw=1'
  68. sub parse_rss3 {
  69. $rss->add_module(
  70. prefix => 'wiki',
  71. uri => 'http://purl.org/rss/1.0/modules/wiki/'
  72. );
  73. my @entries = ();
  74. foreach my $entry (split(/\n\n+/, $data)) {
  75. my %entry = ();
  76. while ($entry =~ /(\S+?): (.*?)(?=\n[^\t]|\Z)/sg) {
  77. my ($key, $value) = ($1, $2);
  78. $value =~ s/\n\t/\n/g;
  79. $entry{$key} = $value if $value;
  80. }
  81. push(@entries, \%entry);
  82. }
  83. # the first entry is the channel
  84. my %entry = %{shift(@entries)};
  85. $rss->channel(%entry);
  86. # the rest are items
  87. while (@entries) {
  88. my %entry = %{shift(@entries)};
  89. my %dc = (date => $entry{'last-modified'},
  90. contributor => $entry{generator},);
  91. my %wiki = (size => $entry{size},);
  92. $entry{dc} = %dc;
  93. $entry{wiki} = %wiki;
  94. for my $key qw(last-modified generator size) { delete $entry{$key}; }
  95. $rss->add_item(%entry);
  96. }
  97. print $rss->as_string();
  98. }