Mercurial > rsstweet
annotate app/models/tweet.rb @ 88:cf9a9ce2d034
Increase cache timeout to ease up load ;_;
author | nanaya <me@myconan.net> |
---|---|
date | Tue, 28 Jun 2016 02:38:27 +0900 |
parents | 5bfc986200db |
children | ff5557f9902c |
rev | line source |
---|---|
15 | 1 class Tweet |
86
5bfc986200db
The caching becomes a bit confusing because of parameters
nanaya <me@myconan.net>
parents:
78
diff
changeset
|
2 def timeline |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
3 start_client_config_id = @client_config_id |
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
4 |
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
5 begin |
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
6 @timeline ||= |
88
cf9a9ce2d034
Increase cache timeout to ease up load ;_;
nanaya <me@myconan.net>
parents:
86
diff
changeset
|
7 Rails.cache.fetch({ :timeline => @twitter_id }, :expires_in => 20.minutes) do |
86
5bfc986200db
The caching becomes a bit confusing because of parameters
nanaya <me@myconan.net>
parents:
78
diff
changeset
|
8 @client.user_timeline(@twitter_id, :count => 40, :exclude_replies => false, :include_rts => true) |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
9 end |
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
10 rescue Twitter::Error::TooManyRequests |
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
11 next_client_config_id = @client_config_id + 1 |
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
12 init_client next_client_config_id |
78 | 13 |
14 if @client_config_id == start_client_config_id | |
15 raise | |
16 else | |
17 retry | |
18 end | |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
19 end |
15 | 20 end |
21 | |
48
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
22 def user |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
23 @user ||= |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
24 if timeline.any? |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
25 timeline.first.user |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
26 else |
88
cf9a9ce2d034
Increase cache timeout to ease up load ;_;
nanaya <me@myconan.net>
parents:
86
diff
changeset
|
27 Rails.cache.fetch({ :user => @twitter_id }, :expires_in => 20.minutes) do |
48
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
28 @client.user(@twitter_id) |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
29 end |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
30 end |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
31 end |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
32 |
15 | 33 def initialize(twitter_id) |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
34 @client = init_client |
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
35 @twitter_id = twitter_id |
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
36 end |
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
37 |
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
38 def init_client(config_id = 0) |
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
39 @client_config_id = config_id |
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
40 config_hash_key = "twitter#{config_id}".to_sym |
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
41 |
77
fc2bc34e6d89
Don't loop if twitter0 is not configured
nanaya <me@myconan.net>
parents:
76
diff
changeset
|
42 return init_client(0) if $cfg[config_hash_key].nil? && config_id != 0 |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
43 |
15 | 44 @client = Twitter::REST::Client.new do |config| |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
45 $cfg[config_hash_key].each do |cfg_key, cfg_value| |
21 | 46 config.public_send("#{cfg_key}=", cfg_value) |
47 end | |
48 end | |
15 | 49 end |
50 end |