Mercurial > rsstweet
annotate app/models/tweet.rb @ 151:f0ac0c99af36
Update gems
author | nanaya <me@nanaya.pro> |
---|---|
date | Sat, 17 Feb 2018 20:45:00 +0900 |
parents | 7ca8aeba1a63 |
children | fd7344643903 |
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 | |
146
7ca8aeba1a63
Try tracing where the failures happen
nanaya <me@nanaya.pro>
parents:
145
diff
changeset
|
23 if data[:data].any? && data[:data].first.user.id != @twitter_id |
7ca8aeba1a63
Try tracing where the failures happen
nanaya <me@nanaya.pro>
parents:
145
diff
changeset
|
24 wrong_user = data[:data].first.user |
7ca8aeba1a63
Try tracing where the failures happen
nanaya <me@nanaya.pro>
parents:
145
diff
changeset
|
25 Rails.logger.warn "Wrong timeline data. Requested: #{@twitter_id}, got: #{wrong_user.id} (#{wrong_user.name.printable})" |
7ca8aeba1a63
Try tracing where the failures happen
nanaya <me@nanaya.pro>
parents:
145
diff
changeset
|
26 end |
7ca8aeba1a63
Try tracing where the failures happen
nanaya <me@nanaya.pro>
parents:
145
diff
changeset
|
27 |
145 | 28 data[:data] = data[:data].select do |tweet| |
29 tweet.retweeted_status.nil? || tweet.user.id != tweet.retweeted_status.user.id | |
30 end.map { |tweet| tweet.to_h } | |
134
3646b3e319c7
Only try/catch the tweet downloading part
nanaya <me@nanaya.pro>
parents:
108
diff
changeset
|
31 end |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
32 end |
145 | 33 end |
141 | 34 |
145 | 35 raise Twitter::Error::NotFound if raw[:result] == :not_found |
144 | 36 |
145 | 37 @timeline = raw[:data].map { |tweet_hash| Twitter::Tweet.new(tweet_hash) } |
38 end | |
39 | |
40 @timeline | |
15 | 41 end |
42 | |
48
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
43 def user |
145 | 44 if @user.nil? |
45 return timeline.first.user if timeline.any? | |
46 | |
47 raw = Rails.cache.fetch("user:v1:#{@twitter_id}", :expires_in => cache_expires_time) do | |
48 client_try :user, @twitter_id | |
48
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
49 end |
145 | 50 |
51 raise Twitter::Error::NotFound if raw[:result] == :not_found | |
52 | |
53 @user = raw[:data] | |
54 end | |
55 | |
56 @user | |
48
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
57 end |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
58 |
135 | 59 def client |
60 @clients[client_config_id] ||= | |
61 Twitter::REST::Client.new do |config| | |
62 $cfg[:twitter][client_config_id].each do |cfg_key, cfg_value| | |
138 | 63 config.public_send(:"#{cfg_key}=", cfg_value) |
135 | 64 end |
65 end | |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
66 end |
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
67 |
145 | 68 def client_try(method, *args) |
69 initial_config_id = client_config_id | |
70 | |
71 begin | |
72 data = client.public_send method, *args | |
73 rescue Twitter::Error::TooManyRequests | |
74 @client_config_id += 1 | |
75 | |
76 if initial_config_id == client_config_id | |
77 raise | |
78 else | |
79 retry | |
80 end | |
81 rescue Twitter::Error::NotFound | |
82 return { :result => :not_found } | |
83 end | |
84 | |
85 { :result => :ok, :data => data } | |
86 end | |
87 | |
135 | 88 def client_config_id |
89 @client_config_id ||= 0 | |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
90 |
136
59991d10f8a3
Fix the variable as well while at it
nanaya <me@nanaya.pro>
parents:
135
diff
changeset
|
91 @client_config_id %= $cfg[:twitter].size |
15 | 92 end |
93 end |