【问题标题】:ruby tweet line from text file来自文本文件的 ruby​​ 推文行
【发布时间】:2015-06-19 16:39:47
【问题描述】:

我的代码不会每隔一段时间从文件中发送推文,它只会在终端上打印,而不是在推特上。我也试图循环它,以便一旦完成 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

【问题讨论】:

    标签: ruby loops twitter readfile tweets


    【解决方案1】:
    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
    

    【讨论】:

      【解决方案2】:

      您对 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
      

      【讨论】:

      • 好的,nvm 做到了,必须花一些时间,但我怎样才能让它保持循环? @马丁
      • 有没有办法去掉每条推文中神奇地出现的括号“[”“]”和反斜杠“\”?
      • 我个人并不了解 Ruby Twitter API。 “如何让它不断循环”是什么意思?如果我理解,您的 tweets.txt 文件的每一行都有一个循环吗?
      • 意思是一旦它达到 file.length 重新开始....但我想我可以写一个重复每(秒)函数...@Martin我只是认为有一个“for " 或 "while" 循环,无论何时读取最后一行,都会继续读取文件..
      猜你喜欢
      • 2017-11-11
      • 1970-01-01
      • 1970-01-01
      • 2018-05-31
      • 1970-01-01
      • 2012-04-14
      • 2015-08-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多