Module: Crown::Hatena::Bookmark

Defined in:
lib/crown/hatena/bookmark.rb

Defined Under Namespace

Classes: LinkTrace, Related, Response, User

Class Method Summary collapse

Class Method Details

.count(uri, proxy_host = nil, proxy_port = nil) ⇒ Object

————————————————————— #

count

uri に指定したページのブックマーク数を取得する.

————————————————————— #



156
157
158
159
160
161
162
163
164
165
166
# File 'lib/crown/hatena/bookmark.rb', line 156

def count(uri, proxy_host = nil, proxy_port = nil)
    session = Net::HTTP.new('api.b.st-hatena.com', 80, proxy_host, proxy_port)
    path = '/entry.count?url=' + CGI.escape(uri)
    begin
        result = Crown::HTTP.get(session, path)
        return 0 if (result == nil || result.code.to_i != 200)
        return result.body.to_i
    rescue Exception => e
        return 0
    end
end

.each(query, proxy_host = nil, proxy_port = nil) ⇒ Object

————————————————————— #

each

LinkTrace クラスを使用してブックマークされている記事の URL
一覧を取得し,それらのブックマーク情報を順に返す.

————————————————————— #



215
216
217
218
219
220
221
222
223
224
# File 'lib/crown/hatena/bookmark.rb', line 215

def each(query, proxy_host = nil, proxy_port = nil)
    tracer = LinkTrace.new(query, proxy_host, proxy_port)
    while (tracer.more?)
        uris = tracer.get
        uris.each { |uri|
            result = Bookmark.get(uri, proxy_host, proxy_port)
            yield(result) if (result != nil)
        }
    end
end

.get(uri, proxy_host = nil, proxy_port = nil) ⇒ Object

————————————————————— #

get

uri に指定したページのブックマーク情報を取得する.

————————————————————— #



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/crown/hatena/bookmark.rb', line 175

def get(uri, proxy_host = nil, proxy_port = nil)
    session = Net::HTTP.new('b.hatena.ne.jp', 80, proxy_host, proxy_port)
    path = '/entry/json/?url=' + CGI.escape(uri)
    
    begin
        result = Crown::HTTP.get(session, path)
        return nil if (result == nil || result.code.to_i != 200)
        json = JSON.parse(result.body)
        
        users = Hash.new
        json['bookmarks'].each { |item|
            tags = Array.new
            item['tags'].each { |tag|
                tags.push(tag)
            }
            date = Time.local(*item['timestamp'].split(/\W/))
            entry = User.new(tags, date, item['comment'])
            users[item['user']] = entry
        }
        
        related = Array.new()
        json['related'].each { |item|
            entry = Related.new(item['eid'].to_i, item['url'], item['title'], item['count'].to_i)
            related.push(entry)
        }
    rescue Exception
        return nil
    end
    
    return Response.new(json['eid'].to_i, json['url'], json['screenshot'], json['title'], json['count'].to_i, users, related)
end