9 Commits

Author SHA1 Message Date
7f5a09098a u 2025-04-05 18:45:17 +11:00
35c27b28a3 u 2025-04-05 16:29:30 +11:00
dab30b4938 fix 2025-04-05 12:18:58 +11:00
4be44c4eb2 add limiter 2025-04-05 11:53:00 +11:00
e01d9805c6 README 2025-04-05 03:37:00 +11:00
76a3e977ba u 2025-04-05 03:30:18 +11:00
a0e24ca14b u 2025-04-05 03:14:04 +11:00
6ae7a18fd9 u 2025-04-05 03:10:28 +11:00
18dc9781b1 u 2025-04-05 02:42:09 +11:00
84 changed files with 2241 additions and 297 deletions

View File

@@ -1,14 +1,61 @@
# go-igdb
a go library to access IGDB API
A Go client library for the IGDB (Internet Game Database) API v4. This library provides a simple and efficient way to interact with IGDB's protobuf-based API.
## Usage
## Features
- Full support for IGDB API v4
- Protobuf-based communication for better performance
- Automatic token management for Twitch authentication
- Built-in retry mechanism for failed requests
- Optional Cloudflare bypass support via FlareSolverr
- All endpoints are supported
## Installation
```bash
go get github.com/bestnite/go-igdb
```
## Quick Start
```go
g := igdb.New("clientID", "clientSecret")
game, err := g.GetGame(325594)
// Create a new IGDB client
client := igdb.New("your-client-id", "your-client-secret")
// Get a game by ID
game, err := client.GetGameByID(1942)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Game: %s\n", game.Name)
// Search games with custom query
games, err := client.GetGames("search \"Zelda\"; fields name,rating,release_dates.*;")
if err != nil {
log.Fatal(err)
}
fmt.Println(game.Name)
```
## Advanced Usage
### Query Format
The library uses IGDB's query syntax. For example:
```go
// Get games released in 2023
games, err := client.GetGames("where release_dates.y = 2023; fields name,rating;")
// Get specific fields for a company
companies, err := client.GetCompanies("where id = 1234; fields name,description,country;")
```
## Requirements
- Go 1.24 or higher
- IGDB/Twitch API credentials (Client ID and Client Secret)
## Contributing
Contributions are welcome! Please feel free to submit a Pull Request.

