#!/bin/bash
backlog_host='https://xxxx.backlog.com/'
api_key='xxxx'
user_name=sample
user_id=xxxx
echo "crawl $user_name"
./main.rb crawl --from=2016-12-01 --url=$backlog_host --api_key=$api_key --user_id=$user_id > activeties-$user_name.csv
echo "convert $user_name"
./main.rb convert activeties-$user_name.csv > data.js
Last active
January 10, 2018 06:52
-
-
Save tk3/4293daded7868c3c8bd14ba85cafd995 to your computer and use it in GitHub Desktop.
Backlog Contribution graph
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
require "faraday" | |
require "faraday_middleware" | |
require "time" | |
class Client | |
def initialize(host, api_key) | |
@api_key = api_key | |
@conn = Faraday.new(host) do |faraday| | |
faraday.request :json | |
faraday.response :json, :content_type => /\bjson$/ | |
faraday.adapter Faraday.default_adapter | |
end | |
end | |
def get_myself | |
r = @conn.get do |req| | |
req.url "/api/v2/users/myself" | |
req.params[:apiKey] = @api_key | |
end | |
r.body | |
end | |
def get_users | |
r = @conn.get do |req| | |
req.url "/api/v2/users" | |
req.params[:apiKey] = @api_key | |
end | |
r.body | |
end | |
def get_activities(user_id, params = {}) | |
r = @conn.get do |req| | |
req.url "/api/v2/users/#{user_id}/activities" | |
req.params[:apiKey] = @api_key | |
req.params[:count] = params[:count] if params.key?(:count) | |
req.params[:maxId] = params[:maxId] if params.key?(:maxId) | |
end | |
r.body | |
end | |
end |
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
var list_data = document.getElementById("list-data") | |
var key = "sample"; | |
var d = document.createElement("div"); | |
var p = document.createElement("p"); | |
p.innerHTML = "2017 " + key; | |
var g = document.createElement("div"); | |
g.id = "cal-heatmap-" + key; | |
d.appendChild(p); | |
d.appendChild(g); | |
list_data.appendChild(d); | |
var cal = new CalHeatMap(); | |
cal.init({ | |
start: new Date(2017, 1, 0), | |
itemSelector: "#cal-heatmap-" + key, | |
range: 13, | |
domain: "month", | |
legend: [1, 5, 15, 20, 30], | |
data: data | |
}); |
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
source 'https://rubygems.org' | |
gem "faraday" | |
gem "faraday_middleware" | |
gem "thor" |
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
<!doctype HTML> | |
<meta charset = 'utf-8'> | |
<html> | |
<head> | |
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/cal-heatmap/3.6.2/cal-heatmap.css'> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js" charset="utf-8"></script> | |
<script src='https://cdnjs.cloudflare.com/ajax/libs/cal-heatmap/3.6.2/cal-heatmap.min.js' type='text/javascript'></script> | |
<style> | |
.rChart { | |
display: block | |
margin: auto auto; | |
} | |
</style> | |
</head> | |
<body> | |
<div style="margin-left: 40px;" id="list-data"> | |
</div> | |
<script src='./data.js' type='text/javascript'></script> | |
<script src='./draw-data.js' type='text/javascript'></script> | |
</body> | |
</html> |
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 "thor" | |
$: << "./lib" | |
require "client.rb" | |
class MyCLI < Thor | |
desc "crawl HOST_URL API_KEY USERID", "crawl" | |
option :url, :required => true | |
option :api_key, :required => true | |
option :user_id, :required => true | |
option :from | |
def crawl | |
require "date" | |
if options[:from] | |
from = Time.parse options[:from] | |
else | |
from = (Date.today << 3).to_time | |
end | |
user_id = options[:user_id] | |
client = Client.new options[:url], options[:api_key] | |
r = client.get_activities(user_id, {:count => 1}) | |
next_max_id = r.first["id"] | |
c = Date.today.to_s | |
while from <= Time.parse(c).localtime | |
r = client.get_activities(user_id, {:count => 20, :maxId => next_max_id}) | |
r.each do |e| | |
puts "#{e['id']},#{e['created']},#{e['type']}" | |
end | |
next_max_id = r.last["id"] | |
c = r.last["created"] | |
sleep 4 | |
end | |
end | |
desc "convert CSV_FILE", "convert csv file to contribute graph json" | |
def convert(csv_file) | |
require "csv" | |
require "time" | |
require "json" | |
c = CSV.read csv_file | |
d = c.map {|a| | |
[a[0], Time.parse(Time.parse(a[1]).localtime.strftime("%Y%m%d")).to_i, a[2]] | |
}.inject(Hash.new(0)){|hash, a| hash[a[1].to_s] += 1; hash} | |
print "var data = #{JSON.dump(d)}" | |
end | |
desc "users", "list users" | |
option :url, :required => true | |
option :api_key, :required => true | |
def users | |
require "json" | |
client = Client.new options[:url], options[:api_key] | |
puts JSON.dump(client.get_users) | |
end | |
end | |
MyCLI.start ARGV |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment