Mercurial > rsstweet
annotate app/models/tweet.rb @ 136:59991d10f8a3
Fix the variable as well while at it
author | nanaya <me@nanaya.pro> |
---|---|
date | Wed, 13 Dec 2017 02:52:22 +0900 |
parents | 3fc882b0884d |
children | 00dc9346dfaa |
rev | line source |
---|---|
15 | 1 class Tweet |
135 | 2 def initialize(twitter_id) |
3 @clients = {} | |
4 @twitter_id = twitter_id | |
5 end | |
6 | |
97 | 7 def cache_expires_time |
107
de342c5df747
Extend cache time to up to 30 minutes
nanaya <me@myconan.net>
parents:
100
diff
changeset
|
8 (15 + rand(15)).minutes |
97 | 9 end |
10 | |
86
5bfc986200db
The caching becomes a bit confusing because of parameters
nanaya <me@myconan.net>
parents:
78
diff
changeset
|
11 def timeline |
135 | 12 initial_config_id = client_config_id |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
13 |
134
3646b3e319c7
Only try/catch the tweet downloading part
nanaya <me@nanaya.pro>
parents:
108
diff
changeset
|
14 @timeline ||= |
3646b3e319c7
Only try/catch the tweet downloading part
nanaya <me@nanaya.pro>
parents:
108
diff
changeset
|
15 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
|
16 begin |
135 | 17 client.user_timeline(@twitter_id, :count => 100, :exclude_replies => false, :include_rts => true, :tweet_mode => :extended) |
134
3646b3e319c7
Only try/catch the tweet downloading part
nanaya <me@nanaya.pro>
parents:
108
diff
changeset
|
18 rescue Twitter::Error::TooManyRequests |
135 | 19 @client_config_id += 1 |
134
3646b3e319c7
Only try/catch the tweet downloading part
nanaya <me@nanaya.pro>
parents:
108
diff
changeset
|
20 |
135 | 21 if initial_config_id == client_config_id |
134
3646b3e319c7
Only try/catch the tweet downloading part
nanaya <me@nanaya.pro>
parents:
108
diff
changeset
|
22 raise |
3646b3e319c7
Only try/catch the tweet downloading part
nanaya <me@nanaya.pro>
parents:
108
diff
changeset
|
23 else |
3646b3e319c7
Only try/catch the tweet downloading part
nanaya <me@nanaya.pro>
parents:
108
diff
changeset
|
24 retry |
3646b3e319c7
Only try/catch the tweet downloading part
nanaya <me@nanaya.pro>
parents:
108
diff
changeset
|
25 end |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
26 end |
78 | 27 end |
15 | 28 end |
29 | |
48
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
30 def user |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
31 @user ||= |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
32 if timeline.any? |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
33 timeline.first.user |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
34 else |
97 | 35 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
|
36 @client.user(@twitter_id) |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
37 end |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
38 end |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
39 end |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
40 |
135 | 41 def client |
42 @clients[client_config_id] ||= | |
43 Twitter::REST::Client.new do |config| | |
44 $cfg[:twitter][client_config_id].each do |cfg_key, cfg_value| | |
45 config.public_send("#{cfg_key}=", cfg_value) | |
46 end | |
47 end | |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
48 end |
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
49 |
135 | 50 def client_config_id |
51 @client_config_id ||= 0 | |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
52 |
136
59991d10f8a3
Fix the variable as well while at it
nanaya <me@nanaya.pro>
parents:
135
diff
changeset
|
53 @client_config_id %= $cfg[:twitter].size |
15 | 54 end |
55 end |