3 个版本

0.1.2 2023 年 3 月 5 日
0.1.1 2023 年 3 月 5 日
0.1.0 2023 年 3 月 5 日

#896 in 游戏开发

Apache-2.0/MIT

72KB
1.5K SLoC

IGDB Rust 客户端 idgb

Build Status License Cargo Documentation

这是 Carlos Landeras 编写的原始 igbsd-rs 的分支。我已经更新了它,使其与最新的 IGDB API 兼容。

非官方互联网游戏数据库 Rust Api 客户端

使用 igdb,您可以轻松检索与电子游戏相关的信息,例如:

  • 游戏
  • 游戏引擎
  • 游戏年龄评级
  • 系列作品
  • 发行日期
  • 多人游戏信息
  • 平台
  • 玩家视角
  • 视频
  • 艺术作品
  • 封面
  • 屏幕截图
  • 网站

等等!。

使用现成的 客户端方法 或使用 RequestBuilder 构建您自己的 查询 来检索您所需的确切数据。

以下有 代码示例 部分,您可以查看一些示例

IGDB 网站

https://www.igdb.com/

IGDB Api 端点信息网站

https://api-docs.igdb.com/#endpoints

文档

请在此处查看文档: docs

示例

您可以在以下位置找到一些示例代码片段: examples

快速入门

将以下行添加到您的 Cargo.toml

[dependencies]
igdb = "*"

或者如果您已经安装了它,可以使用 cargo add

$ cargo add igdb

端点

igdb-rs 目前支持以下端点

端点 描述
年龄评级 根据各种评级机构的年龄评级
艺术作品 官方艺术作品(分辨率和宽高比可能不同)
角色 电子游戏角色
角色头像 描绘游戏角色的图像
公司 电子游戏公司。包括出版商和开发商
封面 游戏封面艺术
游戏 电子游戏!
游戏引擎 如虚幻引擎等电子游戏引擎
游戏模式 单人游戏、多人游戏等
游戏视频 与游戏相关的视频
系列作品 如星球大战等电子游戏系列列表
多人游戏模式 关于支持的多人游戏类型的数据
平台 运行游戏或游戏分发网络的硬件
平台标志 平台标志
玩家视角 玩家视角描述了玩家在视频游戏中的视图/视角
发布日期 一个方便的端点,可以扩展游戏发布日期。用于深入了解发布日期、平台和版本。
屏幕截图 游戏截图
主题 视频游戏主题
网站 网站 URL,通常与游戏相关

注意:客户端会自动通过宏生成,因此添加新端点非常简单。如果您需要添加其他端点,请随时请求或与提交拉取请求的库合作。

代码示例

使用 igdb,您可以轻松查询互联网游戏数据库。

