Class: ExTwitter::Client
- Inherits:
-
Twitter::REST::Client
- Object
- Twitter::REST::Client
- ExTwitter::Client
show all
- Includes:
- ExistingApi, NewApi, Utils
- Defined in:
- lib/ex_twitter/client.rb
Constant Summary
collapse
- INDENT =
4
Constants included
from Utils
Utils::PROFILE_SAVE_KEYS, Utils::STATUS_SAVE_KEYS
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from NewApi
#_extract_inactive_users, #_extract_screen_names, #_extract_uids, #_extract_users, #_fetch_parallelly, #clusters_assigned_to, #clusters_belong_to, #common_followers, #common_friends, #followers_parallelly, #friends_and_followers, #friends_followers_and_statuses, #friends_parallelly, #inactive_followers, #inactive_friends, #mutual_friends, #one_sided_followers, #one_sided_following, #removed, #removing, #replied, #replying
#favorites, #follower_ids, #followers, #friend_ids, #friends, #friendship?, #home_timeline, #mentions_timeline, #search, #user, #user?, #user_timeline, #users, #verify_credentials
Methods included from Utils
#__screen_name, #__uid, #__uid_i, #authenticating_user?, #authorized_user?, #call_old_method, #collect_with_cursor, #collect_with_max_id, #decode_json, #encode_json, #fetch_cache_or_call_api, #file_cache_key, #instrument, #namespaced_key, #screen_name, #uid, #uid_or_screen_name?
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/ex_twitter/client.rb', line 16
def initialize(options = {})
@cache = ActiveSupport::Cache::FileStore.new(File.join('tmp', 'api_cache'))
@call_count = 0
@uid = options.has_key?(:uid) ? options.delete(:uid).to_i : nil
@screen_name = options.has_key?(:screen_name) ? options.delete(:screen_name).to_s : nil
::LogSubscriber.attach_to :ex_twitter
::LogSubscriber.attach_to :active_support
@@logger = @logger =
if options[:logger]
options.delete(:logger)
else
Dir.mkdir('log') unless File.exists?('log')
Logger.new('log/ex_twitter.log')
end
super
end
|
Instance Attribute Details
#authenticated_user ⇒ Object
Returns the value of attribute authenticated_user.
41
42
43
|
# File 'lib/ex_twitter/client.rb', line 41
def authenticated_user
@authenticated_user
end
|
#cache ⇒ Object
Returns the value of attribute cache.
41
42
43
|
# File 'lib/ex_twitter/client.rb', line 41
def cache
@cache
end
|
#call_count ⇒ Object
Returns the value of attribute call_count.
40
41
42
|
# File 'lib/ex_twitter/client.rb', line 40
def call_count
@call_count
end
|
#logger ⇒ Object
Returns the value of attribute logger.
41
42
43
|
# File 'lib/ex_twitter/client.rb', line 41
def logger
@logger
end
|
Class Method Details
.logger ⇒ Object
36
37
38
|
# File 'lib/ex_twitter/client.rb', line 36
def self.logger
@@logger
end
|
Instance Method Details
148
149
150
|
# File 'lib/ex_twitter/client.rb', line 148
def (, options = {})
calc_scores_from_users(.map { |t| t.user }, options)
end
|
#calc_scores_from_users(users, options) ⇒ Object
140
141
142
143
144
145
146
|
# File 'lib/ex_twitter/client.rb', line 140
def calc_scores_from_users(users, options)
min = options.has_key?(:min) ? options[:min] : 0
max = options.has_key?(:max) ? options[:max] : 1000
users.each_with_object(Hash.new(0)) { |u, memo| memo[u.id] += 1 }.
select { |_k, v| min <= v && v <= max }.
sort_by { |_, v| -v }.to_h
end
|
#close_friends(_uid, options = {}) ⇒ Object
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
# File 'lib/ex_twitter/client.rb', line 171
def close_friends(_uid, options = {})
min = options.has_key?(:min) ? options[:min] : 0
max = options.has_key?(:max) ? options[:max] : 1000
uid_i = _uid.to_i
_replying = options.has_key?(:replying) ? options.delete(:replying) : replying(uid_i, options)
_replied = options.has_key?(:replied) ? options.delete(:replied) : replied(uid_i, options)
_favoriting = options.has_key?(:favoriting) ? options.delete(:favoriting) : favoriting(uid_i, options)
min_max = {min: min, max: max}
_users = _replying + _replied + _favoriting
return [] if _users.empty?
scores = calc_scores_from_users(_users, min_max)
replying_scores = calc_scores_from_users(_replying, min_max)
replied_scores = calc_scores_from_users(_replied, min_max)
favoriting_scores = calc_scores_from_users(_favoriting, min_max)
scores.keys.map { |uid| _users.find { |u| u.id.to_i == uid.to_i } }.
map do |u|
u[:score] = scores[u.id]
u[:replying_score] = replying_scores[u.id]
u[:replied_score] = replied_scores[u.id]
u[:favoriting_score] = favoriting_scores[u.id]
u
end
end
|
#favorited_by(user) ⇒ Object
168
169
|
# File 'lib/ex_twitter/client.rb', line 168
def favorited_by(user)
end
|
#favoriting(user, options = {}) ⇒ Object
160
161
162
163
164
165
166
|
# File 'lib/ex_twitter/client.rb', line 160
def favoriting(user, options= {})
favs = options.has_key?(:favorites) ? options.delete(:favorites) : favorites(user, options)
select_favoriting_from_favs(favs, options)
rescue => e
logger.warn "#{__method__} #{user.inspect} #{e.class} #{e.message}"
raise e
end
|
#select_favoriting_from_favs(favs, options = {}) ⇒ Object
152
153
154
155
156
157
158
|
# File 'lib/ex_twitter/client.rb', line 152
def select_favoriting_from_favs(favs, options = {})
return [] if favs.empty?
uids = (favs)
result = uids.map { |uid, score| f = favs.
find { |f| f.user.id.to_i == uid.to_i }; Array.new(score, f) }.flatten.map { |f| f.user }
(options.has_key?(:uniq) && !options[:uniq]) ? result : result.uniq { |u| u.id }
end
|
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/ex_twitter/client.rb', line 109
def (times)
five_mins = 5.minutes
wday_expended_seconds =
(0..6).each_with_object((0..6).map { |n| [n, nil] }.to_h) do |wday, wday_memo|
target_times = times.select { |t| t.wday == wday }
wday_memo[wday] = target_times.empty? ? nil : target_times.each_cons(2).map {|a, b| (a - b) < five_mins ? a - b : five_mins }.sum
end
days = times.map{|t| t.to_date.to_s(:long) }.uniq.size
weeks = (days > 7) ? days / 7.0 : 1.0
wday_expended_seconds.map { |k, v| [I18n.t('date.abbr_day_names')[k], (v.nil? ? nil : v / weeks / 60)] }.map do |key, value|
{name: key, y: value}
end
end
|
#usage_stats(user, options = {}) ⇒ Object
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/ex_twitter/client.rb', line 123
def usage_stats(user, options = {})
n_days_ago = options.has_key?(:days) ? options[:days].days.ago : 100.years.ago
= options.has_key?(:tweets) ? options.delete(:tweets) : user_timeline(user)
times =
.map { |t| ActiveSupport::TimeZone['Tokyo'].parse(t.created_at.to_s) }.
select { |t| t > n_days_ago }
[
usage_stats_wday_series_data(times),
usage_stats_wday_drilldown_series(times),
usage_stats_hour_series_data(times),
usage_stats_hour_drilldown_series(times),
(times)
]
end
|
#usage_stats_hour_drilldown_series(times) ⇒ Object
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/ex_twitter/client.rb', line 96
def usage_stats_hour_drilldown_series(times)
wday_count =
(0..23).each_with_object((0..23).map { |n| [n, nil] }.to_h) do |hour, hour_memo|
hour_memo[hour] =
times.select { |t| t.hour == hour }.map { |t| t.wday }.each_with_object((0..6).map { |n| [n, 0] }.to_h) do |wday, wday_memo|
wday_memo[wday] += 1
end
end
wday_count.map do |key, value|
{name: key.to_s, id: key.to_s, data: value.to_a.map{|a| [I18n.t('date.abbr_day_names')[a[0]], a[1]] }}
end
end
|
#usage_stats_hour_series_data(times) ⇒ Object
87
88
89
90
91
92
93
94
|
# File 'lib/ex_twitter/client.rb', line 87
def usage_stats_hour_series_data(times)
hour_count = times.each_with_object((0..23).map { |n| [n, 0] }.to_h) do |time, memo|
memo[time.hour] += 1
end
hour_count.map do |key, value|
{name: key.to_s, y: value, drilldown: key.to_s}
end
end
|
#usage_stats_wday_drilldown_series(times) ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/ex_twitter/client.rb', line 74
def usage_stats_wday_drilldown_series(times)
hour_count =
(0..6).each_with_object((0..6).map { |n| [n, nil] }.to_h) do |wday, wday_memo|
wday_memo[wday] =
times.select { |t| t.wday == wday }.map { |t| t.hour }.each_with_object((0..23).map { |n| [n, 0] }.to_h) do |hour, hour_memo|
hour_memo[hour] += 1
end
end
hour_count.map { |k, v| [I18n.t('date.abbr_day_names')[k], v] }.map do |key, value|
{name: key, id: key, data: value.to_a.map{|a| [a[0].to_s, a[1]] }}
end
end
|
#usage_stats_wday_series_data(times) ⇒ Object
65
66
67
68
69
70
71
72
|
# File 'lib/ex_twitter/client.rb', line 65
def usage_stats_wday_series_data(times)
wday_count = times.each_with_object((0..6).map { |n| [n, 0] }.to_h) do |time, memo|
memo[time.wday] += 1
end
wday_count.map { |k, v| [I18n.t('date.abbr_day_names')[k], v] }.map do |key, value|
{name: key, y: value, drilldown: key}
end
end
|