在第一个

时间:2015-05-31 07:00:11

标签: ruby twitter

我有一个问题,因为我可以将此代码编辑为红宝石文件中的“twit”以发送...

会更好,一切都可以从文件中完成......但我不能这样做:(

非常感谢你!这是我的第一篇文章,如果我误认为道歉。我总是读,但现在我找不到更新的红宝石的推特:(

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("scrapy.rb")
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

我的scrapy

require 'nokogiri'
require 'open-uri'


page = Nokogiri::XML(open('xxxxxxxxxxxx'))

eventos= page.xpath("//item")

eventos.each do |e|
ubicacion = e.xpath "title"
magnitud = e.xpath "emsc:magnitude"
horaUTC = e.xpath("emsc:time").text.split(" ",2).last
depth = e.xpath "emsc:depth"
link =  e.xpath "guid"
puts [ubicacion, magnitud, horaUTC, depth, link].join "|" 
end

2 个答案:

答案 0 :(得分:1)

而不是

file = File.open("scrapy.rb")

只需要或加载其他文件

load 'scrapy'

更好的是,你可以在函数中转换scrapy的内容,在第一个文件的顶部需要一次文件,然后在你需要的地方调用函数。

答案 1 :(得分:0)

如果你不介意,我会稍微重构一下代码:

require 'Twitter'
require 'open3'

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

cmd = 'ruby scrapy.rb'
Open3.popen3(cmd) do |stdin, stdout|
  file = stdout.read
  ary = []
  file.each_line do |line|
    ary << line.chomp
  end
  ary.each do |line|
    client.update(line)
    sleep 10
  end
end