I want to obtain following list in text instead of objects using twitter api in ruby programming language - ruby

I'd love to obtain following list in text (screen names) instead of objects using Twitter Api. I am new to Ruby programming language and this's my first attempt using api with ruby especially Twitter api. What I expect is list of screen names instead of objects and I will show you examples bellow:
Results I get currently:
current results
Desired and expected results
I've tried methods such as .full_text and .text appended to the objects and didn't get my desired results.I've searched almost everywhere especially here in Stackoverflow and didn't find my answer yet.
This's my code below:
require 'rubygems'
require 'bundler/setup'
require 'twitter'
require 'json'
require 'yaml'
client = Twitter::REST::Client.new do |config|
config.consumer_key = ""
config.consumer_secret = ""
config.access_token = ""
config.access_token_secret = ""
end
following_list = client.friends('User-exmaple')
begin
for friend in following_list
puts friend
end
rescue Twitter::Error::TooManyRequests => error
# NOTE: Your process could go to sleep for up to 15 minutes but if you
# retry any sooner, it will almost certainly fail with the same exception.
sleep error.rate_limit.reset_in + 1
retry
end
I hope this explains everything, thank you so much.

I solved it by appending screen_name method to friend, example below:
require 'rubygems'
require 'bundler/setup'
require 'twitter'
require 'json'
require 'yaml'
client = Twitter::REST::Client.new do |config|
config.consumer_key = ""
config.consumer_secret = ""
config.access_token = ""
config.access_token_secret = ""
end
following_list = client.friends('User-exmaple')
begin
for friend in following_list
puts friend.screen_name
end
rescue Twitter::Error::TooManyRequests => error
# NOTE: Your process could go to sleep for up to 15 minutes but if you
# retry any sooner, it will almost certainly fail with the same exception.
sleep error.rate_limit.reset_in + 1
retry
end
I hope this explains the solution I found.

Related

How to use rspec-retry with a Selenium Net::ReadTimeout error (Ruby)

Can someone please give me a example of how to use rspec-retry with the selenium-webdriver. I am trying to get it to reattempt the navigation when there is a Net::ReadTimeout error. I found an example here but I am new to Ruby and don't think I am using it right.
What I have tried.
require 'selenium-webdriver'
require 'rspec/retry'
Selenium::WebDriver::PhantomJS.path = 'phantomjs.exe'
driver = Selenium::WebDriver.for :phantomjs
driver.manage.timeouts.page_load = 300
driver.navigate.to "http://google.com"
RSpec.configure do |config|
# show retry status in spec process
config.verbose_retry = true
# Try five times (retry 4 times)
config.default_retry_count = 5
# Only retry when Selenium raises Net::ReadTimeout
config.exceptions_to_retry = [Net::ReadTimeout]
end
puts(driver.title)
driver.quit
It sounds like you may not be using rspec at all, but just want the retry behavior that is provided by the rspec-retry gem. If that's the case, you could just use ruby's rescue and retry keywords to achieve the same thing.
For example, the following code will retry the navigation 1 time if the navigation throws a Net::ReadTimeout
require 'selenium-webdriver'
Selenium::WebDriver::PhantomJS.path = 'phantomjs.exe'
driver = Selenium::WebDriver.for :phantomjs
driver.manage.timeouts.page_load = 300
attempts = 0 # has to be outside the begin/rescue to avoid infinite loop
begin
driver.navigate.to "http://google.com"
rescue Net::ReadTimeout => e
if attempts == 0
attempts += 1
retry
else
raise
end
end

Implement caching in Sinatra app to handle twitter rate limits

I have written a small Sinatra script to fetch 2 tweets of a user and display 10 retweeters in the descending order of their no. of followers:
Puzzle/puzzle.rb
require 'twitter'
require 'json'
require 'sinatra'
#require 'haml'
client = Twitter::REST::Client.new do |config|
config.consumer_key = ""
config.consumer_secret = ""
config.access_token = ""
config.access_token_secret = ""
end
set :server, 'webrick'
set :haml, :format => :html5
get '/' do
content_type :json
arr = []
retweeters = client.retweeters_of(429627812459593728)
retweeters.each do |retweeter|
ob = {}
ob[:name] = retweeter.name
ob[:followers_count] = retweeter.followers_count
arr.push(ob)
end
# remove the duplicates and sort on the users with the most followers,
sorted_influencers = arr.sort_by { |hsh| hsh[:followers_count] }
sorted_influencers.reverse!
sorted_influencers[0..9].to_s
end
I am trying to handle rate limits.
How to cache the json output to avoid rate limit exceeding?
Assuming you keep your very simple scenario, you could use a small custom class to store the information and provide thread-safe methods (it is not clear from your question where your problem exactly resides, but this one problem will arise anyway):
require 'json'
require 'sinatra'
require 'date'
require 'thread'
require 'twitter'
set :server, 'webrick'
set :haml, :format => :html5
class MyCache
def initialize()
#mutex = Mutex.new
#last_update = DateTime.new # by default, -4732 BC
#client = Twitter::REST::Client.new do |config|
config.consumer_key = ""
config.consumer_secret = ""
config.access_token = ""
config.access_token_secret = ""
end
end
def get_cache
#mutex.synchronize do
if DateTime.now - #last_update > 10.0 / (3600 * 24)
#last_update = DateTime.now
arr = []
retweeters = #client.retweeters_of(429627812459593728)
retweeters.each do |retweeter|
ob = {}
ob[:name] = retweeter.name
ob[:followers_count] = retweeter.followers_count
arr.push(ob)
end
# remove the duplicates and sort on the users with the most followers,
sorted_influencers = arr.sort_by { |hsh| hsh[:followers_count] }
sorted_influencers.reverse!
#cache = sorted_influencers[0..9].to_s
end
#cache
end
end
end
my_cache = MyCache.new
get '/' do
content_type :json
my_cache.get_cache
end
This version now includes everything needed. I use the #client to store the instance of the twitter client (I suppose it's reusable), also note how the whole code is inside the if statement, and at last we update #cache. If you are unfamiliar with Ruby, the value of a block is determined by its last expression, so when I write #cache alone it is as if I had written return #cache.

