Mercurial > rsstweet
annotate app/models/tweet.rb @ 161:c2e9d3a36bde
Improved id check
author | nanaya <me@nanaya.pro> |
---|---|
date | Fri, 03 Aug 2018 02:01:32 +0900 |
parents | 4e4195e60c2b |
children | ed73d92c7113 |
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 | |
158
74422bae017d
Always use canonical id and turn cache time generator a class method
nanaya <me@nanaya.pro>
parents:
157
diff
changeset
|
9 def self.cache_expires_time |
74422bae017d
Always use canonical id and turn cache time generator a class method
nanaya <me@nanaya.pro>
parents:
157
diff
changeset
|
10 (15 + rand(15)).minutes |
74422bae017d
Always use canonical id and turn cache time generator a class method
nanaya <me@nanaya.pro>
parents:
157
diff
changeset
|
11 end |
74422bae017d
Always use canonical id and turn cache time generator a class method
nanaya <me@nanaya.pro>
parents:
157
diff
changeset
|
12 |
135 | 13 def initialize(twitter_id) |
14 @clients = {} | |
161 | 15 @twitter_id = twitter_id.to_s |
135 | 16 end |
17 | |
158
74422bae017d
Always use canonical id and turn cache time generator a class method
nanaya <me@nanaya.pro>
parents:
157
diff
changeset
|
18 def id |
74422bae017d
Always use canonical id and turn cache time generator a class method
nanaya <me@nanaya.pro>
parents:
157
diff
changeset
|
19 user.id |
152 | 20 end |
21 | |
86
5bfc986200db
The caching becomes a bit confusing because of parameters
nanaya <me@myconan.net>
parents:
78
diff
changeset
|
22 def timeline |
145 | 23 if @timeline.nil? |
158
74422bae017d
Always use canonical id and turn cache time generator a class method
nanaya <me@nanaya.pro>
parents:
157
diff
changeset
|
24 cache_key = "timeline:v2:#{id}/#{Base64.urlsafe_encode64 id.to_s}" |
74422bae017d
Always use canonical id and turn cache time generator a class method
nanaya <me@nanaya.pro>
parents:
157
diff
changeset
|
25 raw = Rails.cache.fetch(cache_key, :expires_in => self.class.cache_expires_time) do |
74422bae017d
Always use canonical id and turn cache time generator a class method
nanaya <me@nanaya.pro>
parents:
157
diff
changeset
|
26 client_try(:user_timeline, id, TIMELINE_OPTIONS).tap do |data| |
145 | 27 if data[:result] == :ok |
158
74422bae017d
Always use canonical id and turn cache time generator a class method
nanaya <me@nanaya.pro>
parents:
157
diff
changeset
|
28 if data[:data].any? && data[:data].first.user.id != id |
146
7ca8aeba1a63
Try tracing where the failures happen
nanaya <me@nanaya.pro>
parents:
145
diff
changeset
|
29 wrong_user = data[:data].first.user |
160 | 30 throw "Wrong timeline data. Requested: #{id}, got: #{wrong_user.id} (#{wrong_user.screen_name.printable})" |
146
7ca8aeba1a63
Try tracing where the failures happen
nanaya <me@nanaya.pro>
parents:
145
diff
changeset
|
31 end |
7ca8aeba1a63
Try tracing where the failures happen
nanaya <me@nanaya.pro>
parents:
145
diff
changeset
|
32 |
145 | 33 data[:data] = data[:data].select do |tweet| |
34 tweet.retweeted_status.nil? || tweet.user.id != tweet.retweeted_status.user.id | |
35 end.map { |tweet| tweet.to_h } | |
134
3646b3e319c7
Only try/catch the tweet downloading part
nanaya <me@nanaya.pro>
parents:
108
diff
changeset
|
36 end |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
37 end |
145 | 38 end |
141 | 39 |
145 | 40 raise Twitter::Error::NotFound if raw[:result] == :not_found |
144 | 41 |
145 | 42 @timeline = raw[:data].map { |tweet_hash| Twitter::Tweet.new(tweet_hash) } |
43 end | |
44 | |
45 @timeline | |
15 | 46 end |
47 | |
48
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
48 def user |
145 | 49 if @user.nil? |
158
74422bae017d
Always use canonical id and turn cache time generator a class method
nanaya <me@nanaya.pro>
parents:
157
diff
changeset
|
50 raw = Rails.cache.fetch("user:v1:#{@twitter_id}", :expires_in => self.class.cache_expires_time) do |
160 | 51 client_try(:user, @twitter_id).tap do |data| |
52 if data[:result] == :ok | |
53 user = data[:data] | |
54 | |
161 | 55 if user.id_str != @twitter_id || user.screen_name != @twitter_id |
160 | 56 throw "Wrong user data. Requested: #{@twitter_id}, got: #{user.id} (#{user.screen_name.printable})" |
57 end | |
58 end | |
59 end | |
48
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
60 end |
145 | 61 |
62 raise Twitter::Error::NotFound if raw[:result] == :not_found | |
63 | |
64 @user = raw[:data] | |
65 end | |
66 | |
67 @user | |
48
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
68 end |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
69 |
135 | 70 def client |
71 @clients[client_config_id] ||= | |
72 Twitter::REST::Client.new do |config| | |
73 $cfg[:twitter][client_config_id].each do |cfg_key, cfg_value| | |
138 | 74 config.public_send(:"#{cfg_key}=", cfg_value) |
135 | 75 end |
76 end | |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
77 end |
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
78 |
145 | 79 def client_try(method, *args) |
80 initial_config_id = client_config_id | |
81 | |
82 begin | |
83 data = client.public_send method, *args | |
84 rescue Twitter::Error::TooManyRequests | |
85 @client_config_id += 1 | |
86 | |
87 if initial_config_id == client_config_id | |
88 raise | |
89 else | |
90 retry | |
91 end | |
92 rescue Twitter::Error::NotFound | |
93 return { :result => :not_found } | |
94 end | |
95 | |
96 { :result => :ok, :data => data } | |
97 end | |
98 | |
135 | 99 def client_config_id |
158
74422bae017d
Always use canonical id and turn cache time generator a class method
nanaya <me@nanaya.pro>
parents:
157
diff
changeset
|
100 @client_config_count ||= $cfg[:twitter].size |
74422bae017d
Always use canonical id and turn cache time generator a class method
nanaya <me@nanaya.pro>
parents:
157
diff
changeset
|
101 @client_config_id ||= rand(@client_config_count) |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
102 |
158
74422bae017d
Always use canonical id and turn cache time generator a class method
nanaya <me@nanaya.pro>
parents:
157
diff
changeset
|
103 @client_config_id %= @client_config_count |
15 | 104 end |
105 end |