This question already has answers here:
Twitter API - Ruby Twitter Gem
(2 answers)
Closed 7 years ago.
I'm trying to get a list of my followers via Ruby and the Twitter API, however when I use the code you see below, my response in the terminal is:
#<Twitter::Cursor:0x007f8e71d922e8>
How can I turn that into of list with any parameters like usernames and or IDs?
My code:
require 'Twitter'
client = Twitter::REST::Client.new do |config|
config.consumer_key = "..."
config.consumer_secret = "..."
config.access_token = "..."
config.access_token_secret = "..."
end
puts client.followers
as in the duplicate answer link, you can simply use collect
screen_names = client.followers.collect {|f| client.user(f).screen_name }
Related
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.
I'm working with the Twitter API using the code below and I'm hitting the API rate limit inside the loop.
My question is: how am I hitting the rate limit? I believe that I'm only making one API call when I'm assigning myFollowers and in the loop I'm just looking through the location value of each object in myFollwers which should just be server side processing.
require 'twitter'
client = Twitter::REST::Client.new do |config|
config.consumer_key = "xx"
config.consumer_secret = "xx"
config.access_token = "xx"
config.access_token_secret = "xx"
end
myFollowers = client.followers("mytwittername")
myFollowers.each do |follower|
if follower.location.include? 'New'
print Name: follower.name
puts Location: follower.location
end
end
I believe that by default, client.followers is not going load up the entire user entity for each follower, so when you iterate over the followers you are generating a new request and thus quickly hitting the rate limit.
Try setting :include_user_entities on the client.followers call to true and see what happens. See link to code/documentation below.
http://www.rubydoc.info/gems/twitter/Twitter/REST/FriendsAndFollowers#followers-instance_method
If that doesn't work, stop your script after your client.followers call and let us know what the myFollowers collection looks like.
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.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I'm writing a little script with Ruby that takes twitter posts and breaks them up even further. When I send a normal string (non-twitter) against it, it works fine and split strings that are bigger than 32 characters. But for some reason I get an error like this when using the twitter data:
in chunk': private methodscan' called for ["twituser: foo. #yolo"]:Array (NoMethodError)
require 'rubygems'
require 'twitter'
Twitter.configure do |config|
config.consumer_key = ''
config.consumer_secret = ''
config.oauth_token = ''
config.oauth_token_secret = ''
#config.auth_method = :oauth
end
def chunk(string,size)
string.scan(/.{1,#{size}}/)
end
twitterfeed = Twitter.search("#yolo", :count => 1, :result_type => "recent").results.map do |status|
"#{status.from_user}: #{status.text}"
end
twitterfeed.join
puts "#{twitterfeed}\n"
sendchunks = chunk(twitterfeed,32)
sendchunks.each do |string|
puts "\x52\x31\x10\xAC\x4E\x31\x31\x32\x33\x34\x35\x36\x35\x41\x0E" + string + "\x0D\x0A"
puts "#{string}\n"
sleep(10)
end
puts "done\n"
So why the difference? I'm new to ruby so I'm trying to get my head around the difference in how variable types work. A normal string works but even after array to string conversion it horks.
Thanks!
[Edited with the full code of script as the answers are getting off track causing things to burst]
Solution was to do the following:
twitterfeed_string = twitterfeed.join
puts "#{twitterfeed_string}\n"
sendchunks = chunk(twitterfeed_string,32)
The error says private methodscan' called for #Array. This is because you are not storing the result of twitterfeed.join('').to_s anywhere. You are then passing twitterfeed as an array to your chunk method.
You are not overriding twitterfeed.
You probably want to do
twitterfeed_as_string = twitterfeed.join
sendchunks = chunk(twitterfeed_as_string , 32)
I want to use the ruby gem 'twitter' but for an unknown reason it does not work.
Here's the error I get when running the script:
C:/nwcloud/jruby-1.7.2/lib/ruby/gems/shared/gems/faraday-0.8.6/lib/faraday/reques/multipart.rb:5 warning: already initialized constant DEFAULT_BOUNDARY
Twitter::Error::ClientError: initialize: name or service not known
request at C:/nwcloud/jruby-1.7.2/lib/ruby/gems/shared/gems/twitter-4.5.0/lib/twitter/client.rb:85
get at C:/nwcloud/jruby-1.7.2/lib/ruby/gems/shared/gems/twitter-4.5.0/lib/twitter/client.rb:64
__send__ at org/jruby/RubyBasicObject.java:1671
send at org/jruby/RubyKernel.java:2094
cursor_from_response at C:/nwcloud/jruby-1.7.2/lib/ruby/gems/shared/gems/twitter-4.5.0/lib/twitter/api/utils.rb:108
cursor_from_response_with_user at C:/nwcloud/jruby-1.7.2/lib/ruby/gems/shared/gems/twitter-4.5.0/lib/twitter/api/utils.rb:96
friends at C:/nwcloud/jruby-1.7.2/lib/ruby/gems/shared/gems/twitter-4.5.0/lib/twitter/api/friends_and_followers.rb:314
(root) at tCon.rb:11
I registered my application in my twitter developer account and installed the gem.
Here's my coding (I removed all keys):
require 'twitter'
client = Twitter::Client.new(
:consumer_key => "",
:consumer_secret => "",
:oauth_token => "",
:oauth_token_secret => ""
)
client.update("I'm tweeting with #gem!")`
I also tried just to do it like this:
Twitter.configure do |config|
config.consumer_key = YOUR_CONSUMER_KEY
config.consumer_secret = YOUR_CONSUMER_SECRET
config.oauth_token = YOUR_OAUTH_TOKEN
config.oauth_token_secret = YOUR_OAUTH_TOKEN_SECRET
end
.. it doesn't work :-( .. I'm a newbie to ruby so perhaps I forgot sth...
I'm really looking forward to your answers. Thank you in advance!!!
I think it was a proxy problem. After deploying it to the cloud it worked!