您只需要使用您的 client_id 和 token 创建一个 IGDBClient 对象。您可以在此处注册并生成一个:[https://api-docs.igdb.com/#account-creation](https://api-docs.igdb.com/#account-creation)

运行示例

您可以在以下位置找到一些示例代码片段: examples

In order to run the samples, just set the IGDB_CLIENT_ID and IGDB_TOKEN
environment variables with your client_id and token key and execute it by
using the sample name specified inside Cargo.toml file.

Example:

cargo run --example search-games-by-name
cargo run --example game-video-urls

使用您的用户密钥创建 IGDBClient

    let igdb_client = IGDBClient::new("client_id", "token");

按名称查找游戏

    let games_client = IGDBClient::new("client_id", "token").games();
    let game = games_client.get_first_by_name("Witcher 3").await.unwrap();

    println!("Name: {}", game.name);
    println!("Summary: {} ...", &game.summary[..150]);
    println!("Story line: {}", game.storyline);
    println!("Url: {}", game.url);

    //  Name: The Witcher 3: Wild Hunt - Hearts of Stone
    //  Summary: Hired by the Merchant of Mirrors, Geralt is tasked with overcoming Olgierd von Everec -- a ruthless bandit captain enchanted with the power of immorta ...
    //  Story line: Professional monster slayer is hired to deal with a ruthless bandit captain who possesses the power of immortality.
    //  Url: https://www.igdb.com/games/the-witcher-3-wild-hunt-hearts-of-stone

按名称查找游戏

    let games_client = IGDBClient::new("client_id", "token").games();
        // Get ten first results containing Borderlands in it's name
    let games_results = games_client.get_by_name("Borderlands", 10).await.unwrap();

    for game in games_results {
        println!("Name: {}", game.name);
        println!("Story line: {}", game.storyline);
        println!("Url: {}", game.url);
    }

    //  Name: Borderlands: The Pre-Sequel - Lady Hammerlock The Baroness
    //  Story line:
    //  Url: https://www.igdb.com/games/borderlands-the-pre-sequel-lady-hammerlock-the-baroness
    //  Name: Borderlands Legends
    //  Story line:
    //  Url: https://www.igdb.com/games/borderlands-legends
    //  Name: Tales from the Borderlands: Episode 3 - Catch a Ride
    //  Story line:
    //  Url: https://www.igdb.com/games/tales-from-the-borderlands-episode-3-catch-a-ride
    //  Name: Borderlands 2: Game of the Year Edition
    //  Story line:
    //  Url: https://www.igdb.com/games/borderlands-2-game-of-the-year-edition

    //Omitted for brevity...

游戏角色

    let igdb_client = IGDBClient::new("client_id", "token");

    let characters_client = igdb_client.characters();

    //Get Witcher 3 characters
    for ch in characters_client.get_by_game_id(1942, 10).await.unwrap() {
    println!("name: {}, slug: {}, url: {}", ch.name, ch.slug, ch.url);
    }

    //  name: Dandelion, slug: dandelion, url: https://www.igdb.com/characters/dandelion
    //  name: Jaskier, slug: jaskier, url: https://www.igdb.com/characters/jaskier
    //  name: Emhyr Var Empreis, slug: emhyr-var-empreis, url: https://www.igdb.com/characters/emhyr-var-empreis
    //  name: Ciri, slug: ciri, url: https://www.igdb.com/characters/ciri
    //  name: Avallac'h, slug: avallach, url: https://www.igdb.com/characters/avallach

    //Omitted for brevity...

游戏引擎信息

    let igdb_client = IGDBClient::new("client_id", "token");

    let games_client = igdb_client.games();
    let game = games_client
        .get_first_by_name("Always Sometimes Monsters")
        .await
        .unwrap();

    let engine_id = game.game_engines.first().unwrap();

    let engines_client = igdb_client.game_engines();
    let engine = engines_client
        .get_first_by_id(*engine_id as usize)
        .await
        .unwrap();

    println!(
        "name: {}, url: {}, companies: {:?}",
        engine.name, engine.url, engine.companies
    );

    // name: RPG Maker VX Ace, url: https://www.igdb.com/game_engines/rpg-maker-vx-ace, companies: []

游戏发布数据


  let igdb_client = IGDBClient::new("client_id", "token");

  let release_client = igdb_client.release_dates();

  //Get releases for Borderlands3 with id 19164
  let releases = release_client.get_by_game_id(19164, 10).await.unwrap();

  let platform_client = igdb_client.platforms();

  for release in releases {
    let platform = platform_client
        .get_first_by_id(release.platform as usize)
        .await
        .unwrap();

    println!(
            "platform: {} release date: {}",
            platform.name, release.human
            );
    }

    //  platform: Xbox One release date: 2019-Sep-13
    //  platform: PC (Microsoft Windows) release date: 2019-Sep-13
    //  platform: PlayStation 4 release date: 2019-Sep-13
    //  platform: Google Stadia release date: 2019-Sep-13

游戏视频 URL

    let igdb_client = IGDBClient::new("client_id", "token");
    let videos_client = igdb_client.game_videos();

    //Query first 8 youtube videos for Witcher 3
    let response = videos_client.get_by_game_id(1942, 8).await.unwrap();

    for video in response {
    println!("{:?}", video);
    }

    // Youtube links for Witcher 3 Game

    //  GameVideo { id: 5993, game: 1942, video_id: "xQGam9OHSUo" }
    //  GameVideo { id: 5989, game: 1942, video_id: "_IBAovRNCuA" }
    //  GameVideo { id: 5995, game: 1942, video_id: "8ZLfJjlZKvc" }
    //  GameVideo { id: 5987, game: 1942, video_id: "5nLipy-Z4yo" }
    //  GameVideo { id: 5991, game: 1942, video_id: "6f8TbvsZ5Mk" }
    //  GameVideo { id: 5994, game: 1942, video_id: "p14dHAwLOmo" }
    //  GameVideo { id: 5990, game: 1942, video_id: "QrwGXAcE6ZA" }
    //  GameVideo { id: 5996, game: 1942, video_id: "sb81f-ejNSI" }

截图和封面下载


    let igdb_client = IGDBClient::new("client_id", "token");
    let games_client = igdb_client.games();
    let witcher = games_client.get_first_by_name("Witcher 3").await.unwrap();

    //Get the first 3 covers for the Witcher 3 game
    let covers_client = igdb_client.covers();
    let covers_response = covers_client.get_by_game_id(witcher.id, 3).await.unwrap();

    //Get the first 3 screenshots for the Witcher 3 game
    let screenshots_client = igdb_client.screenshots();
    let screenshots_response = screenshots_client
        .get_by_game_id(witcher.id, 3)
        .await
        .unwrap();

    for (i, cover) in covers_response.iter().enumerate() {
        covers_client
            .download_by_id(
                cover.id,
                format!("cover{}.jpg", i),
                MediaQuality::ScreenshotHuge,
            )
            .await
            .unwrap();
    }

    for (i, screenshot) in screenshots_response.iter().enumerate() {
        screenshots_client
        .download_by_id(
                screenshot.id,
                format!("screenshot{}.jpg", i),
                MediaQuality::ScreenshotHuge,
            )
            .await
            .unwrap();
    }

自定义查询 I

查找满足请求构建器标准的游戏


    let igdb_client = IGDBClient::new("client_id", "token");

    let mut game_request = IGDBClient::create_request();
    game_request
        .add_field("name")
        .add_fields(vec!["storyline", "summary"])
        .contains("name", "Ast")
        .add_where("category", Equality::NotEqual, "0")
        .sort_by("name", OrderBy::Descending)
        .limit(3);

    // Generated query
    // fields name,storyline,summary; where name  ~ *"Ast"* & category != 0; sort name desc; limit 3;

    let game_client = igdb_client.games();
    let games = game_client.get(game_request).await.unwrap();

    for g in games {
        println!("Name: {}", g.name);
    }

    //Name: Yo-Kai Watch Blasters: Moon Rabbit Crew
    //Name: XCOM 2: Shen's Last Gift
    //Name: Warlock: Master of the Arcane - Armageddon

自定义查询 II

通过构建自定义查询获取《边境之地 2》的多玩家信息


    let idbg_client = IGDBClient::new("client_id", "token");

    let games_client = idbg_client.games();

    let mut games_req = IGDBClient::create_request();
    games_req
        .add_field("id")
        .add_field("name")
        .search("Borderlands 2")
        .limit(3);

    let result = games_client.get(games_req).await.unwrap();

    let ids: Vec<String> = result.iter().map(|g| g.id.to_string()).collect();
    let names: Vec<String> = result.iter().map(|g| g.name.clone()).collect();

    let multiplayer_client = idbg_client.multiplayer_modes();

    let mut mul_request = IGDBClient::create_request();

    mul_request.all_fields().add_where_in("id".to_owned(), ids);

    let results = multiplayer_client.get(mul_request).await.unwrap();

    for (i, m) in results.iter().enumerate() {
        println!("{} has online coop: {}", names[i], m.onlinecoop);
        println!("{} has local coop: {}", names[i], m.lancoop);
    }

    //  Prints:
    //  Borderlands 2 has online coop: true
    //  Borderlands 2 has local coop: false

游戏评分和投票

    let igdb_client = IGDBClient::new("client_id", "token");
    let games_client = igdb_client.games();

    let game = games_client.get_first_by_name("Modern Warfare 3").await.unwrap();

    println!("Game: {}, rating: {}, total votes: {}", game.name, game.total_rating as usize, game.total_rating_count);

    //Game: Call of Duty: Modern Warfare 3, rating: 80, total votes: 442

系列游戏


    let igdb_client = IGDBClient::new("client_id", "token");
    let franchises_client = igdb_client.franchises();
    let games_client = igdb_client.games();

    //Get games inside franchises containing name "Lego"
    for franchise in franchises_client
        .get_by_name("Lego", 5)
        .await
        .unwrap()
    {
        for game in &franchise.games {
            let game_info = games_client.get_first_by_id(*game as usize).await.unwrap();

            println!("Name: {}", game_info.name);
        }
    }

    // Name: Lego Indiana Jones 2: The Adventure Continues
    // Name: Lego Indiana Jones: The Original Adventures
    // Name: LEGO Star Wars II: The Original Trilogy
    // Name: Lego Racers 2
    // Name: LEGO Racers
    // Omitted for brevity...

游戏年龄分级

    let igdb_client = IGDBClient::new("client_id", "token");
    let games_client = igdb_client.games();
    let age_rating_client = igdb_client.age_ratings();

    let game = games_client
        .get_first_by_name("Call of Duty: Modern Warfare 3")
        .await
        .unwrap();

    for age_rating in game.age_ratings {

        //Get a maximum of 3 age ratings for Modern Warfare 3

        let ratings = age_rating_client
            .get_by_id(age_rating as usize, 3)
            .await
            .unwrap();

        for rating in ratings {
            println!(
                "Game: {}, Category: {:?}, Rating: {:?}",
                game.name, rating.category, rating.rating
            );
        }

    // Game: Call of Duty: Modern Warfare 3, Category: PEGI, Rating: Eighteen
    }

依赖项

~10–24MB
~385K SLoC