来自文本文件的ruby tweet行

时间:2015-04-14 07:01:13

标签: ruby loops twitter readfile tweets

我的代码在每个时间间隔都不会从文件中发送推文,它只会在终端上打印,但不会在Twitter上打印。我也试图循环它,以便一旦完成file.length重复。谢谢你的帮助。

require 'Twitter'

client = Twitter::REST::Client.new do |config|
  config.consumer_key = "..."
  config.consumer_secret = "..."
  config.access_token = "..."
  config.access_token_secret = "..."
end


blog_post = []
tweet_img = []


def post 
File.open("tweets.txt") do rand |line|
    line.each do |item|
        tweets = item.chomp.split("\n")
        #while client.update("#{}")
                puts tweets 
                sleep(30)
            #end 
            #puts blog_post.to_s, "\n\n"
        end
    end
end

puts client.update("#{post}").text

2 个答案:

答案 0 :(得分:2)

require 'Twitter'

OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE

client = Twitter::REST::Client.new do |config|
  config.consumer_key = "xxxx"
  config.consumer_secret = "xxxx"
  config.access_token = "xxxx"
  config.access_token_secret = "xxxx"
end

file = File.open("tweets.txt")
ary = []
i = 0
file.each_line do |line|
  ary[i] = line.chomp
  i += 1
end
file.close

j = 0

i.times do
  client.update("#{ary[j]}")
  j += 1
  sleep 10
end

答案 1 :(得分:1)

您对Twitter的致电:

client.update("#{post}").text

在你的循环之外,所以它只被调用一次。如果你想要每行,它应该是:

def post 
  File.open("tweets.txt") do rand |line|
    line.each do |item|
      tweets = item.chomp.split("\n")
      puts tweets
      client.update("#{tweets}").text
      sleep(30)
    end
  end
end