golang RAW游戏数据库API接口插件库rawg-sdk-go的使用

golang RAW游戏数据库API接口插件库rawg-sdk-go的使用

RAWG视频游戏数据库Golang客户端

这是一个非官方的RAWG SDK GO库。该库包含与RAWG API交互的方法。

安装

go get github.com/dimuska139/rawg-sdk-go

使用示例

package main

import (
    "context"
    "fmt"
    "net/http"
    "time"
    "github.com/dimuska139/rawg-sdk-go/v3"
)

func main() {
    // 配置RAWG客户端
    config := rawg.Config{
        ApiKey:  "yourapikey", // 您的个人API密钥
        Language: "ru",       // 设置语言
        Rps:      5,          // 设置每秒请求数限制
    }
    
    // 创建RAWG客户端
    client := rawg.NewClient(http.DefaultClient, &config)
    
    // 创建游戏过滤器
    filter := rawg.NewGamesFilter().
        SetSearch("Gta5").      // 搜索GTA5游戏
        SetPage(1).             // 设置页码
        SetPageSize(10).        // 设置每页数量
        ExcludeCollection(1).   // 排除特定集合
        WithoutParents()        // 不包含父游戏
    
    // 设置请求超时
    ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*500)
    defer cancel()
    
    // 获取游戏数据
    data, total, err := client.GetGames(ctx, filter)
    if err != nil {
        fmt.Printf("获取游戏数据失败: %v\n", err)
        return
    }
    
    // 处理返回数据
    fmt.Printf("找到%d个游戏,共%d个结果\n", len(data), total)
    for _, game := range data {
        fmt.Printf("游戏名称: %s, 发布日期: %s\n", game.Name, game.Released)
    }
}

API限制

每个IP每秒只允许5个请求。配置中的"Rps"参数会执行此限制,因此您无需担心。

许可证

RAWG SDK GO根据MIT许可证发布。


更多关于golang RAW游戏数据库API接口插件库rawg-sdk-go的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于golang RAW游戏数据库API接口插件库rawg-sdk-go的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


rawg-sdk-go 使用指南

rawg-sdk-go 是一个用于与 RAWG 游戏数据库 API 交互的 Golang SDK。RAWG 是一个包含超过 50 万款游戏信息的数据库,提供了丰富的游戏数据接口。

安装

首先安装 rawg-sdk-go 包:

go get github.com/dimuska139/rawg-sdk-golang

初始化客户端

package main

import (
	"context"
	"fmt"
	"log"
	"os"

	rawg "github.com/dimuska139/rawg-sdk-golang"
)

func main() {
	// 从环境变量获取API密钥
	apiKey := os.Getenv("RAWG_API_KEY")
	if apiKey == "" {
		log.Fatal("RAWG_API_KEY environment variable not set")
	}

	// 创建客户端配置
	config := rawg.Config{
		APIKey:  apiKey,
		Lang:    "en", // 语言设置
		Timeout: 10,   // 超时时间(秒)
	}

	// 创建客户端
	client, err := rawg.NewClient(config)
	if err != nil {
		log.Fatalf("Error creating RAWG client: %v", err)
	}

	// 使用示例
	ctx := context.Background()
	
	// 获取游戏列表
	games, err := client.GetGames(ctx, &rawg.GamesQueryParams{
		PageSize: 5,
	})
	if err != nil {
		log.Fatalf("Error getting games: %v", err)
	}

	for _, game := range games.Results {
		fmt.Printf("%d: %s (%s)\n", game.ID, game.Name, game.Released)
	}
}

常用功能示例

1. 获取游戏详情

