2009年6月20日土曜日

Twitter crawler with ruby 3

ライブラリを使わないでTwitter APIを使ってみた。

(JSONパーサは使ってるけど)

--------------------------------------------------------------------------------
require 'net/http'
require 'json'

request = Net::HTTP::Get.new('/search.json?q=hoge&rpp=2')
http = Net::HTTP.start('search.twitter.com')
response = http.request(request)
puts JSON.parse(response.body)
---------------------------------------------------------------------------------

>request = Net::HTTP::Get.new('/search.json?q=hoge&rpp=2')
はjson形式で結果を取得、文字列hogeを含む最新の発言を最大2件取得するという意味。


>response = http.request(request)
で、responseにはjson形式の文字列が返される


>puts JSON.parse(response.body)
response.bodyは、responseから#<net::httpok:0xc5f9e0>部分を取ったもの。
それをパースしてHash型で返す。


json形式の文字列がどんなものかは

http://apiwiki.twitter.com/Twitter-Search-API-Method%3A-search

のJSON exampleを見ればOK!

0 件のコメント:

コメントを投稿