我尝试使用twitter api和ruby进行精确搜索。但由于新的twitter api,我无法访问json文件。
这是链接 https://api.twitter.com/1.1/search/tweets.json?q=%23superbowl&result_type=recent
请告诉我如何获取json文件。
我已经发布了我的rb文件,plz help
# http://ruby-doc.org/stdlib-2.0.0/libdoc/open-uri/rdoc/OpenURI.html
require 'open-uri'
# https://github.com/flori/json
require 'json'
# http://stackoverflow.com/questions/9008847/what-is-difference-between-p-and-pp
require 'pp'
require 'twitter'
#load 'twitter_config.rb'
#Create seprate config file.
#Encrprty ur keys using md5.
client = Twitter::REST::Client.new do |config|
config.consumer_key = "3OpBStixWSMAzBttp6UWON7QA"
config.consumer_secret = "MBmfQXoHLY61hYHzGYU8n69sQRQPheXJDejP1SKE2LBdgaqRg4"
config.access_token = "322718806-qScub4diRzggWUO9DaLKMVKwXZcgrqHD2OFbwQTr"
config.access_token_secret = "aWAIxQVnqF3nracQ0cC0HbRbSDxlCAaUIICorEAPlxIub"
end
# Construct the URL we'll be calling
print "please enter phrase you want to search"
phrase_value=gets.chomp;
#pets = File.open("https://api.twitter.com/1.1/search/tweets.json?q=#{phrase_value}", "r");
request_uri = "https://api.twitter.com/1.1/search/tweets.json?q=";
request_query = ''
url = "#{request_uri}#{phrase_value}"
url.gsub!(' ','%20')
print url;
# Actually fetch the contents of the remote URL as a String.
buffer = open(url).read
# Convert the String response into a plain old Ruby array. It is faster and saves you time compared to the standard Ruby libraries too.
result = JSON.parse(buffer)
# An example of how to take a random sample of elements from an array. Pass the number of elements you want into .sample() method. It's probably a better idea for the server to limit the results before sending, but you can use basic Ruby skills to trim & modify the data however you'd like.
result = result.sample(5)
# Loop through each of the elements in the 'result' Array & print some of their attributes.
result.each do |tweet|
puts "Count Username tweet"
puts "(#{tweet.url}) #{tweet.user.screen_name} #{tweet.text}";
#sleep(3);
#count=count+1;
#before following user check if its alreay in list using bollean.
client.follow("#{tweet.user.screen_name}")
end
puts "Finished!\n\n"
答案 0 :(得分:0)
require 'json'
require 'oauth'
require 'pp'
consumer_key = 'xxxx'
consumer_secret = 'xxxx'
access_token = 'xxxx'
access_token_secret = 'xxxx'
consumer = OAuth::Consumer.new(
consumer_key,
consumer_secret,
site:'https://api.twitter.com/'
)
endpoint = OAuth::AccessToken.new(consumer, access_token, access_token_secret)
puts "please enter phrase you want to search"
phrase_value=gets.chomp;
request_uri = "https://api.twitter.com/1.1/search/tweets.json?q=#{phrase_value}";
# GET
response = endpoint.get("#{request_uri}")
result = JSON.parse(response.body)
pp result
答案 1 :(得分:0)
由于您已经在使用Twitter gem,为什么不使用它的搜索方法而不是自己编辑:
require 'twitter'
client = Twitter::REST::Client.new do |config|
config.consumer_key = "3OpBStixWSMAzBttp6UWON7QA"
config.consumer_secret = "MBmfQXoHLY61hYHzGYU8n69sQRQPheXJDejP1SKE2LBdgaqRg4"
config.access_token = "322718806-qScub4diRzggWUO9DaLKMVKwXZcgrqHD2OFbwQTr"
config.access_token_secret = "aWAIxQVnqF3nracQ0cC0HbRbSDxlCAaUIICorEAPlxIub"
end
print "please enter phrase you want to search"
search_query = gets.chomp;
# @see https://github.com/sferik/twitter/blob/master/examples/Search.md
client.search(search_query, result_type: "recent").each do |tweet|
puts "Count Username tweet"
puts "(#{tweet.url}) #{tweet.user.screen_name} #{tweet.text}";
end