Changeset 4284 for platform/tdiary

Show
Ignore:
Timestamp:
01/09/08 19:52:05 (5 years ago)
Author:
hsbt
Message:

platform/tdiary/plugin/hb_footer4sec.rb: merge rss-recent.rb.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • platform/tdiary/plugin/hb_footer4sec.rb

    r3279 r4284  
    22# hb_footer.rb 
    33# 
    4 # はてなブックマーク (http://b.hatena.ne.jp/) のコメントを該当セクションに貼り付けるtDiaryプラグイン 
    5 # 改造版rss_recent Version 0.0.5i2と共に使用する 
    6 # 
     4# ��ϒ�ƒ�ʒ�֒�Ò����ޒ����� (http://b.hatena.ne.jp/) ��Β����ᒥ���򒳺�������������璥��Ž���Ւ�����iary��ג�钥�����������¤��rss_recent Version 0.0.5i2��Ȓ����˒�Ȓ������� 
    75# Licence: GPL 
    86# Author: ishinao <ishinao@ishinao.net> 
     
    1412  rss_url = "http://b.hatena.ne.jp/entry/rss/#{td_url}" 
    1513 
    16   template_head = %Q[<div class="section">\n<h4>このセクションに対する<a href="#{CGI.escapeHTML(hb_url)}">はてブ</a></h4>\n<ul class="hb_footer">\n] 
    17   template_list = '<li><span class="date">#{time.strftime("%Y年%m月%d日")}</span> <span class="hatenaid"><a href="#{CGI.escapeHTML(url)}">#{CGI.escapeHTML(title)}</a></span> <span class="comment">#{CGI.escapeHTML(description.to_s)}</span></li>' 
     14  template_head = %Q[<div class="section">\n<h4>�����Β����������璥��������� href="#{CGI.escapeHTML(hb_url)}">��ϒ�ƒ��/a></h4>\n<ul class="hb_footer">\n] 
     15  template_list = '<li><span class="date">#{time.strftime("%Y�ǯ%m�����")}</span> <span class="hatenaid"><a href="#{CGI.escapeHTML(url)}">#{CGI.escapeHTML(title)}</a></span> <span class="comment">#{CGI.escapeHTML(description.to_s)}</span></li>' 
    1816  template_foot = "</ul>\n</div>\n" 
    1917 
     
    2220    cache_time = 3600 * 12; 
    2321  end 
    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) 
     23end 
     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 
     43require "rss/rss" 
     44 
     45RSS_RECENT_FIELD_SEPARATOR = "\0" 
     46RSS_RECENT_ENTRY_SEPARATOR = "\1" 
     47RSS_RECENT_VERSION = "0.0.5i2" 
     48RSS_RECENT_HTTP_HEADER = { 
     49        "User-Agent" => "tDiary RSS recent plugin version #{RSS_RECENT_VERSION}. " << 
     50        "Using RSS parser version is #{::RSS::VERSION}.", 
     51} 
     52 
     53def 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 
     82end 
     83 
     84class InvalidResourceError < StandardError; end 
     85 
     86def 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 
     135end 
     136 
     137def 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 
     157end 
     158 
     159def 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 
     165end 
     166 
     167def 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 
     176end 
     177 
     178def 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 
     196end 
     197 
     198def 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 
     208end 
     209 
     210# from RWiki 
     211def 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" 
     220end 
     221 
     222# from RWiki 
     223def 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" 
     234end 
     235 
     236def 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 
     242end