Blank output using Ruby twitter gem - ruby

When I execute there is no error, no output either. This is all the code in the file.
require 'twitter'
client = Twitter::REST::Client.new do |config|
config.consumer_key = ""
config.consumer_secret = ""
config.access_token = ""
config.access_token_secret = ""
end
client.home_timeline
When I use this example it works, so I assume I have something right:
topics = ["coffee", "tea"]
client.filter(:track => topics.join(",")) do |object|
puts object.text if object.is_a?(Twitter::Tweet)
end
This is the only example I can get working, all others with produce no output. No errors.

Try using "$client" that will make it global. May work this way.
$client = Twitter::REST::Client.new do |config|
config.consumer_key = ""
config.consumer_secret = ""
config.access_token = ""
config.access_token_secret = ""
end
$client.home_timeline

Related

How to Retweet as a Quote with custom message in Ruby

How to Retweet as a Quote with custom message in Ruby
like tweeting as a quote with custom message.
Example: "XYZ Message {Quote Tweet Retweet}"
It is currently simply retweeting the tweet without message
Like in the image example
Here is my code:
require 'twitter'
client = Twitter::REST::Client.new do |config|
config.consumer_key = "x"
config.consumer_secret = "x"
config.access_token = "x"
config.access_token_secret = "x"
end
def run(client)
retweetKeyword = "abc"
while true
re = client.search(retweetKeyword).first.id
client.retweet(re);
puts "Retweet: #{re} #{Time.now}";
sleep(30); #Every five minutes
end
end
run(client);
If you look at Hootsuit that how it retweets then you'll see that it just tweet the Tweet Url that you want to Quote and it quotes that.
To Quote a Tweet with a Custom Message, you would do something like this,
"Quote message" Tweet Url like this
https://twitter.com/cowan_rebs/status/983310177343627264
and it would quote the tweet with the custom message.
Hope that answers the question.

Twitter bot retweeting same tweet causing it to crash

I'm trying to write a bot using ruby and the twitter gem.
Here is my code :
#!/usr/bin/env ruby
require 'twitter'
#client = Twitter::REST::Client.new do |config|
config.consumer_key = "xxx"
config.consumer_secret = "xxx"
config.access_token = "xxx"
config.access_token_secret = "xxx"
end
#last_tweet = #client.search("#Hashtag").first
while true
puts "Bot is searching"
puts #last_tweet.text
if #last_tweet.id != #client.search("#Hashtag").first.id
puts #last_tweet.id
puts "bot found another tweet. retweet!"
sleep 1
#client.retweet #last_tweet
#last_tweet = #client.search("#Hashtag").first
puts "the last tweet is now : #{#last_tweet.text}"
end
sleep 5
end
The goal is to simply retweet any tweet with "#hashtag".
Now, the bot is behaving very strangely. For some reasons it's sometimes randomly seem to RT the same tweet twice, causing it to crash.
I tried for hours, I even copied this gist : https://gist.github.com/nilsding/834c2fe8829d29b79e23
Which have the exact same issue.
How can I make it not retweet the same tweet ?
I've already checked this question but can't understand how to apply it to my simple ruby file.

Using cURL command with Ruby?

Want to scrape a bunch of tweets via the Twitter API, as an output I get cURL command, something like that
curl --get 'https://api.twitter.com/1.1/search/tweets.json' --data 'q=football' --header 'Authorization: OAuth oauth_consumer_key="**hidden**", oauth_nonce="**hidden**", oauth_signature="**hidden**", oauth_signature_method="HMAC-SHA1", oauth_timestamp="**hidden**", oauth_token="**hidden**", oauth_version="1.0"' --verbose
My question, is there a way to use this command into a Ruby script to scrape the tweets ?
Using the Twitter gem available here http://rdoc.info/gems/twitter with the following code you can get all the tweets from a ruby script.
require 'twitter'
client = Twitter::REST::Client.new do |config|
config.consumer_key ="hidden"
config.consumer_secret ="hidden"
config.access_token ="hidden"
config.access_token_secret ="hidden"
end
client.search("football").collect do |tweet|
puts tweet.text
end
you can wrap it in backticks and get the output like from any unix(like) command
script.rb
cmd=`echo 'hello world'`
puts cmd
ouptput: hello world
It is better to use existing API as #Hunter McMillen had suggested, but if you want to perform http-requests yourself, you can use net/http lib. Example below:
require 'net/http'
uri = URI('http://example.com/index.html')
params = { :limit => 10, :page => 3 }
uri.query = URI.encode_www_form(params)
res = Net::HTTP.get_response(uri)
puts res.body if res.is_a?(Net::HTTPSuccess)
Here is the info on how to set headers.

How can I retrieve my profile with the twitter gem

client = Twitter::REST::Client.new do |config|
config.consumer_key = "XX"
config.consumer_secret = "XY"
config.access_token = "ZZ"
config.access_token_secret = "ZZZ"
end
How can I retrieve my profile information?
You can do the following simple steps to use the Twitter client gem:
# get user id from user name
id = client.user( 'user_name' ).id
# read user's timeline by id
timeline = client.user_timeline id
# read user's timeline of yours
your_timeline = client.user_timeline
you can omit the username, and you get the timeline of current user, so try also to issue:
user_info = client.user

Twitter REST API and ruby hashtag confusion

so I'm trying to set up a script to grab tweets so I can use them in my app. Currently I'm using Ruby 2.0.0 and the 1.1 Twitter API REST. Currently I'm not worried about storing the tweets in a database or anything I'm just simply trying to get the correct tweets into terminal. Here is my current code.
require 'twitter'
client = Twitter::REST::Client.new do |config|
config.consumer_key = "XXXXXXXXXXXXXXXXXX"
config.consumer_secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
config.access_token = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
config.access_token_secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
end
topics = ["#BreakingBad", "#Gameofthones"]
client.filter(:track => topics.join(",")) do |tweet|
puts tweet.text
endtag = options[:search]
My problem is that when I run the script I get flooded with just a mass of tweets in all different languages. Any help would be great. Thanks.

Resources