Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8697

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

$
0
0
標題通りの 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/

Viewing all articles
Browse latest Browse all 8697

Trending Articles