twitter_with_auto_pagination 0.8.8 → 0.8.9
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1c58523412b513e538fc1b184026dbe03a3c8d66
|
4
|
+
data.tar.gz: 7ec3c80b9615c90a04d01a39b1550afda3d9115d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6258c081d6c2745cbc5e917e9f4e39886eca6fb1b8aa2aad9e2f91e6c2a7b77555092a15eba50fc8279223251614e62b3ec4e04c85dc8ae610c29584ee89f837
|
7
|
+
data.tar.gz: fa959db4a3e3eec7e9a43501185d42a1e7f39807ffc7cd9b2177928c92909db660aa00029bf4203f32e1cfd4b708630fb6e509fdf059a5f4ca9317e78f731e5e
|
@@ -5,77 +5,131 @@ module TwitterWithAutoPagination
|
|
5
5
|
module Uncategorized
|
6
6
|
include TwitterWithAutoPagination::REST::Utils
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
8
|
+
EVERY_DAY = (0..6)
|
9
|
+
WDAY_COUNT = EVERY_DAY.map { |n| [n, 0] }.to_h
|
10
|
+
WDAY_NIL_COUNT = EVERY_DAY.map { |n| [n, nil] }.to_h
|
11
|
+
|
12
|
+
EVERY_HOUR = (0..23)
|
13
|
+
HOUR_COUNT = EVERY_HOUR.map { |n| [n, 0] }.to_h
|
14
|
+
HOUR_NIL_COUNT = EVERY_HOUR.map { |n| [n, nil] }.to_h
|
15
|
+
|
16
|
+
def count_wday(times)
|
17
|
+
times.each_with_object(WDAY_COUNT.dup) { |time, memo| memo[time.wday] += 1 }
|
18
|
+
end
|
19
|
+
|
20
|
+
def count_hour(times)
|
21
|
+
times.each_with_object(HOUR_COUNT.dup) { |time, memo| memo[time.hour] += 1 }
|
22
|
+
end
|
23
|
+
|
24
|
+
# [
|
25
|
+
# {:name=>"Sun", :y=>111, :drilldown=>"Sun"},
|
26
|
+
# {:name=>"Mon", :y=>95, :drilldown=>"Mon"},
|
27
|
+
# {:name=>"Tue", :y=>72, :drilldown=>"Tue"},
|
28
|
+
# {:name=>"Wed", :y=>70, :drilldown=>"Wed"},
|
29
|
+
# {:name=>"Thu", :y=>73, :drilldown=>"Thu"},
|
30
|
+
# {:name=>"Fri", :y=>81, :drilldown=>"Fri"},
|
31
|
+
# {:name=>"Sat", :y=>90, :drilldown=>"Sat"}
|
32
|
+
# ]
|
33
|
+
def usage_stats_wday_series_data(times, day_names:)
|
34
|
+
count_wday(times).map do |wday, count|
|
35
|
+
{name: day_names[wday], y: count, drilldown: day_names[wday]}
|
14
36
|
end
|
15
37
|
end
|
16
38
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
39
|
+
# [
|
40
|
+
# {
|
41
|
+
# :name=>"Sun",
|
42
|
+
# :id=>"Sun",
|
43
|
+
# :data=> [ ["0", 7], ["1", 12], ... , ["22", 10], ["23", 12] ]
|
44
|
+
# },
|
45
|
+
# ...
|
46
|
+
# {
|
47
|
+
# :name=>"Mon",
|
48
|
+
# :id=>"Mon",
|
49
|
+
# :data=> [ ["0", 22], ["1", 11], ... , ["22", 9], ["23", 14] ]
|
50
|
+
# }
|
51
|
+
def usage_stats_wday_drilldown_series(times, day_names:)
|
52
|
+
counts =
|
53
|
+
EVERY_DAY.each_with_object(WDAY_NIL_COUNT.dup) do |wday, memo|
|
54
|
+
memo[wday] = count_hour(times.select { |t| t.wday == wday })
|
24
55
|
end
|
25
|
-
|
26
|
-
|
56
|
+
|
57
|
+
counts.map { |wday, hour_count| [day_names[wday], hour_count] }.map do |wday, hour_count|
|
58
|
+
{name: wday, id: wday, data: hour_count.map { |hour, count| [hour.to_s, count] }}
|
27
59
|
end
|
28
60
|
end
|
29
61
|
|
62
|
+
# [
|
63
|
+
# {:name=>"0", :y=>66, :drilldown=>"0"},
|
64
|
+
# {:name=>"1", :y=>47, :drilldown=>"1"},
|
65
|
+
# ...
|
66
|
+
# {:name=>"22", :y=>73, :drilldown=>"22"},
|
67
|
+
# {:name=>"23", :y=>87, :drilldown=>"23"}
|
68
|
+
# ]
|
30
69
|
def usage_stats_hour_series_data(times)
|
31
|
-
|
32
|
-
|
33
|
-
end
|
34
|
-
hour_count.map do |key, value|
|
35
|
-
{name: key.to_s, y: value, drilldown: key.to_s}
|
70
|
+
count_hour(times).map do |hour, count|
|
71
|
+
{name: hour.to_s, y: count, drilldown: hour.to_s}
|
36
72
|
end
|
37
73
|
end
|
38
74
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
75
|
+
# [
|
76
|
+
# {:name=>"0", :id=>"0", :data=>[["Sun", 7], ["Mon", 22], ["Tue", 8], ["Wed", 9], ["Thu", 9], ["Fri", 6], ["Sat", 5]]},
|
77
|
+
# {:name=>"1", :id=>"1", :data=>[["Sun", 12], ["Mon", 11], ["Tue", 5], ["Wed", 5], ["Thu", 0], ["Fri", 8], ["Sat", 6]]},
|
78
|
+
# ...
|
79
|
+
# ]
|
80
|
+
def usage_stats_hour_drilldown_series(times, day_names:)
|
81
|
+
counts =
|
82
|
+
EVERY_HOUR.each_with_object(HOUR_NIL_COUNT.dup) do |hour, memo|
|
83
|
+
memo[hour] = count_wday(times.select { |t| t.hour == hour })
|
46
84
|
end
|
47
|
-
|
48
|
-
|
85
|
+
|
86
|
+
counts.map do |hour, wday_count|
|
87
|
+
{name: hour.to_s, id: hour.to_s, data: wday_count.map { |wday, count| [day_names[wday], count] }}
|
49
88
|
end
|
50
89
|
end
|
51
90
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
91
|
+
# [
|
92
|
+
# {:name=>"Sun", :y=>14.778310502283107},
|
93
|
+
# {:name=>"Mon", :y=>12.273439878234399},
|
94
|
+
# {:name=>"Tue", :y=>10.110578386605784},
|
95
|
+
# {:name=>"Wed", :y=>9.843683409436835},
|
96
|
+
# {:name=>"Thu", :y=>10.547945205479452},
|
97
|
+
# {:name=>"Fri", :y=>10.61773211567732},
|
98
|
+
# {:name=>"Sat", :y=>12.115753424657534}
|
99
|
+
# ]
|
100
|
+
def twitter_addiction_series(times, day_names:)
|
101
|
+
max_duration = 5.minutes
|
102
|
+
wday_count =
|
103
|
+
EVERY_DAY.each_with_object(WDAY_NIL_COUNT.dup) do |wday, memo|
|
56
104
|
target_times = times.select { |t| t.wday == wday }
|
57
|
-
|
105
|
+
memo[wday] =
|
106
|
+
if target_times.empty?
|
107
|
+
nil
|
108
|
+
else
|
109
|
+
target_times.each_cons(2).map { |newer, older| (newer - older) < max_duration ? newer - older : max_duration }.sum
|
110
|
+
end
|
58
111
|
end
|
59
|
-
days = times.map{|t| t.to_date.to_s(:long) }.uniq.size
|
60
|
-
weeks =
|
61
|
-
|
62
|
-
{name:
|
112
|
+
days = times.map { |t| t.to_date.to_s(:long) }.uniq.size
|
113
|
+
weeks = [days / 7.0, 1.0].max
|
114
|
+
wday_count.map do |wday, seconds|
|
115
|
+
{name: day_names[wday], y: (seconds.nil? ? nil : seconds / weeks / 60)}
|
63
116
|
end
|
64
117
|
end
|
65
118
|
|
66
119
|
def usage_stats(user, options = {})
|
67
120
|
n_days_ago = options.has_key?(:days) ? options[:days].days.ago : 100.years.ago
|
68
121
|
tweets = options.has_key?(:tweets) ? options.delete(:tweets) : user_timeline(user)
|
122
|
+
day_names = options.has_key?(:day_names) ? options.delete(:day_names) : %w(Sun Mon Tue Wed Thu Fri Sat)
|
69
123
|
times =
|
70
124
|
# TODO Use user specific time zone
|
71
125
|
tweets.map { |t| ActiveSupport::TimeZone['Tokyo'].parse(t.created_at.to_s) }.
|
72
126
|
select { |t| t > n_days_ago }
|
73
127
|
[
|
74
|
-
usage_stats_wday_series_data(times),
|
75
|
-
usage_stats_wday_drilldown_series(times),
|
128
|
+
usage_stats_wday_series_data(times, day_names: day_names),
|
129
|
+
usage_stats_wday_drilldown_series(times, day_names: day_names),
|
76
130
|
usage_stats_hour_series_data(times),
|
77
|
-
usage_stats_hour_drilldown_series(times),
|
78
|
-
twitter_addiction_series(times)
|
131
|
+
usage_stats_hour_drilldown_series(times, day_names: day_names),
|
132
|
+
twitter_addiction_series(times, day_names: day_names)
|
79
133
|
]
|
80
134
|
end
|
81
135
|
end
|