以下代码只能运行一次。我在循环中调用了函数,但它不起作用。
我的代码:
def graphRepresent(max_id)
baseurl = "https://api.twitter.com"
path = "/1.1/statuses/user_timeline.json"
query = URI.encode_www_form(
"screen_name" => "#{@name["screen_name"]}",
"count" => 200,
)
if max_id>0 then
my_id = URI.encode_www_form(
"max_id" => max_id,
)
address = URI("#{baseurl}#{path}?#{query}&#{my_id}")
else
address = URI("#{baseurl}#{path}?#{query}")
end
request = Net::HTTP::Get.new address.request_uri
def print_timeline(time_graph)
# ADD CODE TO ITERATE THROUGH EACH TWEET AND PRINT ITS TEXT
today_date = DateTime.parse("#{Date.today}")
tf_date = today_date.strftime('%a %b %d %H:%M:%S +0000 %Y')
today_tweets = 0
today_favorite = 0
today_retweet = 0
yesterday_tweets = 0
yesterday_favorite = 0
yesterday_retweet = 0
week_tweets = 0
week_favorite = 0
week_retweet = 0
month_tweets = 0
month_favorite = 0
month_retweet = 0
index =0
time_graph.each_with_index do |gtweets, index|
if gtweets["created_at"] >= Time.parse("#{Date.today}") then
today_favorite += gtweets["favorite_count"]
today_retweet += gtweets["retweet_count"]
today_tweets += 1
end
if gtweets["created_at"] >= Time.parse("#{Date.today.beginning_of_month}") then
month_favorite += gtweets["favorite_count"]
month_retweet += gtweets["retweet_count"]
month_tweets += 1
end
if gtweets["created_at"] >= Time.parse("#{Date.today.beginning_of_week}") then
week_favorite += gtweets["favorite_count"]
week_retweet += gtweets["retweet_count"]
week_tweets += 1
end
if gtweets["created_at"] >= Time.parse("#{Date.yesterday}") && gtweets["created_at"] < Time.parse("#{Date.today}") then
yesterday_favorite += gtweets["favorite_count"]
yesterday_retweet += gtweets["retweet_count"]
yesterday_tweets += 1
end
if index == 199 then
puts " Done one function ----------------------------------------------------------#{index} "
max_id = gtweets["id"]
graphRepresent(max_id)
end
end
@tweets_today = today_tweets
@favorite_today = today_favorite
@retweet_today = today_retweet
@tweets_yesterday = yesterday_tweets
@favorite_yesterday = yesterday_favorite
@retweet_yesterday = yesterday_retweet
@tweets_week = week_tweets
@favorite_week = week_favorite
@retweet_week = week_retweet
@tweets_month = month_tweets
@favorite_month = month_favorite
@retweet_month = month_retweet
end
puts "#{@tweets_week} Tweets in a Week :)"
http = Net::HTTP.new address.host, address.port
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
consumer_key ||= OAuth::Consumer.new "xxxxxxxxxxxxxxxxxxx", "xxxxxxxxxxxxxxxxxxx"
access_token ||= OAuth::Token.new session['access_token'], session['access_token_secret']
# Issue the request.
request.oauth! http, consumer_key, access_token
http.start
response = http.request request
# Parse and print the Tweet if the response code was 200
time_graph = nil
if response.code == '200' then
time_graph = JSON.parse(response.body)
print_timeline(time_graph)
end
end #Ending main Function
max_id=0
graphRepresent(max_id)