root/platform/tdiary/plugin/pathtraq_counter.rb

Revision 17620, 0.9 kB (checked in by hsbt, 4 months ago)

small fix.

Line 
1# pathtraq_counter.rb $Revision 1.0 $
2#
3# Copyright (c) 2008 SHIBATA Hiroshi <shibata.hiroshi@gmail.com>
4# You can redistribute it and/or modify it under GPL2.
5#
6
7require 'timeout'
8require 'open-uri'
9require 'rubygems'
10require 'json/ext'
11
12def call_pathtraq_json( url, mode )
13        json = nil
14        begin
15                timeout(10) do
16                        open( "http://api.pathtraq.com/page_counter?api=json&url=#{url}&m=#{mode}" ) do |f|
17                                json = JSON.parse( f.read )
18                        end
19                end
20        rescue => e
21                @conf.debug( e )
22        end
23        return json
24end
25
26def pathtraq_counter
27        url = @conf.base_url
28        mode = ['popular', 'hot', 'upcoming']
29
30        r = %Q|<ul>\n|
31        begin
32                mode.each do |m|
33                        json = call_pathtraq_json( url, m )
34                        r << %Q|<li>#{m}: #{json["count"]} hits</li>\n| unless json.nil?
35                end
36        rescue => e
37                @conf.debug( e )
38        end
39        r << %Q|</ul>\n|
40        r << %Q|<div class="iddy"><span class="iddy-powered">Powered by <a href="http://pathtraq.com/">Pathtraq</a></span></div>\n|
41
42        return r
43end
Note: See TracBrowser for help on using the browser.