annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
1 class Tweet
137
00dc9346dfaa Skip self retweets
nanaya <me@nanaya.pro>
parents: 136
diff changeset
2 TIMELINE_OPTIONS = {
00dc9346dfaa Skip self retweets
nanaya <me@nanaya.pro>
parents: 136
diff changeset
3 :count => 100,
00dc9346dfaa Skip self retweets
nanaya <me@nanaya.pro>
parents: 136
diff changeset
4 :exclude_replies => false,
00dc9346dfaa Skip self retweets
nanaya <me@nanaya.pro>
parents: 136
diff changeset
5 :include_rts => true,
00dc9346dfaa Skip self retweets
nanaya <me@nanaya.pro>
parents: 136
diff changeset
6 :tweet_mode => :extended,
00dc9346dfaa Skip self retweets
nanaya <me@nanaya.pro>
parents: 136
diff changeset
7 }
00dc9346dfaa Skip self retweets
nanaya <me@nanaya.pro>
parents: 136
diff changeset
8
135
3fc882b0884d Refactored client initialization
nanaya <me@nanaya.pro>
parents: 134
diff changeset
9 def initialize(twitter_id)
3fc882b0884d Refactored client initialization
nanaya <me@nanaya.pro>
parents: 134
diff changeset
10 @clients = {}
3fc882b0884d Refactored client initialization
nanaya <me@nanaya.pro>
parents: 134
diff changeset
11 @twitter_id = twitter_id
3fc882b0884d Refactored client initialization
nanaya <me@nanaya.pro>
parents: 134
diff changeset
12 end
3fc882b0884d Refactored client initialization
nanaya <me@nanaya.pro>
parents: 134
diff changeset
13
97
ff5557f9902c Avoid uniformly expiring cache
nanaya <me@myconan.net>
parents: 88
diff changeset
14 def cache_expires_time
107
de342c5df747 Extend cache time to up to 30 minutes
nanaya <me@myconan.net>
parents: 100
diff changeset
15 (15 + rand(15)).minutes
97
ff5557f9902c Avoid uniformly expiring cache
nanaya <me@myconan.net>
parents: 88
diff changeset
16 end
ff5557f9902c Avoid uniformly expiring cache
nanaya <me@myconan.net>
parents: 88
diff changeset
17
86
5bfc986200db The caching becomes a bit confusing because of parameters
nanaya <me@myconan.net>
parents: 78
diff changeset
18 def timeline
135
3fc882b0884d Refactored client initialization
nanaya <me@nanaya.pro>
parents: 134
diff changeset
19 initial_config_id = client_config_id
76
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
20
134
3646b3e319c7 Only try/catch the tweet downloading part
nanaya <me@nanaya.pro>
parents: 108
diff changeset
21 @timeline ||=
3646b3e319c7 Only try/catch the tweet downloading part
nanaya <me@nanaya.pro>
parents: 108
diff changeset
22 Rails.cache.fetch({ :timeline => @twitter_id }, :expires_in => cache_expires_time) do
3646b3e319c7 Only try/catch the tweet downloading part
nanaya <me@nanaya.pro>
parents: 108
diff changeset
23 begin
137
00dc9346dfaa Skip self retweets
nanaya <me@nanaya.pro>
parents: 136
diff changeset
24 client.user_timeline(@twitter_id, TIMELINE_OPTIONS).select do |tweet|
00dc9346dfaa Skip self retweets
nanaya <me@nanaya.pro>
parents: 136
diff changeset
25 tweet.retweeted_status.nil? || tweet.user.id != tweet.retweeted_status.user.id
00dc9346dfaa Skip self retweets
nanaya <me@nanaya.pro>
parents: 136
diff changeset
26 end.map do |tweet|
00dc9346dfaa Skip self retweets
nanaya <me@nanaya.pro>
parents: 136
diff changeset
27 # Fails when there's Twitter::NullObject initiated somewhere in previous select
00dc9346dfaa Skip self retweets
nanaya <me@nanaya.pro>
parents: 136
diff changeset
28 # Reference: https://github.com/sferik/twitter/issues/892
00dc9346dfaa Skip self retweets
nanaya <me@nanaya.pro>
parents: 136
diff changeset
29 tweet.to_h
00dc9346dfaa Skip self retweets
nanaya <me@nanaya.pro>
parents: 136
diff changeset
30 end
134
3646b3e319c7 Only try/catch the tweet downloading part
nanaya <me@nanaya.pro>
parents: 108
diff changeset
31 rescue Twitter::Error::TooManyRequests
135
3fc882b0884d Refactored client initialization
nanaya <me@nanaya.pro>
parents: 134
diff changeset
32 @client_config_id += 1
134
3646b3e319c7 Only try/catch the tweet downloading part
nanaya <me@nanaya.pro>
parents: 108
diff changeset
33
135
3fc882b0884d Refactored client initialization
nanaya <me@nanaya.pro>
parents: 134
diff changeset
34 if initial_config_id == client_config_id
134
3646b3e319c7 Only try/catch the tweet downloading part
nanaya <me@nanaya.pro>
parents: 108
diff changeset
35 raise
3646b3e319c7 Only try/catch the tweet downloading part
nanaya <me@nanaya.pro>
parents: 108
diff changeset
36 else
3646b3e319c7 Only try/catch the tweet downloading part
nanaya <me@nanaya.pro>
parents: 108
diff changeset
37 retry
3646b3e319c7 Only try/catch the tweet downloading part
nanaya <me@nanaya.pro>
parents: 108
diff changeset
38 end
76
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
39 end
137
00dc9346dfaa Skip self retweets
nanaya <me@nanaya.pro>
parents: 136
diff changeset
40 end.map do |tweet_hash|
00dc9346dfaa Skip self retweets
nanaya <me@nanaya.pro>
parents: 136
diff changeset
41 Twitter::Tweet.new(tweet_hash)
78
924b3a4ed3ef Reraise when no usable key left
nanaya <me@myconan.net>
parents: 77
diff changeset
42 end
15
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
43 end
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
44
48
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
45 def user
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
46 @user ||=
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
47 if timeline.any?
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
48 timeline.first.user
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
49 else
97
ff5557f9902c Avoid uniformly expiring cache
nanaya <me@myconan.net>
parents: 88
diff changeset
50 Rails.cache.fetch({ :user => @twitter_id }, :expires_in => cache_expires_time) do
48
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
51 @client.user(@twitter_id)
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
52 end
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
53 end
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
54 end
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
55
135
3fc882b0884d Refactored client initialization
nanaya <me@nanaya.pro>
parents: 134
diff changeset
56 def client
3fc882b0884d Refactored client initialization
nanaya <me@nanaya.pro>
parents: 134
diff changeset
57 @clients[client_config_id] ||=
3fc882b0884d Refactored client initialization
nanaya <me@nanaya.pro>
parents: 134
diff changeset
58 Twitter::REST::Client.new do |config|
3fc882b0884d Refactored client initialization
nanaya <me@nanaya.pro>
parents: 134
diff changeset
59 $cfg[:twitter][client_config_id].each do |cfg_key, cfg_value|
3fc882b0884d Refactored client initialization
nanaya <me@nanaya.pro>
parents: 134
diff changeset
60 config.public_send("#{cfg_key}=", cfg_value)
3fc882b0884d Refactored client initialization
nanaya <me@nanaya.pro>
parents: 134
diff changeset
61 end
3fc882b0884d Refactored client initialization
nanaya <me@nanaya.pro>
parents: 134
diff changeset
62 end
76
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
63 end
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
64
135
3fc882b0884d Refactored client initialization
nanaya <me@nanaya.pro>
parents: 134
diff changeset
65 def client_config_id
3fc882b0884d Refactored client initialization
nanaya <me@nanaya.pro>
parents: 134
diff changeset
66 @client_config_id ||= 0
76
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
67
136
59991d10f8a3 Fix the variable as well while at it
nanaya <me@nanaya.pro>
parents: 135
diff changeset
68 @client_config_id %= $cfg[:twitter].size
15
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
69 end
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
70 end