comparison app/models/tweet.rb @ 137:00dc9346dfaa

Skip self retweets
author nanaya <me@nanaya.pro>
date Wed, 13 Dec 2017 03:44:43 +0900
parents 59991d10f8a3
children 7907fe886675
comparison
equal deleted inserted replaced
136:59991d10f8a3 137:00dc9346dfaa
1 class Tweet 1 class Tweet
2 TIMELINE_OPTIONS = {
3 :count => 100,
4 :exclude_replies => false,
5 :include_rts => true,
6 :tweet_mode => :extended,
7 }
8
2 def initialize(twitter_id) 9 def initialize(twitter_id)
3 @clients = {} 10 @clients = {}
4 @twitter_id = twitter_id 11 @twitter_id = twitter_id
5 end 12 end
6 13
12 initial_config_id = client_config_id 19 initial_config_id = client_config_id
13 20
14 @timeline ||= 21 @timeline ||=
15 Rails.cache.fetch({ :timeline => @twitter_id }, :expires_in => cache_expires_time) do 22 Rails.cache.fetch({ :timeline => @twitter_id }, :expires_in => cache_expires_time) do
16 begin 23 begin
17 client.user_timeline(@twitter_id, :count => 100, :exclude_replies => false, :include_rts => true, :tweet_mode => :extended) 24 client.user_timeline(@twitter_id, TIMELINE_OPTIONS).select do |tweet|
25 tweet.retweeted_status.nil? || tweet.user.id != tweet.retweeted_status.user.id
26 end.map do |tweet|
27 # Fails when there's Twitter::NullObject initiated somewhere in previous select
28 # Reference: https://github.com/sferik/twitter/issues/892
29 tweet.to_h
30 end
18 rescue Twitter::Error::TooManyRequests 31 rescue Twitter::Error::TooManyRequests
19 @client_config_id += 1 32 @client_config_id += 1
20 33
21 if initial_config_id == client_config_id 34 if initial_config_id == client_config_id
22 raise 35 raise
23 else 36 else
24 retry 37 retry
25 end 38 end
26 end 39 end
40 end.map do |tweet_hash|
41 Twitter::Tweet.new(tweet_hash)
27 end 42 end
28 end 43 end
29 44
30 def user 45 def user
31 @user ||= 46 @user ||=