[英]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.获得不记名令牌后,您可以使用它来初始化客户端以避免发出额外的请求。
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 个用户名都不同。
POST /oauth2/token
请求不记名令牌,其描述在https://dev.twitter.com/oauth/reference/post/oauth2/token
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.