-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_thumbs.rb
More file actions
79 lines (62 loc) · 1.63 KB
/
count_thumbs.rb
File metadata and controls
79 lines (62 loc) · 1.63 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
68
69
70
71
72
73
74
75
76
77
78
79
#!ruby
# Counts thumbs in a BGG thread.
#
# pre-reqs
# - ruby 2.3.0
# - nokogiri gem
# - httparty gem
#
require "nokogiri"
require "httparty"
require "byebug"
id = 426060 # MSP/STP thread
page = 1
delay = 5 # pause between gets so we don't spam BGG
thumbs_by_username = {}
def username(article_elem)
article_elem.css("div.avatarblock").first.attributes["data-username"].value
end
def thumbs(article_elem)
article_elem.css("dt.recs").text.to_i
end
def last_page?(doc)
doc.css("a[title^='next page']").count < 1
end
loop do
url = "https://www.boardgamegeek.com/thread/#{id}/page/#{page}"
puts url
response = HTTParty.get(url, timeout: 120)
if response.code != 200
puts "error"
puts response.code
break
else
doc = Nokogiri::HTML(response.body)
articles = doc.css("div.article")
puts " found #{articles.count} articles"
articles.each do |a|
username = username(a)
t_count = thumbs(a)
if thumbs_by_username.has_key?(username)
thumbs_by_username[username] += t_count
else
thumbs_by_username[username] = t_count
end
end
end
if last_page?(doc)
break
else
page += 1
sleep delay
end
end
puts thumbs_by_username
puts "Total thumbs: #{thumbs_by_username.values.inject(0) {|sum, n| sum += n}}"
puts "Total unique users: #{thumbs_by_username.keys.count}"
puts "\n-------------------------------------------------------"
puts "rank:\t [thumbs]\t username"
puts "-------------------------------------------------------"
thumbs_by_username.sort {|a,b| b[1] <=> a[1]}.each_with_index do |n, index|
puts "#{index + 1}:\t [#{n[1]}]\t #{n[0]}"
end