func getGameDetails(client *rawg.Client, ctx context.Context, gameID int) {
	game, err := client.GetGame(ctx, gameID)
	if err != nil {
		log.Printf("Error getting game details: %v", err)
		return
	}

	fmt.Printf("\nGame Details:\n")
	fmt.Printf("ID: %d\n", game.ID)
	fmt.Printf("Name: %s\n", game.Name)
	fmt.Printf("Released: %s\n", game.Released)
	fmt.Printf("Rating: %.2f\n", game.Rating)
	fmt.Printf("Metacritic: %d\n", game.Metacritic)
	fmt.Printf("Playtime: %d hours\n", game.Playtime)
	fmt.Printf("Description: %s\n", game.DescriptionRaw)
}

2. 搜索游戏

func searchGames(client *rawg.Client, ctx context.Context, query string) {
	games, err := client.GetGames(ctx, &rawg.GamesQueryParams{
		Search:   query,
		PageSize: 3,
	})
	if err != nil {
		log.Printf("Error searching games: %v", err)
		return
	}

	fmt.Printf("\nSearch Results for '%s':\n", query)
	for _, game := range games.Results {
		fmt.Printf("- %s (ID: %d, Rating: %.2f)\n", game.Name, game.ID, game.Rating)
	}
}

3. 获取游戏截图

func getGameScreenshots(client *rawg.Client, ctx context.Context, gameID int) {
	screenshots, err := client.GetGameScreenshots(ctx, gameID, &rawg.PaginationParams{
		PageSize: 3,
	})
	if err != nil {
		log.Printf("Error getting screenshots: %v", err)
		return
	}

	fmt.Printf("\nScreenshots:\n")
	for _, screenshot := range screenshots.Results {
		fmt.Printf("- %s\n", screenshot.Image)
	}
}

4. 获取游戏系列

func getGameSeries(client *rawg.Client, ctx context.Context, gameID int) {
	series, err := client.GetGameSeries(ctx, gameID, &rawg.PaginationParams{
		PageSize: 5,
	})
	if err != nil {
		log.Printf("Error getting game series: %v", err)
		return
	}

	fmt.Printf("\nGame Series:\n")
	for _, game := range series.Results {
		fmt.Printf("- %s (ID: %d)\n", game.Name, game.ID)
	}
}

高级用法

分页处理

func listAllGames(client *rawg.Client, ctx context.Context) {
	page := 1
	pageSize := 50
	totalGames := 0

	for {
		games, err := client.GetGames(ctx, &rawg.GamesQueryParams{
			Page:     page,
			PageSize: pageSize,
		})
		if err != nil {
			log.Printf("Error getting games page %d: %v", page, err)
			break
		}

		if len(games.Results) == 0 {
			break
		}

		totalGames += len(games.Results)
		fmt.Printf("Page %d: %d games (Total: %d)\n", page, len(games.Results), totalGames)

		// 处理当前页的游戏数据
		for _, game := range games.Results {
			// 这里可以添加你的处理逻辑
			_ = game
		}

		// 检查是否还有更多页面
		if games.Next == "" {
			break
		}

		page++
	}
}

错误处理

func safeGetGameDetails(client *rawg.Client, ctx context.Context, gameID int) {
	game, err := client.GetGame(ctx, gameID)
	if err != nil {
		if rawgErr, ok := err.(*rawg.Error); ok {
			switch rawgErr.Code {
			case 404:
				fmt.Printf("Game with ID %d not found\n", gameID)
			case 401:
				fmt.Println("Invalid API key")
			case 429:
				fmt.Println("API rate limit exceeded")
			default:
				fmt.Printf("API error: %s (code %d)\n", rawgErr.Message, rawgErr.Code)
			}
		} else {
			fmt.Printf("Network/other error: %v\n", err)
		}
		return
	}

	fmt.Printf("Found game: %s\n", game.Name)
}

总结

rawg-sdk-go 提供了方便的接口来访问 RAWG 游戏数据库。主要功能包括:

  1. 游戏搜索和列表
  2. 游戏详情获取
  3. 相关资源获取(截图、系列、DLC等)
  4. 分页处理
  5. 错误处理

使用前请确保已注册 RAWG API 并获取有效的 API 密钥。注意遵守 API 的速率限制和其他使用条款。

回到顶部