0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Steam Web API で所有ゲームのジャンルを順位付けする

Posted at

a.png
標題通りの NodeJS スクリプトを作ったので投稿しておきます。
本来はそういうウェブサービスを作ろうと思ったんですが、Steam Web APIはブラウザから利用できないことを知り、あきらめた次第です(いちいちサーバ建てたくない病)。

// `npm i --save node-fetch`
const fetch = require('node-fetch')

// Steam Web APIキー
// SEE: https://steamcommunity.com/dev/apikey
const steamWebApiKey = ''

// Steamニックネーム(e.g. `mimonelu`)
const nickname = ''

// 計上対象となる最短プレイ時間
const minPlayTime = 60

// Promise版JSON fetch
const fetchJson = (url) => new Promise((resolve, reject) => {
  console.log(`Access: ${url}`)
  fetch(url)
    .then((res) => res.json())
    .then((json) => {
      resolve(json)
    })
})

const main = async () => {
  // ニックネームからSteam IDを取得
  const userJson = await fetchJson(`https://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=${steamWebApiKey}&vanityurl=${nickname}`)
  const steamId = userJson.response.steamid

  // 所有アプリケーションを取得
  const appListJson = await fetchJson(`http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=${steamWebApiKey}&steamid=${steamId}`)

  // 計上対象となるアプリケーションIDリストを生成
  const appIdList = appListJson.response.games
    .filter((app) => app.playtime_forever >= minPlayTime)
    .map((app) => app.appid)

  const genreObject = {}
  for (const appId of appIdList) {
    // アプリケーションのジャンルを取得
    // NOTICE: 情報量が大きすぎるので `filters=genres` で必要な情報のみ取得している 
    // NOTICE: `appids=111,222,...` というように複数のIDを指定可能だが機能しない模様
    const appDetailObjectJson = await fetchJson(`http://store.steampowered.com/api/appdetails?appids=${appId}&filters=genres`)

    if (appDetailObjectJson[appId].data.genres !== undefined) {
      appDetailObjectJson[appId].data.genres.forEach((genre) => {
        if (genreObject[genre.id] === undefined) {
          genreObject[genre.id] = {
            ...genre,
            count: 1,
          }
        } else {
          genreObject[genre.id].count ++
        }
      })
    }
  }

  // ジャンル配列の生成とソート
  const genreList = Object.values(genreObject)
  genreList.sort((a, b) => a.count < b.count ? 1 : (a.count > b.count ? - 1 : 0))

  // 出力
  console.log('- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -')
  console.log(`Nickname: ${nickname}
Number of owned games: ${appListJson.response.games.length}
Number of owned games (played over ${minPlayTime} minutes): ${appIdList.length}
Results:`)
  console.log(genreList)
  console.log('- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -')
}

main()

ポイントとしては、

  • API キーが必要になるので、 https://steamcommunity.com/dev/apikey から取得してください。ドメインはご自由に。
  • 最短プレイ時間を指定可能です。10分くらいしかプレイしていないゲームを計上しても意味がないので…。
  • このままではデモ版とかデザインツールとかも含まれてしまいますが、そのあたりはがんばってください。

くらいです。

あ、あと全然関係ないんですが、Steamではよくレビューを投稿したりしているのでよろしくお願いします(?)
https://steamcommunity.com/id/mimonelu/

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?