Created
December 25, 2017 07:05
-
-
Save gilbertwat/0bd1646bd594658753723e948bb83b17 to your computer and use it in GitHub Desktop.
Geekbot API to CSV for easy analysis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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