-
Notifications
You must be signed in to change notification settings - Fork 1
/
hn_parser.rb
executable file
·238 lines (227 loc) · 5.96 KB
/
hn_parser.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/ruby
require 'config'
require 'hn_parsers'
require 'stores'
require 'time_tools'
puts '### Parsing Hacker News data'
def parse_threads
puts '# Parsing threads from html'
HNThreadParser.all.each do |thread|
thread.save
end
end
def parse_all_times
puts '# Parsing times from html'
store = AllTimesStore.new()
store.clear()
puts 'new comments:'
HNCommentsParser.all.each do |comment|
store.add_times(comment)
end
puts 'new threads:'
HNIndexParser.all("newest*").each do |index|
store.add_times(index)
end
store.save
end
def calculate_canonical_times
puts '# Calculating canonical times (averages if more than 1 reading)'
puts '...can take a while...'
a_store = AllTimesStore.new()
store = a_store.to_canonical_times()
store.save
end
def update_thread_post_times
puts '# Estimating times for posts'
store = TimesStore.new()
ThreadStore.all.each do |thread|
print "."
thread.each do |post|
post[:time] = store.time(post[:id])
post.delete(:time_string)
end
thread.save
end
print "\n"
end
def set_on_frontpage_times
puts '# Estimating when threads are on frontpage'
puts 'reading index pages:'
indices = HNIndexParser.all("index*")
indices.sort! {|x, y| x.save_time <=> y.save_time}
puts 'estimating when on and off frontpage:'
added_hash = {}
on_done_hash = {}
indices.each do |index|
current_hash = {}
print "."
index.each do |thread|
current_hash[thread[:id]] = 1
if !on_done_hash[thread[:id]]
full_thread = ThreadStore.new(thread[:id])
full_thread.on_frontpage_time = index.save_time
full_thread.save
on_done_hash[thread[:id]] = 1
end
end
added_hash.merge!(current_hash)
added_hash.keys.each do |id|
if !current_hash[id]
thread = ThreadStore.new(id)
thread.off_frontpage_time = index.save_time
thread.save
added_hash.delete(id)
end
end
end
print "\n"
end
def prune
puts '# Removing incomplete data'
end_time = ForumTools::CONFIG[:samples][ForumTools::CONFIG[:environment].to_sym][:end_time].to_i
ThreadStore.all.each do |thread|
print "."
delete = false
if !thread.respond_to?(:on_frontpage_time) or thread.on_frontpage_time.nil? or
!thread.respond_to?(:off_frontpage_time) or thread.off_frontpage_time.nil?
delete = true
end
if thread.empty?
delete = true
end
thread.each do |post|
if !post[:time]
delete = true
end
end
if !delete and thread[0][:time] > end_time
delete = true
end
if delete
puts "\ndeleting " + thread.file_name
thread.delete
end
end
print "\n"
end
def parse_users
puts '# Parsing users'
puts 'from stores'
times_for_each_user_hash = {}
threads_for_each_user_hash = {}
ThreadStore.all.each do |thread|
if !threads_for_each_user_hash[thread[0][:user]]
threads_for_each_user_hash[thread[0][:user]] = 0
end
threads_for_each_user_hash[thread[0][:user]] += 1
thread.each do |post|
if !times_for_each_user_hash[post[:user]]
times_for_each_user_hash[post[:user]] = []
end
times_for_each_user_hash[post[:user]] << post[:time]
end
end
peak_window_for_each_user = {}
posts_per_hour_for_each_user = {}
times_for_each_user_hash.each_pair do |user, times|
peak_window_for_each_user[user] = TimeTools.peak_window(times)
posts_per_hour_for_each_user[user] = TimeTools.per_period_adder(times, "hour")
end
puts 'from user-pages if available'
other_for_each_user_hash = {}
HNUserParser.all.each do |user|
other_for_each_user_hash[user.name] = user.to_hash
end
puts 'from timezones if available'
timezone_for_each_user_hash = {}
HNTimezoneListParser.all.each do |timezone_list|
timezone_list.each do |user|
timezone_for_each_user_hash[user] = timezone_list.name
end
end
country_for_each_user_hash = {}
HNCountryListParser.all.each do |country_list|
country_list.each do |user|
country_for_each_user_hash[user] = country_list.name
end
end
store = UsersStore.new()
store_for_each_user_hash = store.hash
store.clear()
times_for_each_user_hash.each_pair do |name, times|
peak_window = peak_window_for_each_user[name]
if other_for_each_user_hash[name]
user = other_for_each_user_hash[name]
elsif store_for_each_user_hash[name]
user = store_for_each_user_hash[name]
else
user = {}
end
user[:name] = name
user[:posts] = times.size
user[:threads] = threads_for_each_user_hash[name] || 0
user[:peak_window] = peak_window
user[:single_peak] = TimeTools.single_peak(peak_window, posts_per_hour_for_each_user[name])
if timezone_for_each_user_hash[name]
user[:timezone] = timezone_for_each_user_hash[name]
end
if country_for_each_user_hash[name]
user[:country] = country_for_each_user_hash[name]
end
store << user
end
store.save
end
def parse_forums
puts '# Parsing forums'
puts 'Only one forum'
threads = 0
posts = 0
start_time = Time.now.to_i
end_time = 0
users_hash = {}
ThreadStore.all() do |thread|
threads += 1
if thread[0][:time] < start_time
start_time = thread[0][:time]
end
thread.each do |post|
posts += 1
users_hash[post[:user]] = 1
if post[:time] > end_time
end_time = post[:time]
end
end
end
store = ForumsStore.new()
store.clear()
forum = {}
forum[:name] = "1"
forum[:threads] = threads || 0
forum[:posts] = posts
forum[:start_time] = start_time
forum[:end_time] = end_time
forum[:users] = users_hash.keys.size || 0
forum[:rank] = 1
store << forum
store.save
end
def parse_all
parse_threads()
parse_all_times()
calculate_canonical_times()
update_thread_post_times()
set_on_frontpage_times()
prune()
parse_users()
parse_forums()
end
args = ARGV.to_a
if args[0] == "user"
args.delete_at(0)
initialize_environment(args)
parse_users()
else
initialize_environment(args)
parse_all()
end