【问题标题】:How to tell if a STEAM ID is taken using nodejs如何判断是否使用 nodejs 获取了 STEAM ID
【发布时间】:2020-05-30 16:48:34
【问题描述】:

我正在尝试制作一个可以告诉我有关蒸汽页面的信息的功能,我可以使用该功能来判断是否找到了个人资料,并执行相应的操作。

我想出的是下面的函数。这告诉我有关该页面的大量信息,我希望当未找到 Steam 配置文件时状态为 404,但找到的配置文件与未找到的配置文件之间没有区别。

//GETHTML
function getURLInfo(url) {



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

      }

这是控制台输出

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
  }
}

我想尝试的是创建一个函数,使页面的 HTML 成为一个长字符串,然后如果该字符串包含“找不到指定的配置文件”。执行一个动作,但我不知道我会怎么做。我需要有关如何执行此操作的帮助。

【问题讨论】:

标签: javascript html node.js steam


【解决方案1】:

您真正应该使用的是 Steam Web API。您可以在这里注册一个密钥:https://steamcommunity.com/dev/apikey

这是一个有效的请求: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
            }
        ]
    }
}

当没有匹配的 steamIds 时,您会得到一个空响应。

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

更多关于 API 的文档可以在这里找到:https://partner.steamgames.com/doc/webapi_overview

编辑:如果您想查看是否有返回的玩家,只需检查response.players 数组。

获取(网址) .then(res => res.json()) .then(json => { 常量 { 响应 } = json; if (response.players && response.players.length > 0) { // 有找到的玩家。做点什么。 } 别的 { // 没有找到玩家,做点别的。 } });

如果您想使用他们的 VanityURL 进行检查,可以使用此端点:http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key={KEYGOESHERE}&format=json&vanityurl=Eclyps19

成功响应:

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

未找到响应:

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

检查是否成功:

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.
        }
    });

【讨论】:

  • 感谢您的回答!我也可以使用 profileurl 搜索吗?另外,如果我想在响应为空时执行操作怎么办?
  • 更新了我的答案以显示如何检查玩家数组的值。还用正确的 URL 编辑了 API 密钥 URL。
  • 非常感谢!这正是我想要实现的目标,但是有没有办法使用配置文件 url 而不是 steamid 找到相同的信息?只是一个额外的,谢谢!
  • 如果有人想知道,我找到了一种使用配置文件网址获取相同信息的方法! api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/…(将 uservanityurlname 替换为个人资料 url)
  • 我运行了代码并在两个实例上添加了 console.log('this is a test'),输出是这个错误:(node:14088) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'send'未定义的编辑:没关系!这与我的其他错误代码有关
猜你喜欢
  • 2021-10-21
  • 2018-03-04
  • 2012-07-09
  • 1970-01-01
  • 1970-01-01
  • 2010-12-05
  • 2013-12-27
  • 1970-01-01
  • 2021-09-09
相关资源
最近更新 更多