RSS広告カットプロキシー

RSSの広告がうざいので広告カットプロキシーを作ってみました。
文字によるパターンマッチングですので広告表示方法が変わったり、逆に広告でないものがカットされることがあったら直して使ってください。

http://solisart.com/rss/?uri=[rssatomuri]
とすると、AD: やPR:の入っているアイテムを消すことができると思います。
(上のURLは有効ですが、アクセスが多いと停止するかもしれません。あしからず。)



index.cgi

#!/usr/local/bin/ruby

require 'cgi'
require 'open-uri'
require 'rexml/document'

# 以下の2行はxmlスパム防止のFIXですが、導入されていない場合はコメントアウトしてください。
# require 'rubygems'
# require 'rexml-expansion-fix'

cgi = CGI.new

uri = cgi.params['uri'][0]

rss = open(uri).read

doc = REXML::Document.new(rss)

adxpath_list = []

    if doc.root.name == 'rss' and doc.root.attribute("version").value == "2.0"
      doc.elements.each("//rss/channel/item/title") do |element|
        if element.text["AD:"] or element.text["PR:"]
          adxpath_list <<  element.xpath.gsub(/\/title$/, "") 
        end
       end
    elsif doc.root.name == 'RDF'
      doc.elements.each("*/item/title") do |element|
        if element.text["AD:"] or element.text["PR:"]
          adxpath_list <<  element.xpath.gsub(/\/title$/, "") 
        end
       end
    else
      doc.elements.each("//entry/title") do |element|
        if element.text["AD:"] or element.text["PR:"]
          adxpath_list <<  element.xpath.gsub(/\/title$/, "") 
        end
       end
      
    end


adxpath_list.reverse.each do |xpath|
  REXML::XPath.first(doc, xpath).remove
end

# スタイルシートの指定で、"http"から始まっていないことがあるので
output = doc.to_s.sub(/<\?xml-stylesheet[^>]*>/, '')
cgi.out( {'type' => 'application/xml'} ) { output }


?uri=を消してシンプルにしたい場合は、サーバー環境にもよりますが、以下のように設定すると
http://solisart.com/rss/[rssatomuri] となってシンプルになります。
(全てのRSSでうまくいくわけでないのですが、まだよくわかりません)

.htaccess

DirectoryIndex index.cgi
Options +ExecCGI  +FollowSymLinks

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)$ /rss/?uri=$1

AddHandler cgi-script .cgi

広告関係の方、ごめんなさい。
AdBlockStylishを使って広告カットする人はたぶん一部だと思いますから。