Mercurial > rsstweet
annotate app/models/tweet.rb @ 144:1925b08153dc
Record 404s
author | nanaya <me@nanaya.pro> |
---|---|
date | Wed, 13 Dec 2017 06:10:13 +0900 |
parents | 8a858aec1905 |
children | c791b6bfeeda |
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 |
135 | 19 initial_config_id = client_config_id |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
20 |
134
3646b3e319c7
Only try/catch the tweet downloading part
nanaya <me@nanaya.pro>
parents:
108
diff
changeset
|
21 @timeline ||= |
143 | 22 Rails.cache.fetch("timeline:v1:#{@twitter_id}", :expires_in => cache_expires_time) do |
134
3646b3e319c7
Only try/catch the tweet downloading part
nanaya <me@nanaya.pro>
parents:
108
diff
changeset
|
23 begin |
141 | 24 timeline = client.user_timeline(@twitter_id, TIMELINE_OPTIONS) |
134
3646b3e319c7
Only try/catch the tweet downloading part
nanaya <me@nanaya.pro>
parents:
108
diff
changeset
|
25 rescue Twitter::Error::TooManyRequests |
135 | 26 @client_config_id += 1 |
134
3646b3e319c7
Only try/catch the tweet downloading part
nanaya <me@nanaya.pro>
parents:
108
diff
changeset
|
27 |
135 | 28 if initial_config_id == client_config_id |
134
3646b3e319c7
Only try/catch the tweet downloading part
nanaya <me@nanaya.pro>
parents:
108
diff
changeset
|
29 raise |
3646b3e319c7
Only try/catch the tweet downloading part
nanaya <me@nanaya.pro>
parents:
108
diff
changeset
|
30 else |
3646b3e319c7
Only try/catch the tweet downloading part
nanaya <me@nanaya.pro>
parents:
108
diff
changeset
|
31 retry |
3646b3e319c7
Only try/catch the tweet downloading part
nanaya <me@nanaya.pro>
parents:
108
diff
changeset
|
32 end |
144 | 33 rescue Twitter::Error::NotFound |
34 data = { :result => :not_found } | |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
35 end |
141 | 36 |
144 | 37 data || { |
38 :result => :ok, | |
39 :timeline => timeline.select do |tweet| | |
40 tweet.retweeted_status.nil? || tweet.user.id != tweet.retweeted_status.user.id | |
41 end.map do |tweet| | |
42 # Fails when there's Twitter::NullObject initiated somewhere in previous select | |
43 # Reference: https://github.com/sferik/twitter/issues/892 | |
44 tweet.to_h | |
45 end, | |
46 } | |
47 end.tap do |data| | |
48 raise Twitter::Error::NotFound if data[:result] == :not_found | |
49 | |
50 data[:timeline_parsed] = data[:timeline].map do |tweet_hash| | |
51 Twitter::Tweet.new(tweet_hash) | |
141 | 52 end |
144 | 53 end[:timeline_parsed] |
15 | 54 end |
55 | |
48
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
56 def user |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
57 @user ||= |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
58 if timeline.any? |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
59 timeline.first.user |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
60 else |
143 | 61 Rails.cache.fetch("user:v1:#{@twitter_id}", :expires_in => cache_expires_time) do |
142 | 62 client.user(@twitter_id) |
48
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
63 end |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
64 end |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
65 end |
8983c426e256
Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents:
47
diff
changeset
|
66 |
135 | 67 def client |
68 @clients[client_config_id] ||= | |
69 Twitter::REST::Client.new do |config| | |
70 $cfg[:twitter][client_config_id].each do |cfg_key, cfg_value| | |
138 | 71 config.public_send(:"#{cfg_key}=", cfg_value) |
135 | 72 end |
73 end | |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
74 end |
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
75 |
135 | 76 def client_config_id |
77 @client_config_id ||= 0 | |
76
0c023d35cd80
Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents:
73
diff
changeset
|
78 |
136
59991d10f8a3
Fix the variable as well while at it
nanaya <me@nanaya.pro>
parents:
135
diff
changeset
|
79 @client_config_id %= $cfg[:twitter].size |
15 | 80 end |
81 end |