-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_thread_users.rb
More file actions
69 lines (57 loc) · 1.65 KB
/
count_thread_users.rb
File metadata and controls
69 lines (57 loc) · 1.65 KB
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
#!ruby
# Counts users in a BGG thread.
#
# pre-reqs
# - ruby 1.9.2
# - nokogiri gem
#
require "nokogiri"
require "httparty"
id = 426060 # MPS / STP thread
#id = 710797 # TCAT
max_batch_size = 500
degraded_batch_size = 25
batch_size = max_batch_size
last_article_id = 0
usernames = {}
counter = 1
article_count = 0
loop do
url = "https://www.boardgamegeek.com/xmlapi2/thread?id=#{id}&count=#{batch_size}&minarticleid=#{last_article_id}"
puts "Batch #{counter} - articles: #{article_count} - #{url}"
response = HTTParty.get(url, timeout: 120)
if response.code != 200
puts "Something went wrong"
puts response
puts "Pausing 30 seconds"
batch_size = degraded_batch_size
sleep 30
next
else
doc = Nokogiri::XML(response.body)
articles = doc.xpath("//thread/articles/article")
if articles == nil || articles.count < 1
break
end
article_count += articles.count
articles.each do |article|
last_article_id = article.attributes["id"].value.to_i + 1 # We don't want to see the last article in the next request
username = article.attributes["username"].value
if usernames.has_key?(username)
usernames[username] += 1
else
usernames[username] = 1
end
end
end
counter += 1
batch_size = max_batch_size
end
puts "Total posts: #{usernames.values.inject(0) {|sum, n| sum += n}}"
puts "Total unique users: #{usernames.keys.count}"
puts "\n-------------------------------------------------------"
puts "post count: username"
puts "-------------------------------------------------------"
usernames.sort {|a,b| b[1] <=> a[1]}.each do |n|
puts "#{n[1]}: #{n[0]}"
end