Skip to content

Instantly share code, notes, and snippets.

@jberkel
Forked from DRMacIver/fetch_user.rb
Created June 18, 2009 12:48
Show Gist options
  • Save jberkel/131888 to your computer and use it in GitHub Desktop.
Save jberkel/131888 to your computer and use it in GitHub Desktop.
#!/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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment