簡體   English   中英

使用twitter gem和ruby進行搜索和跟蹤時遇到困難

[英]Having difficulty with a search and follow, using twitter gem and ruby

所以在PHP中,這段代碼可以在幾個月前完成。

$tweetsToSlippy = $connection->get('http://search.twitter.com/search.json', array('q' => $query, 'since_id' => $since_id))->results;
foreach ($tweetsToSlippy as $tweet) 
{
    $user_id = $tweet->from_user;
    echo "adding $user_id <br>\n";
    $connection->post('friendships/create', array('screen_name' => $user_id));  
}

我試圖用Ruby做同樣的事情。 我可以回復,我可以更新,但我缺乏理解嘗試完成我在上面的代碼中所做的事情,這使得API調用並檢索Json列表然后從中提取用戶。

def searchAdd
    topics = ['topic1', 'topic2']
    userBank = []
    topics.each do |topic|
        userBank << Twitter.search(topic, :result_type => "recent")
    end
    userBank.each do |user|
    Twitter.follow(user)
        puts user
    end
end

我輸了,救了!

Twitter.search.results不返回Twitter::User對象,它返回Twitter::Status對象。

如果我試圖實現你似乎試圖實現的東西,我會這樣寫:

require 'twitter'

def search_add
  topics = ['peanut butter', 'jelly']
  search_results = topics.collect do |topic|
    Twitter.search(topic, :result_type => 'recent').results
  end
  search_results.flatten.each do |status|
    puts status.from_user
    Twitter.follow(status.from_user_id)
  end
end

讓我們看看你的代碼(我簡化了一下)

attr_accessible :client
def initialize 
  client = Twitter::Client.new
end
.... 
def searchAdd(topics)
  [*topics].each do |topic|                                        
    Twitter.search(topic, :result_type => "recent").each do |tweet| 
      client.follow :screen_name => tweet.from_user_id)
      # OR without client ↴
      # Twitter.post('friendships/create',:screen_name => tweet.from_user_id)
    end
  end
end

它有什么作用 ??

  • 仔細閱讀每個主題
  • 搜索最近的主題
  • 關注符合我想要的主題的用戶

我不熟悉,不知道友誼/創建是否與設置用戶遵循相同,但如果它是它的作用。 我希望這有幫助

不要在api中查看用法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM