szsk (owner)

Revisions

gist: 4033 Download_button fork
public
Description:
sbmcomments
Public Clone URL: git://gist.github.com/4033.git
Embed All Files: show embed
sbmcomments.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# 以下のスクリプトを元に作成しています
# SBM - コメントを取得 | Diaspar Journal
# http://diaspar.jp/node/145
 
require 'net/http'
require 'open-uri'
require 'rubygems'
require 'json/pure'
require 'digest/md5'
require 'simple-rss'
require 'mechanize'
require 'time'
require 'cgi'
 
Net::HTTP.version_1_2
 
site_url = 'http://bmky.net/'
api_url = "http://www.enjoyxstudy.com/urls/get.cgi?url=" + CGI.escape( site_url )
Save_dir = "comments/"
post_reg = %r|^http://bmky\.net/diary/log/(\d+)\.html|
 
def get_hatena_comments( url )
posts = []
host = 'b.hatena.ne.jp'
path = '/entry/json/' + url
body = Net::HTTP.start( host, 80 ).get( path ).body
unless body =~ /^\(null\)/
json = JSON.parse( body.sub!( %r!^\((.*)\)$! ) { $1 } )
json['bookmarks'].each do |item|
if item['comment'] != ''
post = {
:date => Time.parse( item['timestamp'][0..9].gsub( %r!/!, '-' ) ),
:user => item['user'],
:cmnt => item['comment'],
:site => "hatena"
}
posts << post
end
end
end
return posts
end
 
def get_delicious_comments( url )
posts = []
host = 'feeds.delicious.com'
path = '/v2/rss/url/' + Digest::MD5.hexdigest( url )
body = Net::HTTP.start( host, 80 ).get( path ).body
rss = SimpleRSS.new( body )
if rss.items != nil
rss.items.each do |item|
next unless item.description
post = {
:date => item.pubDate,
:user => item.dc_creator,
:cmnt => item.description,
:site => "del.icio.us"
}
posts << post
end
end
return posts
end
 
def create_comments_html( posts, id )
return if posts.empty?
comments = []
html = []
html << '<dl id="sbmcomments">'
posts.each { |item|
if item[:site] == "hatena"
icon = '<img src="http://www.hatena.ne.jp/users/Ri/' + item[:user] + '/profile_s.gif" width="16" height="16" />'
user = '<a href="http://b.hatena.ne.jp/' + item[:user] + '/' + item[:date].strftime( "%Y%m%d" ) + '">' + item[:user] + '</a>'
else
icon = '<img src="/images/icon_add_del.gif" width="16" height="16" />'
user = '<a href="http://del.icio.us/' + item[:user] + '">' + item[:user] + '</a>'
end
html << '<dt class="name">' + icon + " " + user + "</dt>"
unless comments.include?( item[:cmnt] )
html << '<dd class="desc">' + CGI.escapeHTML( item[:cmnt] ) + "</dd>"
comments << item[:cmnt]
end
}
html << "</dl>"
File.open( Save_dir + id.to_s + ".html", "w" ) do |f|
f.puts html.join( "\n" )
end
end
 
 
 
 
 
# Get Urls
agent = WWW::Mechanize.new
urls = agent.get_file( api_url ).gsub( /\r/, "" )
 
# コメントファイルを全て削除
# (Wordpressからファイルの有無で条件分岐させているため)
Dir.foreach( Save_dir ) do |file|
File.unlink( Save_dir + file ) if file =~ /\.html/
end
 
urls.gsub( post_reg ) do
posts = []
url = $&
id = $1
posts += get_hatena_comments( url )
posts += get_delicious_comments( url )
posts.sort! do |a,b|
a[:date] <=> b[:date]
end
 
posts.reverse!
 
create_comments_html( posts, id )
sleep 1
end