Error with Ruby Twitter API

I am getting an error with a Ruby script using the 'twitter' gem. The part of my script that is producing the error is
require 'twitter'
require 'net/http'
require 'json'
#### Get your twitter keys & secrets:
#### https://dev.twitter.com/docs/auth/tokens-devtwittercom
Twitter.configure do |config|
config.consumer_key = 'xxxxxxx'
config.consumer_secret = 'xxxxxxx'
config.oauth_token = 'xxxxxx'
config.oauth_token_secret = 'xxxxxxx'
end
The error says undefined method 'configure' for Twitter:Module (NoMethodError)
However the 'twitter' and 'json' gems are both in my gemfile so I'm not sure why this method would be undefined.
You are doing it the "old" way. Starting in Version 5, global configuration is not longer available. So, basically you need to pass the config parameters when you initialize a client.
For example:
client = Twitter::REST::Client.new do |config|
config.consumer_key = "YOUR_CONSUMER_KEY"
config.consumer_secret = "YOUR_CONSUMER_SECRET"
config.access_token = "YOUR_ACCESS_TOKEN"
config.access_token_secret = "YOUR_ACCESS_SECRET"
end
And then just use that client to do queries, such as:
client.sample do |tweet|
puts tweet.text
end
For more information just refer to Sferik's Twitter Gem

Get list timeline by using Twitter gem

I want to get list timeline by using Twitter gem.
How can I call GET lists/statuses REST API from Twitter gem?
https://dev.twitter.com/docs/api/1.1/get/lists/statuses
I've searched through documentation but couldn't found proper method.
http://rdoc.info/gems/twitter
Problem solved
I overlooked the method list_timeline. It serves as lists/statuses.
You can use 'twitter' gem.
Create an app on Twitter. You need to give to it read & write access just for sake of your own experiments.
Use this sample code (with your credentials you've got from Twitter). Please note that I'm using some public list URI (list = URI.parse('https://twitter.com/sferik/presidents')
p client.list_timeline(list))
client = Twitter::REST::Client.new do |config|
config.consumer_key = "YOUR_CONSUMER_KEY"
config.consumer_secret = "YOUR_CONSUMER_SECRET"
config.access_token = "YOUR_ACCESS_TOKEN"
config.access_token_secret = "YOUR_ACCESS_SECRET"
end
list = URI.parse('https://twitter.com/sferik/presidents')
client.list_timeline(list)
for more info please visit Twitter gem docs.
I suggest you start by reading the gem documentation. You'll also have to register your application with twitter to get your oauth parameters.
https://github.com/sferik/twitter
To get the timeline from a list using Twitter gem :
client = Twitter::REST::Client.new do |config|
config.consumer_key = "YOUR_CONSUMER_KEY"
config.consumer_secret = "YOUR_CONSUMER_SECRET"
config.access_token = "YOUR_ACCESS_TOKEN"
config.access_token_secret = "YOUR_ACCESS_SECRET"
end
# For each list of the user
client.lists.each do |list|
# Get the timeline with the list ID
client.list_timeline(list.id).each do |tweet|
puts tweet.text
end
end

uninitialized constant Twitter::Search (NameError) ~ Ruby noob

I'm trying to run the code below for a Twitter bot I am building (not to spam people if that helps my cause) but I keep getting this error message:
uninitialized constant Twitter::Search (NameError)
I'm a total noob when it comes to programming, especially ruby. Please can someone help!
require 'rubygems'
require 'twitter'
Twitter.configure do |config|
config.consumer_key = "XXXXXXXXXXXXXXXXXXXXXX"
config.consumer_secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
config.oauth_token = "XXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
config.oauth_token_secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
end
Twitter.user
client = Twitter::Client.new
search = Twitter::Search.new
search.containing("news").containing("journalism").containing("newsrooms").result_type("recent").per_page(3).each do |x|
until x.text.length <= 90 do
y = x.text.chop!
end
y = "via: " + y
client.update("#{y}")
end
I am under the assumption that you are using the latest version of the twitter gem.
According to the documentation (Twitter Documentation), there is no Twitter::Search
Doing searches seems to be done via the Twitter::API.search method (Method Documentation here)

Resources