-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.rb
155 lines (135 loc) · 3.87 KB
/
lib.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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
require 'net/http'
require 'uri'
require 'json'
require 'date'
def get_users
[
'rika0384',
'shumon_84',
'ixmel_rd',
'tuki_remon',
'noy72',
'yuiop',
'yebityon',
'vvataarne',
'kuma13',
'fuu32',
'Oike7',
'Masumi',
'takaya',
]
end
def get_file # 本番では消す
begin
str = ''
File.open('save.json') do |file|
str = file.read
end
return str
rescue SystemCallError => e
puts %Q(class=[#{e.class}] message=[#{e.message}])
rescue IOError => e
puts %Q(class=[#{e.class}] message=[#{e.message}])
end
end
def checkContest(type, contest_name)
return false if type.empty?
if type == 'other' # その他のコンテスト
return false if ['arc', 'agc', 'abc'].all?{|contest|
contest_name.match(contest).nil?
}
else # 指定されたコンテスト
return false if contest_name.match(type)
end
return true
end
def getResults(type = '')
root = 'http://beta.kenkoooo.com/atcoder/atcoder-api/results?user=&rivals='
uri = URI.parse(root + get_users.join(','))
# puts Net::HTTP.get(uri)
results = JSON.parse(Net::HTTP.get(uri))
problems = getProblems(type)
# results = JSON.parse(get_file())
results.map{|problem|
next if problem['result'] != 'AC'
next if checkContest(type, problem['contest_id']) # 判定
next if problems[problem['contest_id']]['problem'][problem['problem_id']].nil? # 判定
problems[problem['contest_id']]['problem'][problem['problem_id']][:accept] << problem['user_id']
}
return problems
end
def getContests(type = '')
root = 'http://kenkoooo.com/atcoder/atcoder-api/info/contests'
uri = URI.parse(root)
results = JSON.parse(Net::HTTP.get(uri))
keys = ["start_epoch_second", "id", "title"]
contests = results.map{|result|
next if checkContest(type, result['id']) # 判定
contest = {}
[result['id'], keys.map{|key| [key, result[key] ]}.to_h]
}.reject{|c| c.nil? }.sort_by{|k,v| k}.to_h
end
def getProblems(type='')
root = 'http://kenkoooo.com/atcoder/atcoder-api/info/problems'
uri = URI.parse(root)
results = JSON.parse(Net::HTTP.get(uri))
contests = getContests(type).map{|k,contest |
contest['problem'] = []
[k,contest]
}.to_h
results.map{|result|
next if checkContest(type, result['contest_id']) # 判定
contests[result['contest_id']]['problem'] << [result['id'], {'id': result['id'], 'title': result['title'], 'contest_id': result['contest_id'], 'accept': []}]
}.reject{|c| c.nil?}#.sort_by{|k,v| k }.to_h
contests.map{|k,v|
v['problem'] = v['problem'].sort_by{|k,v| k}.to_h
[k,v]
}.to_h
end
def getResultCount
root = 'http://kenkoooo.com/atcoder/atcoder-api/info/ac'
users = get_users
uri = URI.parse(root)
results = JSON.parse(Net::HTTP.get(uri))
# users = users.map{|user| [user, 0] }.to_h
users = users.map{|user|
result = results.find{|result|
result['user_id'] == user
}
[user, result['problem_count']]
}.to_h
return users.sort_by{|k,v| -v }
end
def getGraph
root = 'http://beta.kenkoooo.com/atcoder/atcoder-api/results?user=&rivals='
users = get_users
uri = URI.parse(root + users.join(','))
results = JSON.parse(Net::HTTP.get(uri))
users = users.map{|user| [user, {} ] }.to_h
results.map{|problem|
next if problem['result'] != 'AC'
user = problem['user_id']
time = Time.at(problem['epoch_second'].to_i).to_s[0,10]
date = Date.parse(time)
users[user][date] = 0 if users[user][date].nil?
users[user][date] += 1
}
# 日付順にソート
users.map{|user, data|
data = data.sort{|(k1, v1), (k2, v2)| k1 <=> k2 }.to_h
users[user] = data
}
# 数え上げ
users.map{|user, data|
count = 0
users[user] = data.map{|date, solved|
count += solved
[date.to_s, count]
}.to_h
}
users = users.map{|user, data|
{:name => user, :data => data}
}
return users
end
puts getResultCount