Skip to content

Instantly share code, notes, and snippets.

@hamukichi
Last active October 2, 2016 07:44
Show Gist options
  • Save hamukichi/25922e8ee18f64f19e8037ecce710767 to your computer and use it in GitHub Desktop.
Save hamukichi/25922e8ee18f64f19e8037ecce710767 to your computer and use it in GitHub Desktop.
APIを利用して、CodeforcesとTopCoder SRMにおける自分のRating変化を取得するPythonスクリプト。例外処理等が不十分なので注意。ライブラリとしてRequestsが必要。
#!/usr/bin/env python3
import collections
import requests
CF_ENTRYPOINT = "http://codeforces.com/api/user.rating"
TC_ENTRYPOINT = "http://api.topcoder.com/v2/users/{}/statistics/data/srm"
RatingInfo = collections.namedtuple("RatingInfo", "old new delta")
class APIError(Exception):
pass
def get_result_json(entrypoint, params=None):
req = requests.get(entrypoint, params)
if req.status_code != 200:
raise APIError("HTTP {}".format(req.status_code))
else:
return req.json()
def get_cf_rating(handle):
res = get_result_json(CF_ENTRYPOINT, params={"handle": handle})
if res["status"] != "OK":
raise APIError("{}: {}".format(res["status"], res["comment"]))
if not res["result"]:
raise ValueError("No rating data")
last_change = res["result"][-1]
old = last_change["oldRating"]
new = last_change["newRating"]
delta = new - old
return RatingInfo(old, new, delta)
def get_tc_rating(handle):
res = get_result_json(TC_ENTRYPOINT.format(handle))
new = res["rating"]
old = res["History"][-2]["rating"]
return RatingInfo(old, new, new - old)
def main():
# ここをあなたのハンドルに変更
cf_handle = "hamukichi"
tc_handle = "hamukichi_nbr"
print("Codeforces: {} -> {} ({:+d})".format(*get_cf_rating(cf_handle)))
print("TopCoder SRM: {} -> {} ({:+d})".format(*get_tc_rating(tc_handle)))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment