| 1 | # |
|---|
| 2 | # mm_footer.rb |
|---|
| 3 | # |
|---|
| 4 | # MM ( http://1470.net/mm/) のmylistを各日付に貼り付けるtDiaryプラグイン |
|---|
| 5 | # |
|---|
| 6 | # Licence: GPL |
|---|
| 7 | # Author: ishinao <ishinao@ishinao.net> |
|---|
| 8 | # |
|---|
| 9 | |
|---|
| 10 | require 'nkf' |
|---|
| 11 | |
|---|
| 12 | add_body_leave_proc(Proc.new do |date| |
|---|
| 13 | |
|---|
| 14 | oldest_date = Time.local(2005, 1, 11) |
|---|
| 15 | if date > oldest_date |
|---|
| 16 | if @mode == 'day' or @mode == 'latest' |
|---|
| 17 | if date < Time.local(2006, 7, 6) |
|---|
| 18 | mm_user = 82 # your MM id |
|---|
| 19 | url = "http://1470.net/mm/mylist.html/#{mm_user}?date=#{date.strftime('%Y-%m-%d')}" |
|---|
| 20 | rssurl = "http://1470.net/mm/mylist.html/#{mm_user}?date=#{date.strftime('%Y-%m-%d')}&mode=rss" |
|---|
| 21 | template_list = '<li>#{content}</li>' |
|---|
| 22 | else |
|---|
| 23 | userName = 'hsbt' # your 1470.net id |
|---|
| 24 | url = "http://1470.net/user/#{userName}/#{date.strftime('%Y/%m/%d')}" |
|---|
| 25 | rssurl = "http://1470.net/user/#{userName}/#{date.strftime('%Y/%m/%d')}/feed" |
|---|
| 26 | template_list = '<li>#{content.gsub(/href="\//, "href=\"http://1470.net\/")}</li>' |
|---|
| 27 | end |
|---|
| 28 | |
|---|
| 29 | template_head = %Q[<div class="section mm_footer">\n<h3><a href="#{CGI.escapeHTML(url)}">今日のメモ</a> powered by <a href="http://1470.net/">1470.net</a></h3>\n<ul class="mm_footer">\n] |
|---|
| 30 | template_foot = "</ul>\n</div>\n" |
|---|
| 31 | |
|---|
| 32 | cache_time = 3600; |
|---|
| 33 | if date.strftime('%Y-%m-%d') != Time.now.strftime('%Y-%m-%d') |
|---|
| 34 | cache_time = 3600 * 12; |
|---|
| 35 | end |
|---|
| 36 | NKF.nkf('-e', mm_footer(rssurl, 50, cache_time, template_head, template_list, template_foot)) |
|---|
| 37 | else |
|---|
| 38 | '' |
|---|
| 39 | end |
|---|
| 40 | end |
|---|
| 41 | end) |
|---|
| 42 | |
|---|
| 43 | require "rss/rss" |
|---|
| 44 | |
|---|
| 45 | MM_FOOTER_FIELD_SEPARATOR = "\0" |
|---|
| 46 | MM_FOOTER_ENTRY_SEPARATOR = "\1" |
|---|
| 47 | MM_FOOTER_VERSION = "0.0.5i" |
|---|
| 48 | MM_FOOTER_HTTP_HEADER = { |
|---|
| 49 | "User-Agent" => "tDiary RSS recent plugin version #{MM_FOOTER_VERSION}. " << |
|---|
| 50 | "Using RSS parser version is #{::RSS::VERSION}.", |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | def mm_footer(url, max = 5, cache_time = 3600, \ |
|---|
| 54 | template_head = "<ul>\n", \ |
|---|
| 55 | template_list = '<li><span class="#{mm_footer_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/rss-recent.#{CGI.escape(url)}" |
|---|
| 60 | |
|---|
| 61 | mm_footer_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 | mm_footer_read_from_cache(cache_file).each do |title, url, time, content| |
|---|
| 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 mm_footer_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 = mm_footer_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 | mm_footer_pubDate_to_dc_date(item) |
|---|
| 123 | [item.title, item.link, item.dc_date, item.content_encoded] |
|---|
| 124 | end |
|---|
| 125 | mm_footer_write_to_cache(cache_file, rss_infos) |
|---|
| 126 | |
|---|
| 127 | rescue URI::InvalidURIError |
|---|
| 128 | mm_footer_write_to_cache(cache_file, [['Invalid URI', url]]) |
|---|
| 129 | rescue InvalidResourceError, ::RSS::Error |
|---|
| 130 | # mm_footer_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 mm_footer_fetch_rss(uri, cache_time) |
|---|
| 138 | rss = nil |
|---|
| 139 | begin |
|---|
| 140 | uri.open(mm_footer_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 mm_footer_http_header(cache_time) |
|---|
| 160 | header = MM_FOOTER_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 mm_footer_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(MM_FOOTER_FIELD_SEPARATOR) |
|---|
| 172 | f << MM_FOOTER_ENTRY_SEPARATOR |
|---|
| 173 | end |
|---|
| 174 | f.flock(File::LOCK_UN) |
|---|
| 175 | end |
|---|
| 176 | end |
|---|
| 177 | |
|---|
| 178 | def mm_footer_read_from_cache(cache_file) |
|---|
| 179 | require 'time' |
|---|
| 180 | infos = [] |
|---|
| 181 | File.open(cache_file) do |f| |
|---|
| 182 | while info = f.gets(MM_FOOTER_ENTRY_SEPARATOR) |
|---|
| 183 | info = info.chomp(MM_FOOTER_ENTRY_SEPARATOR) |
|---|
| 184 | infos << info.split(MM_FOOTER_FIELD_SEPARATOR) |
|---|
| 185 | end |
|---|
| 186 | end |
|---|
| 187 | infos.collect do |title, url, time, content| |
|---|
| 188 | [ |
|---|
| 189 | mm_footer_convert(title), |
|---|
| 190 | mm_footer_convert(url), |
|---|
| 191 | mm_footer_convert(time) {|time| Time.parse(time)}, |
|---|
| 192 | mm_footer_convert(content) |
|---|
| 193 | ] |
|---|
| 194 | end |
|---|
| 195 | end |
|---|
| 196 | |
|---|
| 197 | def mm_footer_convert(str) |
|---|
| 198 | if str.nil? or str.empty? |
|---|
| 199 | nil |
|---|
| 200 | else |
|---|
| 201 | if block_given? |
|---|
| 202 | yield str |
|---|
| 203 | else |
|---|
| 204 | str |
|---|
| 205 | end |
|---|
| 206 | end |
|---|
| 207 | end |
|---|
| 208 | |
|---|
| 209 | def mm_footer_pubDate_to_dc_date(target) |
|---|
| 210 | if target.respond_to?(:pubDate) |
|---|
| 211 | class << target |
|---|
| 212 | alias_method(:dc_date, :pubDate) |
|---|
| 213 | end |
|---|
| 214 | end |
|---|
| 215 | end |
|---|