Mercurial > rsstweet
annotate app/models/tweet.rb @ 145:c791b6bfeeda
More refactors
author | nanaya <me@nanaya.pro> |
---|---|
date | Wed, 13 Dec 2017 06:31:07 +0900 |
parents | 1925b08153dc |
children | 7ca8aeba1a63 |
rev | line source |
---|---|
15 | 1 class Tweet |
137 | 2 TIMELINE_OPTIONS = { |
3 :count => 100, | |
4 :exclude_replies => false, | |
5 :include_rts => true, | |
6 :tweet_mode => :extended, | |
7 } | |
8 | |
135 | 9 def initialize(twitter_id) |
10 @clients = {} | |
11 @twitter_id = twitter_id | |
12 end | |
13 | |
97 | 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 | 16 end |
17 | |
86
5bfc986200db
The caching becomes a bit confusing because of parameters
nanaya <me@myconan.net>
parents:
78
diff
changeset
|
18 def timeline |
145 | 19 if @timeline.nil? |
20 raw = Rails.cache.fetch("timeline:v2:#{@twitter_id}", :expires_in => cache_expires_time) do | |
21 client_try(:user_timeline, @twitter_id, TIMELINE_OPTIONS).tap do |data| | |
22 if data[:result] == :ok | |
23 data[:data] = data[:data].select do |tweet| | |
24 tweet.retweeted_status.nil? || tweet.user.id != tweet.retweeted_status.user.id | |
25 end.map { |tweet| tweet.to_h } | |
134
3646b3e319c7
Only try/catch the tweet downloading part
nanaya <me@nanaya.pro>
parents:
108
diff
changeset
|
26 end |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
27 end |
145 | 28 end |
141 | 29 |
145 | 30 raise Twitter::Error::NotFound if raw[:result] == :not_found |
144 | 31 |
145 | 32 @timeline = raw[:data].map { |tweet_hash| Twitter::Tweet.new(tweet_hash) } |
33 end | |
34 | |
35 @timeline | |
15 | 36 end |
37 | |
48
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
38 def user |
145 | 39 if @user.nil? |
40 return timeline.first.user if timeline.any? | |
41 | |
42 raw = Rails.cache.fetch("user:v1:#{@twitter_id}", :expires_in => cache_expires_time) do | |
43 client_try :user, @twitter_id | |
48
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
44 end |
145 | 45 |
46 raise Twitter::Error::NotFound if raw[:result] == :not_found | |
47 | |
48 @user = raw[:data] | |
49 end | |
50 | |
51 @user | |
48
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 |
135 | 54 def client |
55 @clients[client_config_id] ||= | |
56 Twitter::REST::Client.new do |config| | |
57 $cfg[:twitter][client_config_id].each do |cfg_key, cfg_value| | |
138 | 58 config.public_send(:"#{cfg_key}=", cfg_value) |
135 | 59 end |
60 end | |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
61 end |
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
62 |
145 | 63 def client_try(method, *args) |
64 initial_config_id = client_config_id | |
65 | |
66 begin | |
67 data = client.public_send method, *args | |
68 rescue Twitter::Error::TooManyRequests | |
69 @client_config_id += 1 | |
70 | |
71 if initial_config_id == client_config_id | |
72 raise | |
73 else | |
74 retry | |
75 end | |
76 rescue Twitter::Error::NotFound | |
77 return { :result => :not_found } | |
78 end | |
79 | |
80 { :result => :ok, :data => data } | |
81 end | |
82 | |
135 | 83 def client_config_id |
84 @client_config_id ||= 0 | |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
85 |
136
59991d10f8a3
Fix the variable as well while at it
nanaya <me@nanaya.pro>
parents:
135
diff
changeset
|
86 @client_config_id %= $cfg[:twitter].size |
15 | 87 end |
88 end |