我想使用该/users/{ids}/timeline
方法获取用户在 Stack Overflow 上的活动。
问题是这个方法只接受一个用户ID,但我想传递一个用户名,所以任何人都可以写下他们的昵称并获得相关的统计信息。API 似乎也不支持通过提供用户名来获取用户 ID。
是否有任何解决方法可以根据用户名获取用户的 ID?
我想使用该/users/{ids}/timeline
方法获取用户在 Stack Overflow 上的活动。
问题是这个方法只接受一个用户ID,但我想传递一个用户名,所以任何人都可以写下他们的昵称并获得相关的统计信息。API 似乎也不支持通过提供用户名来获取用户 ID。
是否有任何解决方法可以根据用户名获取用户的 ID?
/users
我通过在 YQL 的帮助下从端点抓取响应以通过 AJAX 跨域获取页面来破解我的方式。
这是粗略的代码(JS Bin):
const idCont = $('div');
const input = $('input');
// input events
input.on('blur', function() {
doAjax(this.value);
}).on('keydown', function(e) {
if (e.keyCode == 13)
doAjax(this.value);
});
function doAjax(url) {
url = 'http://stackoverflow.com/users/filter?search=' + url;
if (url.match('^http')) {
idCont.html('fetching...');
$.getJSON("http://query.yahooapis.com/v1/public/yql?" +
"q=select%20*%20from%20html%20where%20url%3D%22" +
encodeURIComponent(url) +
"%22&format=xml'&callback=?",
function(data) {
idCont.empty();
if (data.results[0]) {
data = filterData(data.results[0]);
getIDs($('<div>').html(data));
}
}
);
}
}
// clean up the response
function filterData(data) {
data = data.replace(/<?\/body[^>]*>/g, '')
.replace(/[\r|\n]+/g, '')
.replace(/<--[\S\s]*?-->/g, '')
.replace(/<noscript[^>]*>[\S\s]*?<\/noscript>/g, '')
.replace(/<script[^>]*>[\S\s]*?<\/script>/g, '')
.replace(/<script.*\/>/, '');
return data;
}
// scrap the DOM for the user's IDs
function getIDs(elm) {
var IDs = '';
elm.find('.user-info').each(function() {
var id = $(this).find('a:first')[0].href.split('/').reverse()[1];
IDs += id + '<br>';
});
idCont.html(IDs);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input>
<div></div>