27
age_rating_categories.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetAgeRatingCategories(query string) ([]*pb.AgeRatingCategory, error) {
resp, err := g.Request("https://api.igdb.com/v4/age_rating_categories.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.AgeRatingCategoryResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Ageratingcategories) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Ageratingcategories, nil
}

View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetAgeRatingContentDescriptions(query string) ([]*pb.AgeRatingContentDescription, error) {
resp, err := g.Request("https://api.igdb.com/v4/age_rating_content_descriptions.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.AgeRatingContentDescriptionResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Ageratingcontentdescriptions) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Ageratingcontentdescriptions, nil
}

View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetAgeRatingContentDescriptionsV2(query string) ([]*pb.AgeRatingContentDescriptionV2, error) {
resp, err := g.Request("https://api.igdb.com/v4/age_rating_content_descriptions_v2.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.AgeRatingContentDescriptionV2Result{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Ageratingcontentdescriptionsv2) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Ageratingcontentdescriptionsv2, nil
}

View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetAgeRatingOrganizations(query string) ([]*pb.AgeRatingOrganization, error) {
resp, err := g.Request("https://api.igdb.com/v4/age_rating_organizations.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.AgeRatingOrganizationResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Ageratingorganizations) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Ageratingorganizations, nil
}

27
age_ratings.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetAgeRatings(query string) ([]*pb.AgeRating, error) {
resp, err := g.Request("https://api.igdb.com/v4/age_ratings.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.AgeRatingResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Ageratings) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Ageratings, nil
}

27
alternative_names.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetAlternativeNames(query string) ([]*pb.AlternativeName, error) {
resp, err := g.Request("https://api.igdb.com/v4/alternative_names.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.AlternativeNameResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Alternativenames) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Alternativenames, nil
}

27
artworks.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetArtworks(query string) ([]*pb.Artwork, error) {
resp, err := g.Request("https://api.igdb.com/v4/artworks.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.ArtworkResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Artworks) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Artworks, nil
}

27
character_genders.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetCharacterGenders(query string) ([]*pb.CharacterGender, error) {
resp, err := g.Request("https://api.igdb.com/v4/character_genders.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.CharacterGenderResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Charactergenders) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Charactergenders, nil
}

27
character_mug_shots.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetCharacterMugShots(query string) ([]*pb.CharacterMugShot, error) {
resp, err := g.Request("https://api.igdb.com/v4/character_mug_shots.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.CharacterMugShotResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Charactermugshots) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Charactermugshots, nil
}

27
character_species.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetCharacterSpecies(query string) ([]*pb.CharacterSpecie, error) {
resp, err := g.Request("https://api.igdb.com/v4/character_species.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.CharacterSpecieResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Characterspecies) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Characterspecies, nil
}

27
characters.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetCharacters(query string) ([]*pb.Character, error) {
resp, err := g.Request("https://api.igdb.com/v4/characters.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.CharacterResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Characters) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Characters, nil
}

View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetCollectionMembershipTypes(query string) ([]*pb.CollectionMembershipType, error) {
resp, err := g.Request("https://api.igdb.com/v4/collection_membership_types.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.CollectionMembershipTypeResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Collectionmembershiptypes) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Collectionmembershiptypes, nil
}

27
collection_memberships.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetCollectionMemberships(query string) ([]*pb.CollectionMembership, error) {
resp, err := g.Request("https://api.igdb.com/v4/collection_memberships.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.CollectionMembershipResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Collectionmemberships) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Collectionmemberships, nil
}

View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetCollectionRelationTypes(query string) ([]*pb.CollectionRelationType, error) {
resp, err := g.Request("https://api.igdb.com/v4/collection_relation_types.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.CollectionRelationTypeResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Collectionrelationtypes) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Collectionrelationtypes, nil
}

27
collection_relations.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetCollectionRelations(query string) ([]*pb.CollectionRelation, error) {
resp, err := g.Request("https://api.igdb.com/v4/collection_relations.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.CollectionRelationResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Collectionrelations) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Collectionrelations, nil
}

27
collection_types.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetCollectionTypes(query string) ([]*pb.CollectionType, error) {
resp, err := g.Request("https://api.igdb.com/v4/collection_types.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.CollectionTypeResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Collectiontypes) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Collectiontypes, nil
}

27
collections.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetCollections(query string) ([]*pb.Collection, error) {
resp, err := g.Request("https://api.igdb.com/v4/collections.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.CollectionResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Collections) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Collections, nil
}

28
companies.go Normal file
View File

@@ -0,0 +1,28 @@
package igdb
import (
"errors"
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetCompanies(query string) ([]*pb.Company, error) {
resp, err := g.Request("https://api.igdb.com/v4/companies.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.CompanyResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Companies) == 0 {
return nil, errors.New("no results")
}
return data.Companies, nil
}

View File

@@ -1,30 +0,0 @@
package igdb
import (
"errors"
"fmt"
"github/bestnite/go-igdb/constant"
pb "github/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *igdb) GetIGDBCompany(id uint64) (*pb.Company, error) {
query := fmt.Sprintf(`where id=%d; fields *;`, id)
resp, err := g.Request(constant.IGDBCompaniesURL, query)
if err != nil {
return nil, fmt.Errorf("failed to fetch IGDB company for ID %d: %w", id, err)
}
var data pb.CompanyResult
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal IGDB companies response: %w", err)
}
if len(data.Companies) == 0 {
return nil, errors.New("company not found")
}
return data.Companies[0], nil
}

27
company_logos.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetCompanyLogos(query string) ([]*pb.CompanyLogo, error) {
resp, err := g.Request("https://api.igdb.com/v4/company_logos.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.CompanyLogoResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Companylogos) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Companylogos, nil
}

27
company_statuses.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetCompanyStatuses(query string) ([]*pb.CompanyStatus, error) {
resp, err := g.Request("https://api.igdb.com/v4/company_statuses.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.CompanyStatusResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Companystatuses) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Companystatuses, nil
}

27
company_websites.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetCompanyWebsites(query string) ([]*pb.CompanyWebsite, error) {
resp, err := g.Request("https://api.igdb.com/v4/company_websites.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.CompanyWebsiteResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Companywebsites) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Companywebsites, nil
}

View File

@@ -1,12 +0,0 @@
package constant
const (
IGDBGameURL = "https://api.igdb.com/v4/games.pb"
IGDBCompaniesURL = "https://api.igdb.com/v4/companies.pb"
IGDBWebsitesURL = "https://api.igdb.com/v4/websites.pb"
IGDBExternalGameURL = "https://api.igdb.com/v4/external_games.pb"
IGDBPopularityURL = "https://api.igdb.com/v4/popularity_primitives.pb"
IGDBWebhookURL = "https://api.igdb.com/v4/%s/webhooks/"
IGDBWebSearchURL = "https://www.igdb.com/search"
TwitchAuthURL = "https://id.twitch.tv/oauth2/token"
)

27
covers.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetCovers(query string) ([]*pb.Cover, error) {
resp, err := g.Request("https://api.igdb.com/v4/covers.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.CoverResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Covers) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Covers, nil
}

27
date_formats.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetDateFormats(query string) ([]*pb.DateFormat, error) {
resp, err := g.Request("https://api.igdb.com/v4/date_formats.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.DateFormatResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Dateformats) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Dateformats, nil
}

151
endpoint/endpoint.go Normal file
View File

@@ -0,0 +1,151 @@
package endpoint
type Endpoint string
var (
AgeRatingCategories Endpoint = "age_rating_categories"
AgeRatingContentDescriptions Endpoint = "age_rating_content_descriptions"
AgeRatingContentDescriptionsV2 Endpoint = "age_rating_content_descriptions_v2"
AgeRatingOrganizations Endpoint = "age_rating_organizations"
AgeRatings Endpoint = "age_ratings"
AlternativeNames Endpoint = "alternative_names"
Artworks Endpoint = "artworks"
CharacterGenders Endpoint = "character_genders"
CharacterMugShots Endpoint = "character_mug_shots"
Characters Endpoint = "characters"
CharacterSpecies Endpoint = "character_species"
CollectionMemberships Endpoint = "collection_memberships"
CollectionMembershipTypes Endpoint = "collection_membership_types"
CollectionRelations Endpoint = "collection_relations"
CollectionRelationTypes Endpoint = "collection_relation_types"
Collections Endpoint = "collections"
CollectionTypes Endpoint = "collection_types"
Companies Endpoint = "companies"
CompanyLogos Endpoint = "company_logos"
CompanyStatuses Endpoint = "company_statuses"
CompanyWebsites Endpoint = "company_websites"
Covers Endpoint = "covers"
DateFormats Endpoint = "date_formats"
EventLogos Endpoint = "event_logos"
EventNetworks Endpoint = "event_networks"
Events Endpoint = "events"
ExternalGames Endpoint = "external_games"
ExternalGameSources Endpoint = "external_game_sources"
Franchises Endpoint = "franchises"
GameEngineLogos Endpoint = "game_engine_logos"
GameEngines Endpoint = "game_engines"
GameLocalizations Endpoint = "game_localizations"
GameModes Endpoint = "game_modes"
GameReleaseFormats Endpoint = "game_release_formats"
Games Endpoint = "games"
GameStatuses Endpoint = "game_statuses"
GameTimeToBeats Endpoint = "game_time_to_beats"
GameTypes Endpoint = "game_types"
GameVersionFeatures Endpoint = "game_version_features"
GameVersionFeatureValues Endpoint = "game_version_feature_values"
GameVersions Endpoint = "game_versions"
GameVideos Endpoint = "game_videos"
Genres Endpoint = "genres"
InvolvedCompanies Endpoint = "involved_companies"
Keywords Endpoint = "keywords"
Languages Endpoint = "languages"
LanguageSupports Endpoint = "language_supports"
LanguageSupportTypes Endpoint = "language_support_types"
MultiplayerModes Endpoint = "multiplayer_modes"
NetworkTypes Endpoint = "network_types"
PlatformFamilies Endpoint = "platform_families"
PlatformLogos Endpoint = "platform_logos"
Platforms Endpoint = "platforms"
PlatformTypes Endpoint = "platform_types"
PlatformVersionCompanies Endpoint = "platform_version_companies"
PlatformVersionReleaseDates Endpoint = "platform_version_release_dates"
PlatformVersions Endpoint = "platform_versions"
PlatformWebsites Endpoint = "platform_websites"
PlayerPerspectives Endpoint = "player_perspectives"
PopularityPrimitives Endpoint = "popularity_primitives"
PopularityTypes Endpoint = "popularity_types"
Regions Endpoint = "regions"
ReleaseDateRegions Endpoint = "release_date_regions"
ReleaseDates Endpoint = "release_dates"
ReleaseDateStatuses Endpoint = "release_date_statuses"
Screenshots Endpoint = "screenshots"
Search Endpoint = "search"
Themes Endpoint = "themes"
Webhooks Endpoint = "webhooks"
Websites Endpoint = "websites"
WebsiteTypes Endpoint = "website_types"
)
var AllEndpoints = []Endpoint{
AgeRatingCategories,
AgeRatingContentDescriptions,
AgeRatingContentDescriptionsV2,
AgeRatingOrganizations,
AgeRatings,
AlternativeNames,
Artworks,
CharacterGenders,
CharacterMugShots,
Characters,
CharacterSpecies,
CollectionMemberships,
CollectionMembershipTypes,
CollectionRelations,
CollectionRelationTypes,
Collections,
CollectionTypes,
Companies,
CompanyLogos,
CompanyStatuses,
CompanyWebsites,
Covers,
DateFormats,
EventLogos,
EventNetworks,
Events,
ExternalGames,
ExternalGameSources,
Franchises,
GameEngineLogos,
GameEngines,
GameLocalizations,
GameModes,
GameReleaseFormats,
Games,
GameStatuses,
GameTimeToBeats,
GameTypes,
GameVersionFeatures,
GameVersionFeatureValues,
GameVersions,
GameVideos,
Genres,
InvolvedCompanies,
Keywords,
Languages,
LanguageSupports,
LanguageSupportTypes,
MultiplayerModes,
NetworkTypes,
PlatformFamilies,
PlatformLogos,
Platforms,
PlatformTypes,
PlatformVersionCompanies,
PlatformVersionReleaseDates,
PlatformVersions,
PlatformWebsites,
PlayerPerspectives,
PopularityPrimitives,
PopularityTypes,
Regions,
ReleaseDateRegions,
ReleaseDates,
ReleaseDateStatuses,
Screenshots,
Search,
Themes,
Webhooks,
Websites,
WebsiteTypes,
}

27
event_logos.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetEventLogos(query string) ([]*pb.EventLogo, error) {
resp, err := g.Request("https://api.igdb.com/v4/event_logos.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.EventLogoResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Eventlogos) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Eventlogos, nil
}

27
event_networks.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetEventNetworks(query string) ([]*pb.EventNetwork, error) {
resp, err := g.Request("https://api.igdb.com/v4/event_networks.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.EventNetworkResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Eventnetworks) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Eventnetworks, nil
}

27
events.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetEvents(query string) ([]*pb.Event, error) {
resp, err := g.Request("https://api.igdb.com/v4/events.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.EventResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Events) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Events, nil
}

27
external_game_sources.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetExternalGameSources(query string) ([]*pb.ExternalGameSource, error) {
resp, err := g.Request("https://api.igdb.com/v4/external_game_sources.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.ExternalGameSourceResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Externalgamesources) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Externalgamesources, nil
}

27
external_games.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetExternalGames(query string) ([]*pb.ExternalGame, error) {
resp, err := g.Request("https://api.igdb.com/v4/external_games.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.ExternalGameResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Externalgames) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Externalgames, nil
}

27
franchises.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetFranchises(query string) ([]*pb.Franchise, error) {
resp, err := g.Request("https://api.igdb.com/v4/franchises.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.FranchiseResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Franchises) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Franchises, nil
}

63
game.go
View File

@@ -1,63 +0,0 @@
package igdb
import (
"fmt"
"github/bestnite/go-igdb/constant"
"strconv"
"strings"
pb "github/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *igdb) GetGame(id uint64) (*pb.Game, error) {
resp, err := g.Request(constant.IGDBGameURL, fmt.Sprintf(`where id = %v; fields *, age_ratings.*, alternative_names.*, artworks.*, collection.*, cover.*, external_games.*, external_games.platform.* , franchise.*, game_engines.*, game_engines.logo.*, game_engines.companies.* , game_modes.*, genres.*, involved_companies.*, involved_companies.company.* , keywords.*, multiplayer_modes.*, multiplayer_modes.platform.*, platforms.*, platforms.platform_logo.*, platforms.platform_family.*, platforms.versions.*, platforms.websites.* , player_perspectives.*, release_dates.*, release_dates.platform.*, release_dates.status.* , screenshots.*, themes.*, videos.*, websites.*, language_supports.*, language_supports.language.*, language_supports.language_support_type.* , game_localizations.*, game_localizations.region.* , collections.*, collections.type.*, collections.as_parent_relations.child_collection.*, collections.as_parent_relations.parent_collection.*, collections.as_parent_relations.type.*,collections.as_child_relations.child_collection.*, collections.as_child_relations.parent_collection.*, collections.as_child_relations.type.*, age_ratings.content_descriptions.*, cover.game_localization.*;`, id))
if err != nil {
return nil, fmt.Errorf("failed to fetch game detail for ID %d: %w", id, err)
}
res := pb.GameResult{}
if err = proto.Unmarshal(resp.Body(), &res); err != nil {
return nil, fmt.Errorf("failed to unmarshal game detail response: %w", err)
}
if len(res.Games) == 0 {
return nil, fmt.Errorf("failed to fetch game detail for ID %d", id)
}
if res.Games[0].Name == "" {
return g.GetGame(id)
}
return res.Games[0], nil
}
func (g *igdb) GetGames(ids []uint64) ([]*pb.Game, error) {
idStrSlice := make([]string, len(ids))
for i, id := range ids {
idStrSlice[i] = strconv.FormatUint(id, 10)
}
idStr := strings.Join(idStrSlice, ",")
resp, err := g.Request(constant.IGDBGameURL, fmt.Sprintf(`where id = (%s); fields *, age_ratings.*, alternative_names.*, artworks.*, collection.*, cover.*, external_games.*, external_games.platform.* , franchise.*, game_engines.*, game_engines.logo.*, game_engines.companies.* , game_modes.*, genres.*, involved_companies.*, involved_companies.company.* , keywords.*, multiplayer_modes.*, multiplayer_modes.platform.*, platforms.*, platforms.platform_logo.*, platforms.platform_family.*, platforms.versions.*, platforms.websites.* , player_perspectives.*, release_dates.*, release_dates.platform.*, release_dates.status.* , screenshots.*, themes.*, videos.*, websites.*, language_supports.*, language_supports.language.*, language_supports.language_support_type.* , game_localizations.*, game_localizations.region.* , collections.*, collections.type.*, collections.as_parent_relations.child_collection.*, collections.as_parent_relations.parent_collection.*, collections.as_parent_relations.type.*,collections.as_child_relations.child_collection.*, collections.as_child_relations.parent_collection.*, collections.as_child_relations.type.*, age_ratings.content_descriptions.*, cover.game_localization.*;`, idStr))
if err != nil {
return nil, fmt.Errorf("failed to fetch IGDB games detail for ID %s: %w", idStr, err)
}
res := pb.GameResult{}
if err = proto.Unmarshal(resp.Body(), &res); err != nil {
return nil, fmt.Errorf("failed to unmarshal IGDB games detail response: %w", err)
}
if len(res.Games) == 0 {
return nil, fmt.Errorf("failed to fetch IGDB games detail for ID %s", idStr)
}
if res.Games[0].Name == "" {
return g.GetGames(ids)
}
return res.Games, nil
}

27
game_engine_logos.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetGameEngineLogos(query string) ([]*pb.GameEngineLogo, error) {
resp, err := g.Request("https://api.igdb.com/v4/game_engine_logos.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.GameEngineLogoResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Gameenginelogos) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Gameenginelogos, nil
}

27
game_engines.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetGameEngines(query string) ([]*pb.GameEngine, error) {
resp, err := g.Request("https://api.igdb.com/v4/game_engines.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.GameEngineResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Gameengines) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Gameengines, nil
}

27
game_localizations.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetGameLocalizations(query string) ([]*pb.GameLocalization, error) {
resp, err := g.Request("https://api.igdb.com/v4/game_localizations.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.GameLocalizationResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Gamelocalizations) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Gamelocalizations, nil
}

27
game_modes.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetGameModes(query string) ([]*pb.GameMode, error) {
resp, err := g.Request("https://api.igdb.com/v4/game_modes.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.GameModeResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Gamemodes) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Gamemodes, nil
}

27
game_release_formats.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetGameReleaseFormats(query string) ([]*pb.GameReleaseFormat, error) {
resp, err := g.Request("https://api.igdb.com/v4/game_release_formats.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.GameReleaseFormatResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Gamereleaseformats) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Gamereleaseformats, nil
}

27
game_statuses.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetGameStatuses(query string) ([]*pb.GameStatus, error) {
resp, err := g.Request("https://api.igdb.com/v4/game_statuses.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.GameStatusResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Gamestatuses) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Gamestatuses, nil
}

27
game_time_to_beats.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetGameTimeToBeats(query string) ([]*pb.GameTimeToBeat, error) {
resp, err := g.Request("https://api.igdb.com/v4/game_time_to_beats.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.GameTimeToBeatResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Gametimetobeats) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Gametimetobeats, nil
}

27
game_types.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetGameTypes(query string) ([]*pb.GameType, error) {
resp, err := g.Request("https://api.igdb.com/v4/game_types.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.GameTypeResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Gametypes) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Gametypes, nil
}

View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetGameVersionFeatureValues(query string) ([]*pb.GameVersionFeatureValue, error) {
resp, err := g.Request("https://api.igdb.com/v4/game_version_feature_values.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.GameVersionFeatureValueResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Gameversionfeaturevalues) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Gameversionfeaturevalues, nil
}

27
game_version_features.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetGameVersionFeatures(query string) ([]*pb.GameVersionFeature, error) {
resp, err := g.Request("https://api.igdb.com/v4/game_version_features.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.GameVersionFeatureResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Gameversionfeatures) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Gameversionfeatures, nil
}

27
game_versions.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetGameVersions(query string) ([]*pb.GameVersion, error) {
resp, err := g.Request("https://api.igdb.com/v4/game_versions.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.GameVersionResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Gameversions) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Gameversions, nil
}

27
game_videos.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetGameVideos(query string) ([]*pb.GameVideo, error) {
resp, err := g.Request("https://api.igdb.com/v4/game_videos.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.GameVideoResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Gamevideos) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Gamevideos, nil
}

55
games.go Normal file
View File

@@ -0,0 +1,55 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetGames(query string) ([]*pb.Game, error) {
resp, err := g.Request("https://api.igdb.com/v4/games.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.GameResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Games) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Games, nil
}
func (g *Client) GetParentGameID(id uint64) (uint64, error) {
detail, err := GetItemByID(id, g.GetGames)
if err != nil {
return 0, fmt.Errorf("failed to fetch IGDB app detail for parent: %d: %w", id, err)
}
hasParent := false
if detail.ParentGame != nil && detail.ParentGame.Id != 0 {
hasParent = true
detail, err = GetItemByID(detail.ParentGame.Id, g.GetGames)
if err != nil {
return 0, fmt.Errorf("failed to fetch IGDB version parent: %d: %w", detail.VersionParent.Id, err)
}
}
for detail.VersionParent != nil && detail.VersionParent.Id != 0 {
hasParent = true
detail, err = GetItemByID(detail.VersionParent.Id, g.GetGames)
if err != nil {
return 0, fmt.Errorf("failed to fetch IGDB version parent: %d: %w", detail.VersionParent.Id, err)
}
}
if hasParent {
return detail.Id, nil
}
return id, nil
}

27
genres.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetGenres(query string) ([]*pb.Genre, error) {
resp, err := g.Request("https://api.igdb.com/v4/genres.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.GenreResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Genres) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Genres, nil
}

2
go.mod
View File

@@ -1,4 +1,4 @@
module github/bestnite/go-igdb
module github.com/bestnite/go-igdb
go 1.24.1

49
id.go
View File

@@ -1,49 +0,0 @@
package igdb
import (
"errors"
"fmt"
"github/bestnite/go-igdb/constant"
pb "github/bestnite/go-igdb/proto"
"strconv"
"google.golang.org/protobuf/proto"
)
func (g *igdb) GetGameIDBySteamAppID(id uint64) (uint64, error) {
query := fmt.Sprintf(`where game_type.id = 0 & uid = "%d"; fields game;`, id)
resp, err := g.Request(constant.IGDBExternalGameURL, query)
if err != nil {
return 0, fmt.Errorf("failed to fetch IGDB ID by Steam App ID %d: %w", id, err)
}
res := pb.ExternalGameResult{}
if err = proto.Unmarshal(resp.Body(), &res); err != nil {
return 0, fmt.Errorf("failed to unmarshal IGDB response for Steam App ID %d: %w", id, err)
}
if len(res.Externalgames) == 0 || res.Externalgames[0].Game.Id == 0 {
return 0, errors.New("no matching IGDB game found")
}
return res.Externalgames[0].Game.Id, nil
}
func (g *igdb) GetSteamIDByGameID(id uint64) (uint64, error) {
query := fmt.Sprintf(`where game = %v & game_type.id = 0; fields *;`, id)
resp, err := g.Request(constant.IGDBExternalGameURL, query)
if err != nil {
return 0, fmt.Errorf("failed to fetch IGDB websites for IGDB ID %d: %w", id, err)
}
res := pb.ExternalGameResult{}
if err := proto.Unmarshal(resp.Body(), &res); err != nil {
return 0, fmt.Errorf("failed to unmarshal IGDB websites response for IGDB ID %d: %w", id, err)
}
if len(res.Externalgames) == 0 {
return 0, errors.New("steam ID not found")
}
return strconv.ParseUint(res.Externalgames[0].Uid, 10, 64)
}

58
igdb.go
View File

@@ -2,37 +2,43 @@ package igdb
import (
"fmt"
"strings"
"github.com/bestnite/go-flaresolverr"
"github.com/go-resty/resty/v2"
)
type igdb struct {
type Client struct {
clientID string
token *twitchToken
flaresolverr *flaresolverr.Flaresolverr
limiter *rateLimiter
}
func New(clientID, clientSecret string) *igdb {
return &igdb{
func New(clientID, clientSecret string) *Client {
return &Client{
clientID: clientID,
limiter: newRateLimiter(4),
token: NewTwitchToken(clientID, clientSecret),
flaresolverr: nil,
}
}
func NewWithFlaresolverr(clientID, clientSecret string, f *flaresolverr.Flaresolverr) *igdb {
return &igdb{
func NewWithFlaresolverr(clientID, clientSecret string, f *flaresolverr.Flaresolverr) *Client {
return &Client{
clientID: clientID,
limiter: newRateLimiter(4),
token: NewTwitchToken(clientID, clientSecret),
flaresolverr: f,
}
}
func (g *igdb) Request(URL string, dataBody any) (*resty.Response, error) {
func (g *Client) Request(URL string, dataBody any) (*resty.Response, error) {
g.limiter.wait()
t, err := g.token.getToken()
if err != nil {
return nil, fmt.Errorf("failed to get Twitch token: %w", err)
return nil, fmt.Errorf("failed to get twitch token: %w", err)
}
resp, err := request().SetBody(dataBody).SetHeaders(map[string]string{
@@ -43,14 +49,48 @@ func (g *igdb) Request(URL string, dataBody any) (*resty.Response, error) {
}).Post(URL)
if err != nil {
return nil, fmt.Errorf("failed to make request: %s: %w", URL, err)
return nil, fmt.Errorf("failed to request: %s: %w", URL, err)
}
return resp, nil
}
func (g *igdb) getFlaresolverr() (*flaresolverr.Flaresolverr, error) {
func (g *Client) getFlaresolverr() (*flaresolverr.Flaresolverr, error) {
if g.flaresolverr == nil {
return nil, fmt.Errorf("flaresolverr is not initialized")
}
return g.flaresolverr, nil
}
func GetItemByID[T any](id uint64, f func(string) ([]*T, error)) (*T, error) {
query := fmt.Sprintf("where id = %d; fields *;", id)
items, err := f(query)
if err != nil {
return nil, err
}
return items[0], nil
}
func GetItemsByIDs[T any](ids []uint64, f func(string) ([]*T, error)) ([]*T, error) {
idStrSlice := make([]string, len(ids))
for i, id := range ids {
idStrSlice[i] = fmt.Sprintf("%d", id)
}
idStr := fmt.Sprintf(`where id = (%s); fields *;`, strings.Join(idStrSlice, ","))
return f(idStr)
}
func GetItemsPagniated[T any](offset, limit int, f func(string) ([]*T, error)) ([]*T, error) {
query := fmt.Sprintf("offset %d; limit %d; f *; sort id asc;", offset, limit)
return f(query)
}
func GetItemsLength[T any](f func(string) ([]*T, error)) (int, error) {
query := "fields id; sort id desc; limit 1;"
items, err := f(query)
if err != nil {
return 0, err
}
return len(items), nil
}

27
involved_companies.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetInvolvedCompanies(query string) ([]*pb.InvolvedCompany, error) {
resp, err := g.Request("https://api.igdb.com/v4/involved_companies.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.InvolvedCompanyResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Involvedcompanies) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Involvedcompanies, nil
}

27
keywords.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetKeywords(query string) ([]*pb.Keyword, error) {
resp, err := g.Request("https://api.igdb.com/v4/keywords.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.KeywordResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Keywords) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Keywords, nil
}

27
language_support_types.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetLanguageSupportTypes(query string) ([]*pb.LanguageSupportType, error) {
resp, err := g.Request("https://api.igdb.com/v4/language_support_types.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.LanguageSupportTypeResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Languagesupporttypes) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Languagesupporttypes, nil
}

27
language_supports.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetLanguageSupports(query string) ([]*pb.LanguageSupport, error) {
resp, err := g.Request("https://api.igdb.com/v4/language_supports.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.LanguageSupportResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Languagesupports) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Languagesupports, nil
}

27
languages.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetLanguages(query string) ([]*pb.Language, error) {
resp, err := g.Request("https://api.igdb.com/v4/languages.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.LanguageResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Languages) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Languages, nil
}

27
multiplayer_modes.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetMultiplayerModes(query string) ([]*pb.MultiplayerMode, error) {
resp, err := g.Request("https://api.igdb.com/v4/multiplayer_modes.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.MultiplayerModeResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Multiplayermodes) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Multiplayermodes, nil
}

27
network_types.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetNetworkTypes(query string) ([]*pb.NetworkType, error) {
resp, err := g.Request("https://api.igdb.com/v4/network_types.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.NetworkTypeResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Networktypes) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Networktypes, nil
}

View File

@@ -1,33 +0,0 @@
package igdb
import (
"fmt"
)
func (g *igdb) GetParentGameID(id uint64) (uint64, error) {
detail, err := g.GetGame(id)
if err != nil {
return 0, fmt.Errorf("failed to fetch IGDB app detail for parent: %d: %w", id, err)
}
hasParent := false
if detail.ParentGame != nil && detail.ParentGame.Id != 0 {
hasParent = true
detail, err = g.GetGame(detail.ParentGame.Id)
if err != nil {
return 0, fmt.Errorf("failed to fetch IGDB version parent: %d: %w", detail.VersionParent.Id, err)
}
}
for detail.VersionParent != nil && detail.VersionParent.Id != 0 {
hasParent = true
detail, err = g.GetGame(detail.VersionParent.Id)
if err != nil {
return 0, fmt.Errorf("failed to fetch IGDB version parent: %d: %w", detail.VersionParent.Id, err)
}
}
if hasParent {
return detail.Id, nil
}
return id, nil
}

27
platform_families.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetPlatformFamilies(query string) ([]*pb.PlatformFamily, error) {
resp, err := g.Request("https://api.igdb.com/v4/platform_families.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.PlatformFamilyResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Platformfamilies) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Platformfamilies, nil
}

27
platform_logos.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetPlatformLogos(query string) ([]*pb.PlatformLogo, error) {
resp, err := g.Request("https://api.igdb.com/v4/platform_logos.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.PlatformLogoResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Platformlogos) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Platformlogos, nil
}

27
platform_types.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetPlatformTypes(query string) ([]*pb.PlatformType, error) {
resp, err := g.Request("https://api.igdb.com/v4/platform_types.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.PlatformTypeResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Platformtypes) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Platformtypes, nil
}

View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetPlatformVersionCompanies(query string) ([]*pb.PlatformVersionCompany, error) {
resp, err := g.Request("https://api.igdb.com/v4/platform_version_companies.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.PlatformVersionCompanyResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Platformversioncompanies) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Platformversioncompanies, nil
}

View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetPlatformVersionReleaseDates(query string) ([]*pb.PlatformVersionReleaseDate, error) {
resp, err := g.Request("https://api.igdb.com/v4/platform_version_release_dates.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.PlatformVersionReleaseDateResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Platformversionreleasedates) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Platformversionreleasedates, nil
}

27
platform_versions.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetPlatformVersions(query string) ([]*pb.PlatformVersion, error) {
resp, err := g.Request("https://api.igdb.com/v4/platform_versions.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.PlatformVersionResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Platformversions) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Platformversions, nil
}

27
platform_websites.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetPlatformWebsites(query string) ([]*pb.PlatformWebsite, error) {
resp, err := g.Request("https://api.igdb.com/v4/platform_websites.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.PlatformWebsiteResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Platformwebsites) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Platformwebsites, nil
}

26
platforms.go Normal file
View File

@@ -0,0 +1,26 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetPlatforms(query string) ([]*pb.Platform, error) {
resp, err := g.Request("https://api.igdb.com/v4/platforms.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.PlatformResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Platforms) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Platforms, nil
}

27
player_perspectives.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetPlayerPerspectives(query string) ([]*pb.PlayerPerspective, error) {
resp, err := g.Request("https://api.igdb.com/v4/player_perspectives.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.PlayerPerspectiveResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Playerperspectives) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Playerperspectives, nil
}

View File

@@ -1,33 +0,0 @@
package igdb
import (
"fmt"
"github/bestnite/go-igdb/constant"
pb "github/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
// GetPopularGameIDs retrieves popular IGDB game IDs based on a given popularity type.
// popularity_type = 1 IGDB Visits: Game page visits on IGDB.com.
// popularity_type = 2 IGDB Want to Play: Additions to IGDB.com users “Want to Play” lists.
// popularity_type = 3 IGDB Playing: Additions to IGDB.com users “Playing” lists.
// popularity_type = 4 IGDB Played: Additions to IGDB.com users “Played” lists.
func (g *igdb) GetPopularGameIDs(popularityType, offset, limit int) ([]uint64, error) {
query := fmt.Sprintf("fields game_id,value,popularity_type; sort value desc; limit %d; offset %d; where popularity_type = %d;", limit, offset, popularityType)
resp, err := g.Request(constant.IGDBPopularityURL, query)
if err != nil {
return nil, fmt.Errorf("failed to fetch popular IGDB game IDs for type %d: %w", popularityType, err)
}
data := pb.PopularityPrimitiveResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal IGDB popular games response: %w", err)
}
gameIDs := make([]uint64, 0, len(data.Popularityprimitives))
for _, game := range data.Popularityprimitives {
gameIDs = append(gameIDs, uint64(game.GameId))
}
return gameIDs, nil
}

27
popularity_primitives.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetPopularityPrimitives(query string) ([]*pb.PopularityPrimitive, error) {
resp, err := g.Request("https://api.igdb.com/v4/popularity_primitives.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.PopularityPrimitiveResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Popularityprimitives) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Popularityprimitives, nil
}

27
popularity_types.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetPopularityTypes(query string) ([]*pb.PopularityType, error) {
resp, err := g.Request("https://api.igdb.com/v4/popularity_types.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.PopularityTypeResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Popularitytypes) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Popularitytypes, nil
}

48
rate_limiter.go Normal file
View File

@@ -0,0 +1,48 @@
package igdb
import (
"sync"
"time"
)
type rateLimiter struct {
mu sync.Mutex
rate int
interval time.Duration
tokens int
lastRefill time.Time
}
func newRateLimiter(rate int) *rateLimiter {
return &rateLimiter{
rate: rate,
interval: time.Second,
tokens: rate,
lastRefill: time.Now(),
}
}
func (r *rateLimiter) wait() {
r.mu.Lock()
defer r.mu.Unlock()
now := time.Now()
elapsed := now.Sub(r.lastRefill)
if elapsed >= r.interval {
r.tokens = r.rate
r.lastRefill = now
}
if r.tokens <= 0 {
waitTime := r.interval - elapsed
r.mu.Unlock()
time.Sleep(waitTime)
r.mu.Lock()
r.tokens = r.rate - 1
r.lastRefill = time.Now()
return
}
r.tokens--
}

27
regions.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetRegions(query string) ([]*pb.Region, error) {
resp, err := g.Request("https://api.igdb.com/v4/regions.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.RegionResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Regions) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Regions, nil
}

27
release_date_regions.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetReleaseDateRegions(query string) ([]*pb.ReleaseDateRegion, error) {
resp, err := g.Request("https://api.igdb.com/v4/release_date_regions.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.ReleaseDateRegionResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Releasedateregions) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Releasedateregions, nil
}

27
release_date_statuses.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetReleaseDateStatuses(query string) ([]*pb.ReleaseDateStatus, error) {
resp, err := g.Request("https://api.igdb.com/v4/release_date_statuses.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.ReleaseDateStatusResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Releasedatestatuses) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Releasedatestatuses, nil
}

27
release_dates.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetReleaseDates(query string) ([]*pb.ReleaseDate, error) {
resp, err := g.Request("https://api.igdb.com/v4/release_dates.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.ReleaseDateResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Releasedates) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Releasedates, nil
}

27
screenshots.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetScreenshots(query string) ([]*pb.Screenshot, error) {
resp, err := g.Request("https://api.igdb.com/v4/screenshots.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.ScreenshotResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Screenshots) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Screenshots, nil
}

View File

@@ -3,48 +3,47 @@ package igdb
import (
"encoding/json"
"fmt"
"github/bestnite/go-igdb/constant"
"io"
"net/http"
"net/url"
"strings"
"time"
pb "github/bestnite/go-igdb/proto"
pb "github.com/bestnite/go-igdb/proto"
"github.com/PuerkitoBio/goquery"
"github.com/bestnite/go-flaresolverr"
"google.golang.org/protobuf/proto"
)
func (g *igdb) SearchGame(query string) ([]*pb.Game, error) {
resp, err := g.Request(constant.IGDBGameURL, query)
if err != nil {
return nil, fmt.Errorf("failed to search: %s: %w", query, err)
}
data := pb.GameResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to parse IGDB search response: %w", err)
}
if len(data.Games) != 0 && data.Games[0].Name == "" {
return g.WebSearchGame(query)
}
return data.Games, nil
}
var webSearchCFCookies struct {
cookies []*http.Cookie
expires time.Time
}
func (g *igdb) WebSearchGame(name string) ([]*pb.Game, error) {
func (g *Client) Search(query string) ([]*pb.Search, error) {
resp, err := g.Request("https://api.igdb.com/v4/search.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.SearchResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Searches) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Searches, nil
}
func (g *Client) WebSearchGames(name string) ([]*pb.Game, error) {
params := url.Values{}
params.Add("q", name)
params.Add("utf8", "✓")
Url := fmt.Sprintf("%s?%s", constant.IGDBWebSearchURL, params.Encode())
Url := fmt.Sprintf("%s?%s", "https://www.igdb.com/search", params.Encode())
f, err := g.getFlaresolverr()
if err != nil {
@@ -94,5 +93,5 @@ func (g *igdb) WebSearchGame(name string) ([]*pb.Game, error) {
ids[i] = game.Id
}
return g.GetGames(ids)
return GetItemsByIDs(ids, g.GetGames)
}

27
themes.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetThemes(query string) ([]*pb.Theme, error) {
resp, err := g.Request("https://api.igdb.com/v4/themes.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.ThemeResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Themes) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Themes, nil
}

View File

@@ -3,7 +3,6 @@ package igdb
import (
"encoding/json"
"fmt"
"github/bestnite/go-igdb/constant"
"net/url"
"time"
)
@@ -36,7 +35,7 @@ func (t *twitchToken) getToken() (string, error) {
}
func (t *twitchToken) loginTwitch() (string, time.Duration, error) {
baseURL, _ := url.Parse(constant.TwitchAuthURL)
baseURL, _ := url.Parse("https://id.twitch.tv/oauth2/token")
params := url.Values{}
params.Add("client_id", t.clientID)
params.Add("client_secret", t.clientSecret)

View File

@@ -1,37 +0,0 @@
package igdb
import (
"fmt"
"github/bestnite/go-igdb/constant"
"net/http"
"net/url"
)
// ActiveWebhook activates a webhook for a specific endpoint.
//
// https://api-docs.igdb.com/#webhooks
func (g *igdb) ActiveWebhook(endpoint, secret, callbackUrl string) error {
t, err := g.token.getToken()
if err != nil {
return fmt.Errorf("failed to get Twitch token: %w", err)
}
dataBody := url.Values{}
dataBody.Set("url", callbackUrl)
dataBody.Set("secret", secret)
dataBody.Set("method", "update")
resp, err := request().SetBody(dataBody.Encode()).SetHeaders(map[string]string{
"Client-ID": g.clientID,
"Authorization": "Bearer " + t,
"User-Agent": "",
"Content-Type": "application/x-www-form-urlencoded",
}).Post(fmt.Sprintf(constant.IGDBWebhookURL, endpoint))
if err != nil {
return fmt.Errorf("failed to make request: %s: %w", callbackUrl, err)
}
if resp.StatusCode() == http.StatusOK {
return nil
}
return fmt.Errorf("failed to activate webhook: %s: %s", callbackUrl, resp.String())
}

26
webhooks.go Normal file
View File

@@ -0,0 +1,26 @@
package igdb
import (
"fmt"
"net/http"
"net/url"
"github.com/bestnite/go-igdb/endpoint"
)
func (g *Client) ActiveWebhook(endpoint endpoint.Endpoint, secret, callbackUrl string) error {
dataBody := url.Values{}
dataBody.Set("url", callbackUrl)
dataBody.Set("secret", secret)
dataBody.Set("method", "update")
resp, err := g.Request(fmt.Sprintf("https://api.igdb.com/v4/%s/webhooks/", endpoint), dataBody.Encode())
if err != nil {
return fmt.Errorf("failed to make request: %s: %w", callbackUrl, err)
}
if resp.StatusCode() == http.StatusOK {
return nil
}
return fmt.Errorf("failed to activate webhook: %s: %s", callbackUrl, resp.String())
}

27
website_types.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetWebsiteTypes(query string) ([]*pb.WebsiteType, error) {
resp, err := g.Request("https://api.igdb.com/v4/website_types.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.WebsiteTypeResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Websitetypes) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Websitetypes, nil
}

27
websites.go Normal file
View File

@@ -0,0 +1,27 @@
package igdb
import (
"fmt"
pb "github.com/bestnite/go-igdb/proto"
"google.golang.org/protobuf/proto"
)
func (g *Client) GetWebsites(query string) ([]*pb.Website, error) {
resp, err := g.Request("https://api.igdb.com/v4/websites.pb", query)
if err != nil {
return nil, fmt.Errorf("failed to request: %w", err)
}
data := pb.WebsiteResult{}
if err = proto.Unmarshal(resp.Body(), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal: %w", err)
}
if len(data.Websites) == 0 {
return nil, fmt.Errorf("no results: %s", query)
}
return data.Websites, nil
}