Stackexchange API - get user id by nick

I want to get the user activity above stackoverflow as shown here , but the problem is that it only accepts the user id. I want to do this only by user name, so that anyone can just write your nickname and get statistics. The API doesn't seem to support getting a user ID by providing a nickname.

Is there any workaround to get the user ID by username? Thank.

+3


source to share


1 answer


I hacked my way up stackoverflow/users

by ditching the answer from using YQL to get a page on a cross-domain AJAX. Here's some sample code: http://jsbin.com/teguho/1/edit

Html

<input>
<div></div>

      



Javascript

var idCont = $('div'),
    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 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);
}

      

0


source







All Articles