Mercurial > rsstweet
annotate app/models/tweet.rb @ 155:e78ba80ff83c
Update gems
author | nanaya <me@nanaya.pro> |
---|---|
date | Sat, 28 Jul 2018 02:01:24 +0900 |
parents | fd7344643903 |
children | 1abfa910d83e |
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 | |
152 | 18 def cache_key |
19 "timeline:v2:#{@twitter_id}/#{Base64.urlsafe_encode64 @twitter_id.to_s}" | |
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? |
152 | 24 raw = Rails.cache.fetch(cache_key, :expires_in => cache_expires_time) do |
145 | 25 client_try(:user_timeline, @twitter_id, TIMELINE_OPTIONS).tap do |data| |
26 if data[:result] == :ok | |
146
7ca8aeba1a63
Try tracing where the failures happen
nanaya <me@nanaya.pro>
parents:
145
diff
changeset
|
27 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
|
28 wrong_user = data[:data].first.user |
7ca8aeba1a63
Try tracing where the failures happen
nanaya <me@nanaya.pro>
parents:
145
diff
changeset
|
29 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
|
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? |
49 return timeline.first.user if timeline.any? | |
50 | |
51 raw = Rails.cache.fetch("user:v1:#{@twitter_id}", :expires_in => cache_expires_time) do | |
52 client_try :user, @twitter_id | |
48
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
53 end |
145 | 54 |
55 raise Twitter::Error::NotFound if raw[:result] == :not_found | |
56 | |
57 @user = raw[:data] | |
58 end | |
59 | |
60 @user | |
48
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
61 end |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
62 |
135 | 63 def client |
64 @clients[client_config_id] ||= | |
65 Twitter::REST::Client.new do |config| | |
66 $cfg[:twitter][client_config_id].each do |cfg_key, cfg_value| | |
138 | 67 config.public_send(:"#{cfg_key}=", cfg_value) |
135 | 68 end |
69 end | |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
70 end |
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
71 |
145 | 72 def client_try(method, *args) |
73 initial_config_id = client_config_id | |
74 | |
75 begin | |
76 data = client.public_send method, *args | |
77 rescue Twitter::Error::TooManyRequests | |
78 @client_config_id += 1 | |
79 | |
80 if initial_config_id == client_config_id | |
81 raise | |
82 else | |
83 retry | |
84 end | |
85 rescue Twitter::Error::NotFound | |
86 return { :result => :not_found } | |
87 end | |
88 | |
89 { :result => :ok, :data => data } | |
90 end | |
91 | |
135 | 92 def client_config_id |
93 @client_config_id ||= 0 | |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
94 |
136
59991d10f8a3
Fix the variable as well while at it
nanaya <me@nanaya.pro>
parents:
135
diff
changeset
|
95 @client_config_id %= $cfg[:twitter].size |
15 | 96 end |
97 end |