Mercurial > rsstweet
annotate app/models/tweet.rb @ 236:498043313523 legit-client
Support multiple headers
author | nanaya <me@nanaya.net> |
---|---|
date | Sat, 15 Jul 2023 01:40:31 +0900 |
parents | 545ce38ef3d6 |
children |
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) |
165 | 14 @twitter_id = twitter_id |
135 | 15 end |
16 | |
158
74422bae017d
Always use canonical id and turn cache time generator a class method
nanaya <me@nanaya.pro>
parents:
157
diff
changeset
|
17 def id |
74422bae017d
Always use canonical id and turn cache time generator a class method
nanaya <me@nanaya.pro>
parents:
157
diff
changeset
|
18 user.id |
152 | 19 end |
20 | |
86
5bfc986200db
The caching becomes a bit confusing because of parameters
nanaya <me@myconan.net>
parents:
78
diff
changeset
|
21 def timeline |
145 | 22 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
|
23 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
|
24 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
|
25 client_try(:user_timeline, id, TIMELINE_OPTIONS).tap do |data| |
145 | 26 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
|
27 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
|
28 wrong_user = data[:data].first.user |
160 | 29 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
|
30 end |
7ca8aeba1a63
Try tracing where the failures happen
nanaya <me@nanaya.pro>
parents:
145
diff
changeset
|
31 |
145 | 32 data[:data] = data[:data].select do |tweet| |
33 tweet.retweeted_status.nil? || tweet.user.id != tweet.retweeted_status.user.id | |
34 end.map { |tweet| tweet.to_h } | |
134
3646b3e319c7
Only try/catch the tweet downloading part
nanaya <me@nanaya.pro>
parents:
108
diff
changeset
|
35 end |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
36 end |
145 | 37 end |
141 | 38 |
145 | 39 raise Twitter::Error::NotFound if raw[:result] == :not_found |
144 | 40 |
145 | 41 @timeline = raw[:data].map { |tweet_hash| Twitter::Tweet.new(tweet_hash) } |
42 end | |
43 | |
44 @timeline | |
15 | 45 end |
46 | |
48
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
47 def user |
145 | 48 if @user.nil? |
165 | 49 cache_key = "user:v1:#{@twitter_id.is_a?(Integer) ? 'id' : 'lookup'}:#{@twitter_id}" |
50 raw = Rails.cache.fetch(cache_key, :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 | |
177 | 55 if user.id != @twitter_id && user.screen_name.downcase != @twitter_id.try(:downcase) |
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 |
219 | 71 Clients.instance.get client_config_id |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
72 end |
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
73 |
145 | 74 def client_try(method, *args) |
75 initial_config_id = client_config_id | |
76 | |
77 begin | |
78 data = client.public_send method, *args | |
79 rescue Twitter::Error::TooManyRequests | |
219 | 80 @client_config_id = (1 + @client_config_id) % @client_config_count |
145 | 81 |
82 if initial_config_id == client_config_id | |
83 raise | |
84 else | |
85 retry | |
86 end | |
87 rescue Twitter::Error::NotFound | |
88 return { :result => :not_found } | |
89 end | |
90 | |
91 { :result => :ok, :data => data } | |
92 end | |
93 | |
135 | 94 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
|
95 @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
|
96 @client_config_id ||= rand(@client_config_count) |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
97 |
219 | 98 @client_config_id |
15 | 99 end |
100 end |