-
Notifications
You must be signed in to change notification settings - Fork 9
/
client.rb
51 lines (38 loc) · 1.39 KB
/
client.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
require 'hunting_season' # product hunt api
require 'csv' # write to CSV
class UpvoteDownloader
def initialize
puts "what's your product hunt developer token?"
@client = ProductHunt::Client.new(gets.chomp)
clear
puts "enter a product hunt URL, ie 'https://www.producthunt.com/posts/fomo-3'"
@slug = gets.chomp.split("/posts/")[1]
clear
end
def clear
system 'clear'
end
def run
puts "looking for post..."
post = @client.all_posts("search[slug]" => @slug)[0]
abort "post with slug '#{@slug}' not found, please try again." if post.nil?
puts "found post..."
get_and_show_voters(post)
end
def get_and_show_voters(post, voters = [])
vote_count = post['votes_count']
puts "processing #{vote_count} votes..."
pages = (vote_count.to_f / 50).ceil
pages.times do |page|
voters << post.votes(per_page: 50, page: page+1, order: 'asc')
voters.flatten!
puts "finished #{voters.count} votes..."
end
output = voters.flatten.uniq.map {|v| v['user']['twitter_username']}.compact.each_slice(1).map {|x| p x} # split users into rows
File.open("#{post['name'].downcase}_voters.csv", "w") {|f| f.write(output.inject([]) { |csv, row| csv << CSV.generate_line(row) }.join(""))}
clear
puts "done!\nyour CSV export is in the same folder as this file.\npowered by: www.ryanckulp.com"
end
end
# run
UpvoteDownloader.new.run