1

我一直在尝试使用 riot games api 来计算所有以前的自定义游戏,然后找到单个玩家的连胜记录,我构建了以下代码来获取特定用户的匹配。

请参阅https://github.com/FriendlyUser/deno-riot-games-custom-games

但我觉得 riot games api 只返回第 11 季之前的 v4 api 数据,如果有人能澄清 api 的工作原理或解释我如何获得更多数据,那就太好了。

import { writeJson } from "https://deno.land/std/fs/mod.ts"
import "https://deno.land/x/dotenv/load.ts"

const player_id = Deno.env.get('ACCOUNT_ID')
const region_url = 'https://na1.api.riotgames.com'
let riot_URL = new URL(`${region_url}/lol/match/v4/matchlists/by-account/${player_id}`)

enum HTTP {
  GET = 'GET',
  POST = 'POST',
  PUT = 'PUT',
  DELETE = 'DELETE'
}

interface MatchlistDto {
  startIndex: number
  totalGames: number
  endIndex: number
  matches: Array<any>
}

function makeFetchOptions(
    riotKey = Deno.env.get('RIOT_API_KEY'),
    method: HTTP = HTTP.GET
): object {
  return {
      method: method,
      headers: { 
        "Accept-Charset": "application/x-www-form-urlencoded; charset=UTF-8",
        "Accept-Language":  "en-US,en;q=0.9",
        'X-Riot-Token': riotKey
      }
  }
}

function appendMatchHistory(riot_endpoint: string): Promise<MatchlistDto> {
  const riotKey = Deno.env.get('RIOT_API_KEY')
  console.log(riotKey)
  const options = makeFetchOptions(riotKey)
  return fetch(riot_endpoint, options)
  .then( (resp: any) => {
    console.log(resp)
    return resp.json() 
  })
  .then( (matchData: MatchlistDto) => {
    return matchData
  })
}

const max_iterations = 1000
let bIndex = 0
let eIndex = 100
let current_url = riot_URL
let riot_endpoint = null
let allMatches = []
let customGames = []

const sleep = (milliseconds: number) => {
  return new Promise(resolve => setTimeout(resolve, milliseconds))
}

for (let i = 0; i < max_iterations; i++) {
    console.log(`beginIndex: ${bIndex} endIndex: ${eIndex}`)
    riot_endpoint = current_url.toString()
    const newMatches = await appendMatchHistory(riot_endpoint)
    await sleep(1500)
    current_url.searchParams.delete('beginIndex')
    current_url.searchParams.delete('endIndex')
    const {matches} = newMatches
    if (matches.length == 0) {
      console.log(`ENDING SCRIPT AT ${eIndex} with ${matches.length}`)
      break
    }
    // startIndex becomes endIndex
    bIndex = eIndex
    eIndex = eIndex + 100
    allMatches.push(newMatches.matches)

    // get new url
    current_url.searchParams.append('beginIndex', String(bIndex))
    current_url.searchParams.append('endIndex', String(eIndex))
}

await writeJson(
  "./allData.json",
  allMatches
);
4

1 回答 1

0

对不起,如果这个答案迟到了。但是是的,Riot API 仅用于“当前”数据,这就是为什么像 U.GG、OP.GG 等网站实际上运行脚本来连续存储数据的原因。因此,要获得统计信息,您必须编写脚本将其存储到您自己的数据库中。

可悲的是,没有办法获得上赛季的数据

于 2020-08-09T01:32:27.027 回答