Mercurial > rsstweet
annotate app/models/tweet.rb @ 220:0e15b4db2da8
Remove unused variable
author | nanaya <me@nanaya.pro> |
---|---|
date | Thu, 27 May 2021 02:31:31 +0900 |
parents | 17461f7682a2 |
children | 545ce38ef3d6 |
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 |
176 | 13 def self.client_options(id) |
14 { | |
15 :timeouts => { | |
16 :connect => 5, | |
17 :read => 5, | |
18 :write => 5, | |
19 }, | |
20 }.merge $cfg[:twitter][id] | |
21 end | |
22 | |
135 | 23 def initialize(twitter_id) |
165 | 24 @twitter_id = twitter_id |
135 | 25 end |
26 | |
158
74422bae017d
Always use canonical id and turn cache time generator a class method
nanaya <me@nanaya.pro>
parents:
157
diff
changeset
|
27 def id |
74422bae017d
Always use canonical id and turn cache time generator a class method
nanaya <me@nanaya.pro>
parents:
157
diff
changeset
|
28 user.id |
152 | 29 end |
30 | |
86
5bfc986200db
The caching becomes a bit confusing because of parameters
nanaya <me@myconan.net>
parents:
78
diff
changeset
|
31 def timeline |
145 | 32 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
|
33 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
|
34 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
|
35 client_try(:user_timeline, id, TIMELINE_OPTIONS).tap do |data| |
145 | 36 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
|
37 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
|
38 wrong_user = data[:data].first.user |
160 | 39 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
|
40 end |
7ca8aeba1a63
Try tracing where the failures happen
nanaya <me@nanaya.pro>
parents:
145
diff
changeset
|
41 |
145 | 42 data[:data] = data[:data].select do |tweet| |
43 tweet.retweeted_status.nil? || tweet.user.id != tweet.retweeted_status.user.id | |
44 end.map { |tweet| tweet.to_h } | |
134
3646b3e319c7
Only try/catch the tweet downloading part
nanaya <me@nanaya.pro>
parents:
108
diff
changeset
|
45 end |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
46 end |
145 | 47 end |
141 | 48 |
145 | 49 raise Twitter::Error::NotFound if raw[:result] == :not_found |
144 | 50 |
145 | 51 @timeline = raw[:data].map { |tweet_hash| Twitter::Tweet.new(tweet_hash) } |
52 end | |
53 | |
54 @timeline | |
15 | 55 end |
56 | |
48
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
57 def user |
145 | 58 if @user.nil? |
165 | 59 cache_key = "user:v1:#{@twitter_id.is_a?(Integer) ? 'id' : 'lookup'}:#{@twitter_id}" |
60 raw = Rails.cache.fetch(cache_key, :expires_in => self.class.cache_expires_time) do | |
160 | 61 client_try(:user, @twitter_id).tap do |data| |
62 if data[:result] == :ok | |
63 user = data[:data] | |
64 | |
177 | 65 if user.id != @twitter_id && user.screen_name.downcase != @twitter_id.try(:downcase) |
160 | 66 throw "Wrong user data. Requested: #{@twitter_id}, got: #{user.id} (#{user.screen_name.printable})" |
67 end | |
68 end | |
69 end | |
48
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
70 end |
145 | 71 |
72 raise Twitter::Error::NotFound if raw[:result] == :not_found | |
73 | |
74 @user = raw[:data] | |
75 end | |
76 | |
77 @user | |
48
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
78 end |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
79 |
135 | 80 def client |
219 | 81 Clients.instance.get client_config_id |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
82 end |
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
83 |
145 | 84 def client_try(method, *args) |
85 initial_config_id = client_config_id | |
86 | |
87 begin | |
88 data = client.public_send method, *args | |
89 rescue Twitter::Error::TooManyRequests | |
219 | 90 @client_config_id = (1 + @client_config_id) % @client_config_count |
145 | 91 |
92 if initial_config_id == client_config_id | |
93 raise | |
94 else | |
95 retry | |
96 end | |
97 rescue Twitter::Error::NotFound | |
98 return { :result => :not_found } | |
99 end | |
100 | |
101 { :result => :ok, :data => data } | |
102 end | |
103 | |
135 | 104 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
|
105 @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
|
106 @client_config_id ||= rand(@client_config_count) |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
107 |
219 | 108 @client_config_id |
15 | 109 end |
110 end |