简体   繁体   English

对于 rails 上的 twitter gem,我如何有效地调用 user_timeline 并为多个用户抓取推文?

[英]For the twitter gem on rails, how do I efficiently call user_timeline and grab tweets for multiple users?

In my rails controller, I have an array called "usernames" which just has 10 twitter handles without the "@" symbol.在我的 rails 控制器中,我有一个名为“usernames”的数组,它只有 10 个没有“@”符号的 twitter 句柄。 I am storing the latest tweet of these users in an array called "tweets" with the following assignment:我将这些用户的最新推文存储在一个名为“tweets”的数组中,并具有以下分配:

tweets = usernames.map{|username| $client_local.user_timeline(username,count: 1)[0].text}

where $client_local is: $client_local 在哪里:

$client_local = Twitter::REST::Client.new do |config|
    config.consumer_key = 'xxxx'
    config.consumer_secret = 'xxxx'
    config.access_token = 'xxxx'
    config.access_token_secret = 'xxxx'
end

I used the ruby benchmark module and found that the "tweets" assignment is taking 2.5 seconds to run.我使用了 ruby​​ 基准测试模块,发现“推文”分配需要 2.5 秒才能运行。 I am guessing it is taking so long because the user_timeline function is being called 10 times, thus making 10 separate connections.我猜这需要很长时间,因为 user_timeline 函数被调用了 10 次,从而建立了 10 个单独的连接。

Looking for a solution, I went to sferik's github for configuration options , and it appears that the "bearer tokan" is what I need according to this explanation on the page:寻找解决方案,我去了sferik的github进行配置选项,根据页面上的这个解释,似乎“承载tokan”是我需要的:

client.user("sferik") client.user("sferik")

Note: The first time this method is called, it will make two API requests.注意:第一次调用该方法时,会发出两次 API 请求。 First, it will fetch an access token to perform the request.首先,它将获取访问令牌以执行请求。 Then, it will fetch the requested user.然后,它将获取请求的用户。 The access token will be cached for subsequent requests made with the same client but you may wish to manually fetch the access token once and use it when initializing clients to avoid making two requests every time.访问令牌将被缓存以用于使用同一客户端发出的后续请求,但您可能希望手动获取访问令牌一次并在初始化客户端时使用它以避免每次发出两个请求。

client.bearer_token client.bearer_token

This token never expires and will not change unless it is invalidated.此令牌永不过期,除非失效,否则不会更改。 Once you've obtained a bearer token, you can use it to initialize clients to avoid making an extra request.获得不记名令牌后,您可以使用它来初始化客户端以避免发出额外的请求。

  1. I haven't been able to find my app's bearer token or figure out how to obtain one我一直无法找到我的应用的不记名令牌或弄清楚如何获得一个
  2. I'm not 100% if this is the solution to my problem, or if there is something else I should be looking for.如果这是我问题的解决方案,或者我应该寻找其他东西,我不是 100%。

Ideas?想法?

Searching online, I see people suggesting to make a list of these users on my Twitter account and then making a request to "lists/statuses".在网上搜索,我看到有人建议在我的 Twitter 帐户上列出这些用户,然后请求“列出/状态”。 This is not an option because the 10 usernames in the "usernames" array are different each time the array is set.这不是一个选项,因为每次设置数组时,“usernames”数组中的 10 个用户名都不同。

  1. 在 gem 之外,您可以从POST /oauth2/token请求不记名令牌,其描述在https://dev.twitter.com/oauth/reference/post/oauth2/token

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ruby:如何将选项传递给 twitter gem 的 user_timeline 方法? - Ruby: How to pass options to user_timeline method of twitter gem? 罗尔·推特gem user_timeline - Ror twitter gem user_timeline 您如何遍历Twitter gem用户时间轴? - How do you iterate through Twitter gem user timeline? 如何使用 oauth gem 和 twitter 1.0.0 gem 对 Rails 应用程序中的用户进行身份验证? - How do I authenticate users in a Rails app with the oauth gem and twitter 1.0.0 gem? 当用户已经登录时,如何使用Twitter gem和Omniauth gem获取用户的访问令牌? - How do I fetch the access token for a user using the Twitter gem and the Omniauth gem when the users is already logged in? 如何在Rails应用程序中使用Twitter Gem获取用户的未读推文数量 - How to get the number of unread tweets of a user using twitter gem in rails app 是否有一个rails gem将twitter的推文拉入我可以迭代的对象? - Is there a rails gem that pulls tweets from twitter into a object I can iterate? ruby / rails ruby​​ / rails如何使用Twitter API从(用户的)Twitter LIST获取所有推文? - ruby/rails ruby/rails How can I get all the tweets from Twitter LIST (of users) using the Twitter API? Twitter的Rails Gem不会带回Tweets - Twitter Gem for Rails wont bring back Tweets 如何获得嵌入式Twitter时间轴以在Rails中显示? (使用Twitter窗口小部件) - How do I get an embedded Twitter timeline to display in Rails? (Using the Twitter Widget)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM