annotate app/models/tweet.rb @ 149:8a84441a8f55

Update gems
author nanaya <me@nanaya.pro>
date Sat, 13 Jan 2018 03:16:35 +0900
parents 7ca8aeba1a63
children fd7344643903
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
1 class Tweet
137
00dc9346dfaa Skip self retweets
nanaya <me@nanaya.pro>
parents: 136
diff changeset
2 TIMELINE_OPTIONS = {
00dc9346dfaa Skip self retweets
nanaya <me@nanaya.pro>
parents: 136
diff changeset
3 :count => 100,
00dc9346dfaa Skip self retweets
nanaya <me@nanaya.pro>
parents: 136
diff changeset
4 :exclude_replies => false,
00dc9346dfaa Skip self retweets
nanaya <me@nanaya.pro>
parents: 136
diff changeset
5 :include_rts => true,
00dc9346dfaa Skip self retweets
nanaya <me@nanaya.pro>
parents: 136
diff changeset
6 :tweet_mode => :extended,
00dc9346dfaa Skip self retweets
nanaya <me@nanaya.pro>
parents: 136
diff changeset
7 }
00dc9346dfaa Skip self retweets
nanaya <me@nanaya.pro>
parents: 136
diff changeset
8
135
3fc882b0884d Refactored client initialization
nanaya <me@nanaya.pro>
parents: 134
diff changeset
9 def initialize(twitter_id)
3fc882b0884d Refactored client initialization
nanaya <me@nanaya.pro>
parents: 134
diff changeset
10 @clients = {}
3fc882b0884d Refactored client initialization
nanaya <me@nanaya.pro>
parents: 134
diff changeset
11 @twitter_id = twitter_id
3fc882b0884d Refactored client initialization
nanaya <me@nanaya.pro>
parents: 134
diff changeset
12 end
3fc882b0884d Refactored client initialization
nanaya <me@nanaya.pro>
parents: 134
diff changeset
13
97
ff5557f9902c Avoid uniformly expiring cache
nanaya <me@myconan.net>
parents: 88
diff changeset
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
ff5557f9902c Avoid uniformly expiring cache
nanaya <me@myconan.net>
parents: 88
diff changeset
16 end
ff5557f9902c Avoid uniformly expiring cache
nanaya <me@myconan.net>
parents: 88
diff changeset
17
86
5bfc986200db The caching becomes a bit confusing because of parameters
nanaya <me@myconan.net>
parents: 78
diff changeset
18 def timeline
145
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
19 if @timeline.nil?
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
20 raw = Rails.cache.fetch("timeline:v2:#{@twitter_id}", :expires_in => cache_expires_time) do
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
21 client_try(:user_timeline, @twitter_id, TIMELINE_OPTIONS).tap do |data|
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
22 if data[:result] == :ok
146
7ca8aeba1a63 Try tracing where the failures happen
nanaya <me@nanaya.pro>
parents: 145
diff changeset
23 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
24 wrong_user = data[:data].first.user
7ca8aeba1a63 Try tracing where the failures happen
nanaya <me@nanaya.pro>
parents: 145
diff changeset
25 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
26 end
7ca8aeba1a63 Try tracing where the failures happen
nanaya <me@nanaya.pro>
parents: 145
diff changeset
27
145
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
28 data[:data] = data[:data].select do |tweet|
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
29 tweet.retweeted_status.nil? || tweet.user.id != tweet.retweeted_status.user.id
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
30 end.map { |tweet| tweet.to_h }
134
3646b3e319c7 Only try/catch the tweet downloading part
nanaya <me@nanaya.pro>
parents: 108
diff changeset
31 end
76
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
32 end
145
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
33 end
141
88d7a7714d4e Restrict to just the fetch process
nanaya <me@nanaya.pro>
parents: 138
diff changeset
34
145
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
35 raise Twitter::Error::NotFound if raw[:result] == :not_found
144
1925b08153dc Record 404s
nanaya <me@nanaya.pro>
parents: 143
diff changeset
36
145
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
37 @timeline = raw[:data].map { |tweet_hash| Twitter::Tweet.new(tweet_hash) }
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
38 end
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
39
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
40 @timeline
15
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
41 end
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
42
48
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
43 def user
145
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
44 if @user.nil?
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
45 return timeline.first.user if timeline.any?
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
46
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
47 raw = Rails.cache.fetch("user:v1:#{@twitter_id}", :expires_in => cache_expires_time) do
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
48 client_try :user, @twitter_id
48
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
49 end
145
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
50
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
51 raise Twitter::Error::NotFound if raw[:result] == :not_found
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
52
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
53 @user = raw[:data]
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
54 end
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
55
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
56 @user
48
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
57 end
8983c426e256 Prevent exploding on empty timeline.
nanaya <me@myconan.net>
parents: 47
diff changeset
58
135
3fc882b0884d Refactored client initialization
nanaya <me@nanaya.pro>
parents: 134
diff changeset
59 def client
3fc882b0884d Refactored client initialization
nanaya <me@nanaya.pro>
parents: 134
diff changeset
60 @clients[client_config_id] ||=
3fc882b0884d Refactored client initialization
nanaya <me@nanaya.pro>
parents: 134
diff changeset
61 Twitter::REST::Client.new do |config|
3fc882b0884d Refactored client initialization
nanaya <me@nanaya.pro>
parents: 134
diff changeset
62 $cfg[:twitter][client_config_id].each do |cfg_key, cfg_value|
138
7907fe886675 Better use symbol?
nanaya <me@nanaya.pro>
parents: 137
diff changeset
63 config.public_send(:"#{cfg_key}=", cfg_value)
135
3fc882b0884d Refactored client initialization
nanaya <me@nanaya.pro>
parents: 134
diff changeset
64 end
3fc882b0884d Refactored client initialization
nanaya <me@nanaya.pro>
parents: 134
diff changeset
65 end
76
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
66 end
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
67
145
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
68 def client_try(method, *args)
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
69 initial_config_id = client_config_id
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
70
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
71 begin
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
72 data = client.public_send method, *args
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
73 rescue Twitter::Error::TooManyRequests
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
74 @client_config_id += 1
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
75
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
76 if initial_config_id == client_config_id
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
77 raise
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
78 else
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
79 retry
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
80 end
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
81 rescue Twitter::Error::NotFound
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
82 return { :result => :not_found }
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
83 end
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
84
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
85 { :result => :ok, :data => data }
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
86 end
c791b6bfeeda More refactors
nanaya <me@nanaya.pro>
parents: 144
diff changeset
87
135
3fc882b0884d Refactored client initialization
nanaya <me@nanaya.pro>
parents: 134
diff changeset
88 def client_config_id
3fc882b0884d Refactored client initialization
nanaya <me@nanaya.pro>
parents: 134
diff changeset
89 @client_config_id ||= 0
76
0c023d35cd80 Allows usage of multiple twitter keys
nanaya <me@myconan.net>
parents: 73
diff changeset
90
136
59991d10f8a3 Fix the variable as well while at it
nanaya <me@nanaya.pro>
parents: 135
diff changeset
91 @client_config_id %= $cfg[:twitter].size
15
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
92 end
207917e41964 Add tweets~
edogawaconan <me@myconan.net>
parents:
diff changeset
93 end