Search code examples
javascripthtmlnode.jssteam

How to tell if a STEAM ID is taken using nodejs


I am trying to make a function that can tell me information about a steam page that I can use to tell if the profile was found or not, and perform an action accordingly.

What I came up with is the function below. This tells me a bunch of information about the page and I was hoping that when the steam profile is not found the status would be 404, but there is no difference between a profile that is found and one that isnt.

//GETHTML
function getURLInfo(url) {



   (fetch(url))
                            .then(res => console.log(res))
                            .then(body => console.log(body)) 

      }

THIS IS THE CONSOLE OUTPUT

Response {
  size: 0,
  timeout: 0,
  [Symbol(Body internals)]: {
    body: Gunzip {
      _writeState: [Uint32Array],
      _readableState: [ReadableState],
      readable: true,
      _events: [Object: null prototype],
      _eventsCount: 6,
      _maxListeners: undefined,
      _writableState: [WritableState],
      writable: true,
      allowHalfOpen: true,
      _transformState: [Object],
      _hadError: false,
      bytesWritten: 0,
      _handle: [Zlib],
      _outBuffer: <Buffer 3c 21 44 4f 43 54 59 50 45 20 68 74 6d 6c 3e 0d 0a 3c 68 74 6d 6c 20 63 6c 61 73 73 3d 22 20 72 65 73 70 6f 6e 73 69 76 65 22 20 6c 61 6e 67 3d 22 65 ... 16334 more bytes>,
      _outOffset: 0,
      _chunkSize: 16384,
      _defaultFlushFlag: 2,
      _finishFlushFlag: 2,
      _defaultFullFlushFlag: 3,
      _info: undefined,
      _level: -1,
      _strategy: 0,
      [Symbol(kCapture)]: false
    },
    disturbed: false,
    error: null
  },
  [Symbol(Response internals)]: {
    url: 'https://steamcommunity.com/id/cmm9/',
    status: 200,
    statusText: 'OK',
    headers: Headers { [Symbol(map)]: [Object: null prototype] },
    counter: 0
  }
}

What I thought about trying is creating a function that makes the HTML of the page a long string, and then if the string includes 'Specified profile could not be found.' perform an action, but I don't know how I would do that. I need help on how to do this.


Solution

  • What you should really be using is the Steam Web API. You can register for a key here: https://steamcommunity.com/dev/apikey

    Here's a valid request: https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/?key={KEYGOESHERE}&format=json&steamids=76561198036370701

    {
        "response": {
            "players": [
                {
                    "steamid": "76561198036370701",
                    "communityvisibilitystate": 3,
                    "profilestate": 1,
                    "personaname": "Heavenanvil",
                    "commentpermission": 1,
                    "profileurl": "https://steamcommunity.com/id/heavenanvil/",
                    "avatar": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/d9/d9838736a062d18eb6848ba8d5cc56d72c1cef80.jpg",
                    "avatarmedium": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/d9/d9838736a062d18eb6848ba8d5cc56d72c1cef80_medium.jpg",
                    "avatarfull": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/d9/d9838736a062d18eb6848ba8d5cc56d72c1cef80_full.jpg",
                    "avatarhash": "d9838736a062d18eb6848ba8d5cc56d72c1cef80",
                    "personastate": 0,
                    "realname": "Heaven",
                    "primaryclanid": "103582791433464576",
                    "timecreated": 1294227386,
                    "personastateflags": 0,
                    "loccountrycode": "RU",
                    "locstatecode": "39",
                    "loccityid": 40975
                }
            ]
        }
    }
    

    And when there's no matching steamIds, you get an empty respoonse.

    {
        "response": {
            "players": []
        }
    }
    

    More documentation on the API can be found here: https://partner.steamgames.com/doc/webapi_overview

    Edit: If you want to check to see if there are any players that were returned, just check the response.players array.

    fetch(url) .then(res => res.json()) .then(json => { const { response } = json; if (response.players && response.players.length > 0) { // There are players that were found. Do something. } else { // No players found, do something else. } });

    If you want to check using their VanityURL, you can use this endpoint: http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key={KEYGOESHERE}&format=json&vanityurl=Eclyps19

    Success Response:

    {
        "response": {
            "steamid": "76561197960670951",
            "success": 1
        }
    }
    

    Not Found Respoonse:

    {
        "response": {
            "success": 42,
            "message": "No match"
        }
    }
    

    To check this for a success:

    fetch(url)
        .then(res => res.json())
        .then(json => {
            const { response } = json;
            if (response.success === 1) {
                // Player found. Do something.
            } else {
                // No player found, do something else.
            }
        });