Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Geekbot API to CSV for easy analysis
#!/usr/bin/env ruby
require 'net/http'
require 'uri'
require 'rubygems'
require 'json'
require 'csv'
after = 1509494400 #2017-11-01 00:00:00 +0800
user_id = 'JS Console Network Tab can help you'
uri = URI.parse("https://api.geekbot.io/v1/reports/?after=#{after}&user_id=#{user_id}&limit=35")
req = Net::HTTP::Get.new(uri)
req['Authorization'] = 'Copy from here: https://geekbot.io/dashboard/#api'
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) do |http|
http.request(req)
end
if (res.is_a?(Net::HTTPSuccess))
response = JSON.parse(res.body)
q_a_table = response.map do |entry|
result = {}
result['timestamp'] = entry['timestamp']
result['yesterday'] = entry['questions'][0]['answer'] unless entry['questions'][0].nil?
result['today'] = entry['questions'][1]['answer'] unless entry['questions'][1].nil?
result['trouble'] = entry['questions'][2]['answer'] unless entry['questions'][2].nil?
result
end.map do |entry|
entry['timestamp'] = Time.at(entry['timestamp'].to_i).strftime('%Y-%m-%d')
entry['yesterday'] = entry['yesterday']&.gsub(/<\/?[^>]*>/, "")
entry['today'] = entry['today']&.gsub(/<\/?[^>]*>/, "")
entry['trouble'] = entry['trouble']&.gsub(/<\/?[^>]*>/, "")
entry
end
str = "timestamp,yesterday,today,trouble\n"
q_a_table.each do |row|
str += "#{row['timestamp']},#{row['yesterday']},#{row['today']},#{row['trouble']}\n"
end
File.open(Time.now.to_i.to_s + '.csv', 'w') do |file|
file.write(str)
end
0
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment