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