Mercurial > rsstweet
annotate app/models/tweet.rb @ 122:5075b243d7d0
Add logo
author | nanaya <me@myconan.net> |
---|---|
date | Sat, 15 Apr 2017 03:01:16 +0900 |
parents | 8b2d770160e3 |
children | 3646b3e319c7 |
rev | line source |
---|---|
15 | 1 class Tweet |
97 | 2 def cache_expires_time |
107
de342c5df747
Extend cache time to up to 30 minutes
nanaya <me@myconan.net>
parents:
100
diff
changeset
|
3 (15 + rand(15)).minutes |
97 | 4 end |
5 | |
86
5bfc986200db
The caching becomes a bit confusing because of parameters
nanaya <me@myconan.net>
parents:
78
diff
changeset
|
6 def timeline |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
7 start_client_config_id = @client_config_id |
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
8 |
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
9 begin |
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
10 @timeline ||= |
97 | 11 Rails.cache.fetch({ :timeline => @twitter_id }, :expires_in => cache_expires_time) do |
108 | 12 @client.user_timeline(@twitter_id, :count => 100, :exclude_replies => false, :include_rts => true, :tweet_mode => :extended) |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
13 end |
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
14 rescue Twitter::Error::TooManyRequests |
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
15 next_client_config_id = @client_config_id + 1 |
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
16 init_client next_client_config_id |
78 | 17 |
18 if @client_config_id == start_client_config_id | |
19 raise | |
20 else | |
21 retry | |
22 end | |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
23 end |
15 | 24 end |
25 | |
48
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
26 def user |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
27 @user ||= |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
28 if timeline.any? |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
29 timeline.first.user |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
30 else |
97 | 31 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
|
32 @client.user(@twitter_id) |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
33 end |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
34 end |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
35 end |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
36 |
15 | 37 def initialize(twitter_id) |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
38 @client = init_client |
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
39 @twitter_id = twitter_id |
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
40 end |
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
41 |
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
42 def init_client(config_id = 0) |
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
43 @client_config_id = config_id |
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
44 config_hash_key = "twitter#{config_id}".to_sym |
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
45 |
77
fc2bc34e6d89
Don't loop if twitter0 is not configured
nanaya <me@myconan.net>
parents:
76
diff
changeset
|
46 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
|
47 |
15 | 48 @client = Twitter::REST::Client.new do |config| |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
49 $cfg[config_hash_key].each do |cfg_key, cfg_value| |
21 | 50 config.public_send("#{cfg_key}=", cfg_value) |
51 end | |
52 end | |
15 | 53 end |
54 end |