Created
June 18, 2009 12:44
Revisions
-
DRMacIver created this gist
Jun 18, 2009 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,10 @@ #!/usr/bin/ruby require "twitter" user = ARGV[0] File.open("data/users/#{user}", "w") do |o| Twitter.all_tweets(user) do |tweet| o.puts tweet["text"] end end This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,107 @@ require "rubygems" require "httparty" class TwitterSearch include HTTParty base_uri "http://search.twitter.com" format :json def self.search(string, params={}) params[:q] = string get "/search.json", :query => params end # Fetch everything in the search history for this query. def self.full_search(string, modifiers = {}) modifiers[:rpp] = 100 results = search(string, modifiers) all_results = results["results"] while (next_page = results["next_page"]) results = get("/search.json" + next_page) all_results += results["results"] end all_results end end class TwitterException < Exception end class Twitter include HTTParty base_uri "http://twitter.com" format :json def self.tweets(screen_name) # this is the lamest caching in the entire world # TODO: Pls fix me @tweets_cache ||= {} results, time = @tweets_cache[screen_name] if !time || (Time.now - time > 300) results = get("/statuses/user_timeline.json", :query => {"id" => screen_name, :count => 50}) @tweets_cache[screen_name] = [results, Time.now] else STDOUT.puts "hit the cache. No need to bother twitter" end raise TwitterException.new(results["error"]) if results.empty? && results["error"] results end def self.friends(screen_name) @friends_cache ||= {} results, time = @friends_cache[screen_name] if !time || (Time.now - time > 300) results = get("/statuses/friends.json", :query => {"id" => screen_name}) @friends_cache[screen_name] = [results, Time.now] else STDOUT.puts "hit the cache. No need to bother twitter" end raise TwitterException.new(results["error"]) if results.empty? && results["error"] results.map{|f| f["screen_name"]} end def self.user(screen_name) @users_cache ||= {} results, time = @users_cache[screen_name] if !time || (Time.now - time > 300) results = get("/users/show.json", :query => {"id" => screen_name}) @users_cache[screen_name] = [results, Time.now] else STDOUT.puts "hit the cache. No need to bother twitter" end raise TwitterException.new(results["error"]) if results.empty? && results["error"] results end def self.all_tweets(user, params={}) if !block_given? results = [] all_tweets(user, params) do |tweet| results << tweet end return results end new_tweets = nil page = 0 while !(new_tweets = get("/statuses/user_timeline.json", :query => {"id" => user, :page => page, :count => 200})).empty? new_tweets.each{|tweet| yield(tweet) } page += 1 end nil; end end