Steam API JSON function

Posted on

Problem

Upon coming face to face with the mighty cross-origin request block, and needing to get a JSON file from an external source, I decided to use the Yahoo Query Language in order to achieve this.

I’m starting to wonder however whether this code is really the most efficient way of doing it, considering the source can be in JSON, XML and Steams own language.

function getID64(name) {
    var Site = 'https://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=' + key + '&vanityurl=' + name;
    //console.debug("getID64 - " + Site);
    var yql = 'https://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from json where url="' + Site + '"') + '&format=json&callback=?';
    //console.debug("getID64 - " + yql);
    $.getJSON(yql, function(data) {
        app.id64 = data.query.results.response.steamid;
        //console.debug("getID64 - " + app.id64);
        //From here onwards it's just changing some elements on the webpage and starting the next function
        document.getElementById("Steam64ID2").innerHTML += " " + app.id64;
        document.getElementById("Steam64ID").value += "" + app.id64;
        document.getElementById("SteamID2").innerHTML += " " + document.getElementById("SteamID").value;
        getFriendList(document.getElementById('Steam64ID').value);
    });
}

Considering for the first half I am only trying to get and parse the file, is there an easier way of doing this?

Solution

Upon coming face to face with the mighty cross-origin request block, and needing to get a JSON file from an external source, I decided to use the Yahoo Query Language in order to achieve this.

Using YQL just to get a JSON and parse it sounds like a dirty workaround.
I don’t know the right answer about that part.
You might be better off asking on stackoverflow.com instead,
to overcome your original problem without resorting to such workaround.

About the code posted, these are only minor nitpicks:

  • Where does key come from? Perhaps it should be a method parameter
  • User lowercase for variable names (site (or url) instead of Site)
  • You look up the 'Steam64ID' element twice. It would be better to do it once and store in a variable to reuse, for example:

    var steam64ID = document.getElementById("Steam64ID").value += "" + app.id64;
    getFriendList(steam64ID);
    
  • The function is called getID64 which sounds like a getter, so it should return something, but it actually doesn’t.

Leave a Reply

Your email address will not be published. Required fields are marked *