Skip to content

Instantly share code, notes, and snippets.

@algon-320
Last active June 9, 2021 13:39
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save algon-320/64137db374404cb066ea65d2f620920f to your computer and use it in GitHub Desktop.
Save algon-320/64137db374404cb066ea65d2f620920f to your computer and use it in GitHub Desktop.
AtCoder・Codeforces・TopCoderのレーティングを取得して色付きで表示する
<p>
AtCoder : <a id="atcoder" target="_blank" style="text-decoration:none;font-weight:bold;">loading</a><br>
Codeforces : <a id="codeforces" target="_blank" style="text-decoration:none;font-weight:bold;">loading</a><br>
TopCoder SRM : <a id="topcoder" target="_blank" style="text-decoration:none;font-weight:bold;">loading</a><br>
</p>
<script type="text/javascript" src="http://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
var ratings = { 'atcoder':0, 'codeforces':0, 'topcoder':0 };
var handles = { 'atcoder':'algon', 'codeforces':'algon_320', 'topcoder':'algon_320' };
var url = { 'atcoder':'https://atcoder.jp/user/', 'codeforces':'http://codeforces.com/profile/', 'topcoder':'https://www.topcoder.com/members/'};
var colors = {
'atcoder':[
{ color:'#808080', min:0, max:399 },
{ color:'#804000', min:400, max:799 },
{ color:'#008000', min:800, max:1199 },
{ color:'#00C0C0', min:1200, max:1599 },
{ color:'#0000FF', min:1600, max:1999 },
{ color:'#C0C000', min:2000, max:2399 },
{ color:'#FF8000', min:2400, max:2799 },
{ color:'#FF0000', min:2800, max:9999 }
],
'codeforces':[
{ color:'#808080', min:0, max:1199 },
{ color:'#008000', min:1200, max:1399 },
{ color:'#03A89E', min:1400, max:1599 },
{ color:'#0000FF', min:1600, max:1899 },
{ color:'#AA00AA', min:1900, max:2099 },
{ color:'#FF8C00', min:2100, max:2399 },
{ color:'#FF0000', min:2400, max:9999 }
],
'topcoder':[
{ color:'#999999', min:0, max:899 },
{ color:'#00A900', min:900, max:1199 },
{ color:'#6666FF', min:1200, max:1499 },
{ color:'#DDCC00', min:1500, max:2199 },
{ color:'#EE0000', min:2200, max:9999 }
]
};
// 色・リンク・ハンドルネームを設定する
function setHtml(service) {
var cf = document.getElementById(service);
for(var i = 0, len = colors[service].length; i < len; i ++) {
var x = colors[service][i];
if(x.min <= ratings[service] && ratings[service] <= x.max)
cf.style.color = x.color; // 色
}
cf.href = url[service] + handles[service]; // リンク
cf.innerHTML = handles[service] + ' (' + ratings[service].toString() + ')'; // ハンドル
}
// 色なしでリンク・ハンドルネームを設定する
function setHtmlWithoutColor(service) {
var cf = document.getElementById(service);
cf.style.color = 'black'; // 文字色は黒
cf.href = url[service] + handles[service]; // リンク
cf.innerHTML = handles[service]; // ハンドル
}
// TopCoder SRMのレーティングを取得する
function getTopCoderRating() {
$.ajax({
type: 'GET',
url: 'http://api.topcoder.com/v2/users/'+handles['topcoder'],
dataType: 'json',
timeout: 10000,
cache: false,
success: function(json) {
if('error' in json) {
setHtmlWithoutColor('topcoder');
}
else {
for(var i = 0, len = json['ratingSummary'].length; i < len; i ++) {
if(json['ratingSummary'][i]['name'] == 'Algorithm') {
ratings['topcoder'] = json['ratingSummary'][i]['rating'];
break;
}
}
setHtml('topcoder');
}
},
error: function() {
setHtmlWithoutColor('topcoder');
}
});
}
// Codeforcesのレーティングを取得する
function getCodeforcesRating() {
$.ajax({
type: 'GET',
url: 'http://codeforces.com/api/user.info?handles='+handles['codeforces'],
dataType: 'json',
timeout: 10000,
cache: false,
success: function(json) {
if(json['status'] == 'OK') {
ratings['codeforces'] = json['result'][0]['rating'];
setHtml('codeforces');
}
else {
setHtmlWithoutColor('codeforces');
}
},
error: function() {
setHtmlWithoutColor('codeforces');
}
});
}
// AtCoderのレーティングを取得する(AtCoderの仕様変更で使えなくなる恐れあり)
function getAtCoderRating() {
var userpage = 'https://atcoder.jp/user/' + handles['atcoder'];
var yql = 'select * from htmlstring where url="' + userpage + '" and xpath="//*[@id=\'main-div\']/div/div/div[2]/dl/dd[2]/span"';
var url = 'https://query.yahooapis.com/v1/public/yql?q=' + encodeURI(yql) + '&format=json&env=store://datatables.org/alltableswithkeys';
$.ajax({
type: 'GET',
url: url,
dataType: 'json',
timeout: 10000,
cache: false,
success: function(json) {
if(json['query']['results'] != null) {
ratings['atcoder'] = Number($("span:eq(0)", "<div>" + JSON.stringify(json['query']['results']['result']) + "</div>").text());
setHtml('atcoder');
}
else {
setHtmlWithoutColor('atcoder');
}
},
error: function() {
setHtmlWithoutColor('atcoder');
}
});
}
getTopCoderRating();
getCodeforcesRating();
getAtCoderRating();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment