| | 38 | # rss-recent.rb: RSS recent plugin |
| | 39 | # |
| | 40 | # rss_recnet: show recnet list from RSS |
| | 41 | # parameters (default): |
| | 42 | # url: URL of RSS |
| | 43 | # max: max of list itmes(5) |
| | 44 | # cache_time: cache time(second) of RSS(60*60) |
| | 45 | # template_head: rendering header part |
| | 46 | # template_list: rendering RSS item part(with loop) |
| | 47 | # template_foot: rendering footer part |
| | 48 | # |
| | 49 | # Copyright (c) 2003-2004 Kouhei Sutou <kou@cozmixng.org> |
| | 50 | # Distributed under the GPL |
| | 51 | # |
| | 52 | # Modified using template string and content:encoded |
| | 53 | # Version 0.0.5i2 by ishinao <ishinao@ishinao.net> |
| | 54 | # |
| | 55 | |
| | 56 | require "rss/rss" |
| | 57 | |
| | 58 | RSS_RECENT_FIELD_SEPARATOR = "\0" |
| | 59 | RSS_RECENT_ENTRY_SEPARATOR = "\1" |
| | 60 | RSS_RECENT_VERSION = "0.0.5i2" |
| | 61 | RSS_RECENT_HTTP_HEADER = { |
| | 62 | "User-Agent" => "tDiary RSS recent plugin version #{RSS_RECENT_VERSION}. " << |
| | 63 | "Using RSS parser version is #{::RSS::VERSION}.", |
| | 64 | } |
| | 65 | |
| | 66 | def hb_footer(url, max = 5, cache_time = 3600, \ |
| | 67 | template_head = "<ul>\n", \ |
| | 68 | template_list = '<li><span class="#{hb_footer_modified_class(time)}"><a href="#{CGI.escapeHTML(url)}" title="#{CGI.escapeHTML(title)}">#{CGI::escapeHTML(title)}</a></span></li>\n', \ |
| | 69 | template_foot = "</ul>\n") |
| | 70 | url.untaint |
| | 71 | |
| | 72 | cache_file = "#{@cache_path}/rss-recent.#{CGI.escape(url)}" |
| | 73 | |
| | 74 | hb_footer_cache_rss(url, cache_file, cache_time.to_i) |
| | 75 | |
| | 76 | return '' unless test(?r, cache_file) |
| | 77 | |
| | 78 | rv = template_head |
| | 79 | |
| | 80 | i = 0 |
| | 81 | hb_footer_read_from_cache(cache_file).each do |title, url, time, content, description| |
| | 82 | break if i >= max |
| | 83 | next if (url.nil? or title.nil?) |
| | 84 | rv << eval('%Q[' + template_list + ']') |
| | 85 | i += 1 |
| | 86 | end |
| | 87 | |
| | 88 | rv << template_foot |
| | 89 | |
| | 90 | if i > 0 |
| | 91 | rv |
| | 92 | else |
| | 93 | '' |
| | 94 | end |
| | 95 | end |
| | 96 | |
| | 97 | class InvalidResourceError < StandardError; end |
| | 98 | |
| | 99 | def hb_footer_cache_rss(url, cache_file, cache_time) |
| | 100 | |
| | 101 | cached_time = nil |
| | 102 | cached_time = File.mtime(cache_file) if File.exist?(cache_file) |
| | 103 | |
| | 104 | if cached_time.nil? or Time.now > cached_time + cache_time |
| | 105 | require 'time' |
| | 106 | require 'open-uri' |
| | 107 | require 'net/http' |
| | 108 | require 'uri/generic' |
| | 109 | require 'rss/parser' |
| | 110 | require 'rss/1.0' |
| | 111 | require 'rss/2.0' |
| | 112 | require 'rss/dublincore' |
| | 113 | require 'rss/content' |
| | 114 | |
| | 115 | begin |
| | 116 | uri = URI.parse(url) |
| | 117 | |
| | 118 | raise URI::InvalidURIError if uri.scheme != "http" |
| | 119 | |
| | 120 | rss_source = hb_footer_fetch_rss(uri, cached_time) |
| | 121 | |
| | 122 | raise InvalidResourceError if rss_source.nil? |
| | 123 | |
| | 124 | # parse RSS |
| | 125 | rss = ::RSS::Parser.parse(rss_source, false) |
| | 126 | raise ::RSS::Error if rss.nil? |
| | 127 | |
| | 128 | # pre processing |
| | 129 | begin |
| | 130 | rss.output_encoding = @conf.charset || charset |
| | 131 | rescue ::RSS::UnknownConversionMethodError |
| | 132 | end |
| | 133 | |
| | 134 | rss_infos = rss.items.collect do |item| |
| | 135 | hb_footer_pubDate_to_dc_date(item) |
| | 136 | [item.title, item.link, item.dc_date, item.content_encoded, item.description] |
| | 137 | end |
| | 138 | hb_footer_write_to_cache(cache_file, rss_infos) |
| | 139 | |
| | 140 | rescue URI::InvalidURIError |
| | 141 | hb_footer_write_to_cache(cache_file, [['Invalid URI', url]]) |
| | 142 | rescue InvalidResourceError, ::RSS::Error |
| | 143 | # hb_footer_write_to_cache(cache_file, [['Invalid Resource', url]]) |
| | 144 | # when cannot get valid RSS, use old cache |
| | 145 | end |
| | 146 | end |
| | 147 | |
| | 148 | end |
| | 149 | |
| | 150 | def hb_footer_fetch_rss(uri, cache_time) |
| | 151 | rss = nil |
| | 152 | begin |
| | 153 | uri.open(hb_footer_http_header(cache_time)) do |f| |
| | 154 | case f.status.first |
| | 155 | when "200" |
| | 156 | rss = f.read |
| | 157 | # STDERR.puts "Got RSS of #{uri}" |
| | 158 | when "304" |
| | 159 | # not modified |
| | 160 | # STDERR.puts "#{uri} does not modified" |
| | 161 | else |
| | 162 | raise InvalidResourceError |
| | 163 | end |
| | 164 | end |
| | 165 | rescue TimeoutError, SocketError, StandardError, |
| | 166 | SecurityError # occured in redirect |
| | 167 | raise InvalidResourceError |
| | 168 | end |
| | 169 | rss |
| | 170 | end |
| | 171 | |
| | 172 | def hb_footer_http_header(cache_time) |
| | 173 | header = RSS_RECENT_HTTP_HEADER.dup |
| | 174 | if cache_time.respond_to?(:rfc2822) |
| | 175 | header["If-Modified-Since"] = cache_time.rfc2822 |
| | 176 | end |
| | 177 | header |
| | 178 | end |
| | 179 | |
| | 180 | def hb_footer_write_to_cache(cache_file, rss_infos) |
| | 181 | File.open(cache_file, 'w') do |f| |
| | 182 | f.flock(File::LOCK_EX) |
| | 183 | rss_infos.each do |info| |
| | 184 | f << info.join(RSS_RECENT_FIELD_SEPARATOR) |
| | 185 | f << RSS_RECENT_ENTRY_SEPARATOR |
| | 186 | end |
| | 187 | f.flock(File::LOCK_UN) |
| | 188 | end |
| | 189 | end |
| | 190 | |
| | 191 | def hb_footer_read_from_cache(cache_file) |
| | 192 | require 'time' |
| | 193 | infos = [] |
| | 194 | File.open(cache_file) do |f| |
| | 195 | while info = f.gets(RSS_RECENT_ENTRY_SEPARATOR) |
| | 196 | info = info.chomp(RSS_RECENT_ENTRY_SEPARATOR) |
| | 197 | infos << info.split(RSS_RECENT_FIELD_SEPARATOR) |
| | 198 | end |
| | 199 | end |
| | 200 | infos.collect do |title, url, time, content, description| |
| | 201 | [ |
| | 202 | hb_footer_convert(title), |
| | 203 | hb_footer_convert(url), |
| | 204 | hb_footer_convert(time) {|time| Time.parse(time)}, |
| | 205 | hb_footer_convert(content), |
| | 206 | hb_footer_convert(description), |
| | 207 | ] |
| | 208 | end |
| | 209 | end |
| | 210 | |
| | 211 | def hb_footer_convert(str) |
| | 212 | if str.nil? or str.empty? |
| | 213 | nil |
| | 214 | else |
| | 215 | if block_given? |
| | 216 | yield str |
| | 217 | else |
| | 218 | str |
| | 219 | end |
| | 220 | end |
| | 221 | end |
| | 222 | |
| | 223 | # from RWiki |
| | 224 | def hb_footer_modified(t) |
| | 225 | return '-' unless t |
| | 226 | dif = (Time.now - t).to_i |
| | 227 | dif = dif / 60 |
| | 228 | return "#{dif}m" if dif <= 60 |
| | 229 | dif = dif / 60 |
| | 230 | return "#{dif}h" if dif <= 24 |
| | 231 | dif = dif / 24 |
| | 232 | return "#{dif}d" |
| | 233 | end |
| | 234 | |
| | 235 | # from RWiki |
| | 236 | def hb_footer_modified_class(t) |
| | 237 | return 'dangling' unless t |
| | 238 | dif = (Time.now - t).to_i |
| | 239 | dif = dif / 60 |
| | 240 | return "modified-hour" if dif <= 60 |
| | 241 | dif = dif / 60 |
| | 242 | return "modified-today" if dif <= 24 |
| | 243 | dif = dif / 24 |
| | 244 | return "modified-month" if dif <= 30 |
| | 245 | return "modified-year" if dif <= 365 |
| | 246 | return "modified-old" |
| | 247 | end |
| | 248 | |
| | 249 | def hb_footer_pubDate_to_dc_date(target) |
| | 250 | if target.respond_to?(:pubDate) |
| | 251 | class << target |
| | 252 | alias_method(:dc_date, :pubDate) |
| | 253 | end |
| | 254 | end |
| | 255 | end |