gophersauce

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 22, 2020 License: MIT Imports: 14 Imported by: 4

README

gophersauce

gophersauce is an API wrapper for SauceNAO written in Go.

go get github.com/jozsefsallai/gophersauce

Basic Example

package main

import (
  "fmt"
  "log"
  "os"

  "github.com/jozsefsallai/gophersauce"
)

func main() {
  client, err := gophersauce.NewClient(nil)
  if err != nil {
    log.Fatal(err)
  }

  response, err := client.FromURL("https://i.imgur.com/v6EiHyj.png")
  if err != nil {
    log.Fatal(err)
  }

  if response.Count() == 0 {
    fmt.Println("There are no results :(")
    os.Exit(0)
  }

  first := response.First()
  if first.IsPixiv() {
    fmt.Println("This result is from Pixiv!")
    fmt.Println("Here's the ID of the image on Pixiv:", first.Data.PixivID)
  } else {
    fmt.Println("Probably not from Pixiv!")
    fmt.Println("Here are the external URLs:", first.Data.ExternalURLs)
  }
}

Currently Supported Providers:

  • Pixiv
  • IMDb
  • Bcy
  • AniDB
  • Pawoo
  • Seiga
  • Sankaku
  • Danbooru

Documentation and Reference

https://pkg.go.dev/github.com/jozsefsallai/gophersauce

Todo

  • Add more providers
  • Unit tests

License

MIT.

Documentation

Overview

Package gophersauce is an API wrapper for the SauceNAO.com reverse image search engine.

Initializing a client (without additional options):

client, err := gophersauce.NewClient(nil)
if err != nil {
	log.Fatal(err)
}

Initializing a client with options:

client, err := gophersauce.NewClient(&gophersauce.Settings{
	APIUrl: "https://custom_server/search.php"
	APIKey: "your API key",
	MaxResults: 4
})
if err != nil {
	log.Fatal(err)
}

Any of the options can be omitted. By default, MaxResults will be 6 and APIKey will be an empty string.

You can also change these properties after instantiating the client:

client.SetAPIUrl("https://custom_server/search.php")
client.SetAPIKey("your API key")
client.SetMaxResults(12)

There are three ways in which you can consume the SauceNAO API: URL, file, and reader.

Reverse searching an image using a URL:

response, err := client.FromURL("https://i.imgur.com/v6EiHyj.png")
if err != nil {
	log.Fatal(err)
}

Reverse searching an image using a file path:

response, err := client.FromFile("path/to/file.png")
if err != nil {
	log.Fatal(err)
}

Reverse searching an image using a reader:

f, _ := os.Open("path/to/file")
response, err := client.FromReader(f)
if err != nil {
	log.Fatal(err)
}

API responses have helpful methods, such as First() which will return the first result (which is likely the one that is the most similar to your image), if any:

response, _ := client.FromURL("https://i.imgur.com/v6EiHyj.png")
first := response.First()
fmt.Println("First result (external URLs):", first.Data.ExternalURLs)

Some of the response fields are, by default, declared as interfaces because of the way the SauceNAO API works. You will have to either check for the type of the field yourself and parse it that way, or use a helper function, such as GetUserID(), GetAccountType() (on type SaucenaoResponse) or GetCreatorString() (on type SearchResult).

Example:

This will not work:

response, _ := client.FromURL("https://i.imgur.com/v6EiHyj.png")
if response.Header.UserID == 0 {
	fmt.Println("You're not logged in!")
}

This will work:

response, _ := client.FromURL("https://i.imgur.com/v6EiHyj.png")

userID, err := response.GetUserID()
if err != nil {
	log.Fatal(err)
}

if userID == 0 {
	fmt.Println("You're not logged in!")
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	MaxResults int
	APIKey     string
	APIUrl     string
}

Client is a Saucenao API client instance.

func NewClient

func NewClient(settings *Settings) (*Client, error)

NewClient creates a new Gophersauce API client instance

func (*Client) FromFile

func (c *Client) FromFile(path string) (*SaucenaoResponse, error)

FromFile will look up an image from a given file path

func (*Client) FromReader

func (c *Client) FromReader(reader io.Reader) (*SaucenaoResponse, error)

FromReader will look up an image from a given IO reader

func (*Client) FromURL

func (c *Client) FromURL(url string) (*SaucenaoResponse, error)

FromURL will look up an image from a given URL address

func (*Client) SetAPIKey

func (c *Client) SetAPIKey(key string)

SetAPIKey updates the API key of the client

func (*Client) SetAPIUrl added in v1.0.1

func (c *Client) SetAPIUrl(url string)

SetAPIUrl updates the API URL of the client

func (*Client) SetMaxResults

func (c *Client) SetMaxResults(maxResults int) error

SetMaxResults updates the max results property of the client

type ResponseHeader

type ResponseHeader struct {
	UserID            interface{}            `json:"user_id"`
	AccountType       interface{}            `json:"account_type"`
	ShortLimit        string                 `json:"short_limit"`
	LongLimit         string                 `json:"long_limit"`
	ShortRemaining    int                    `json:"short_remaining"`
	LongRemaining     int                    `json:"long_remaining"`
	Status            int                    `json:"status"`
	ResultsRequested  interface{}            `json:"results_requested"`
	Index             map[string]SearchIndex `json:"index"`
	SearchDepth       string                 `json:"search_depth"`
	MinimumSimilarity float32                `json:"minimum_similarity"`
	QueryImageDisplay string                 `json:"query_image_display"`
	QueryImage        string                 `json:"query_image"`
	ResultsReturned   int                    `json:"results_returned"`
	Message           string                 `json:"message,omitempty"`
}

ResponseHeader contains meta information about the curernt request.

type SaucenaoResponse

type SaucenaoResponse struct {
	Header  ResponseHeader `json:"header"`
	Results []SearchResult `json:"results"`
}

SaucenaoResponse contains the response returned after sending an API call to Saucenao (POST https://saucenao.com/search.php).

func (*SaucenaoResponse) Count

func (res *SaucenaoResponse) Count() int

Count returns the number of results in the response

func (*SaucenaoResponse) First

func (res *SaucenaoResponse) First() SearchResult

First returns the first result in the response (if any)

func (*SaucenaoResponse) GetAccountType added in v1.0.1

func (res *SaucenaoResponse) GetAccountType() (int, error)

GetAccountType returns the account type of the user as an integer

func (*SaucenaoResponse) GetUserID added in v1.0.1

func (res *SaucenaoResponse) GetUserID() (int, error)

GetUserID returns the ID of the user as an integer (0 if not logged in)

type SearchIndex

type SearchIndex struct {
	Status   int `json:"status"`
	ParentID int `json:"parent_id"`
	ID       int `json:"id"`
	Results  int `json:"results"`
}

SearchIndex contains additional information about an individual search index.

type SearchResult

type SearchResult struct {
	Header SearchResultHeader `json:"header"`
	Data   SearchResultData   `json:"data"`
}

SearchResult is an individual result similar to the requested image.

func (*SearchResult) GetCreatorString added in v1.0.1

func (result *SearchResult) GetCreatorString() string

GetCreatorString will return the creator of the resource as a string

func (*SearchResult) IsAniDB

func (result *SearchResult) IsAniDB() bool

IsAniDB returns true if the result is from AniDB

func (*SearchResult) IsBcy

func (result *SearchResult) IsBcy() bool

IsBcy returns true if the result is from Bcy

func (*SearchResult) IsDanbooru

func (result *SearchResult) IsDanbooru() bool

IsDanbooru returns true if the result is from Danbooru

func (*SearchResult) IsDeviantArt

func (result *SearchResult) IsDeviantArt() bool

IsDeviantArt returns true if the result is from DeviantArt

func (*SearchResult) IsIMDb

func (result *SearchResult) IsIMDb() bool

IsIMDb returns true if the result is from IMDb

func (*SearchResult) IsPawoo

func (result *SearchResult) IsPawoo() bool

IsPawoo returns true if the result is from Pawoo

func (*SearchResult) IsPixiv

func (result *SearchResult) IsPixiv() bool

IsPixiv returns true if the result is from Pixiv

func (*SearchResult) IsSankaku

func (result *SearchResult) IsSankaku() bool

IsSankaku returns true if the result is from Sankaku

func (*SearchResult) IsSeiga

func (result *SearchResult) IsSeiga() bool

IsSeiga returns true if the result is from Seiga

type SearchResultData

type SearchResultData struct {
	ExternalURLs         []string    `json:"ext_urls"`
	Title                string      `json:"title,omitempty"`
	PixivID              int         `json:"pixiv_id,omitempty"`
	MemberName           string      `json:"member_name,omitempty"`
	MemberID             int         `json:"member_id,omitempty"`
	Source               string      `json:"source,omitempty"`
	IMDbID               string      `json:"imdb_id,omitempty"`
	Part                 string      `json:"part,omitempty"`
	Year                 string      `json:"year,omitempty"`
	EstimatedTime        string      `json:"est_time,omitempty"`
	DeviantArtID         int         `json:"da_id,omitempty"`
	AuthorName           string      `json:"author_name,omitempty"`
	AuthorURL            string      `json:"author_url,omitempty"`
	BcyID                int         `json:"bcy_id,omitempty"`
	MemberLinkID         int         `json:"member_link_id,omitempty"`
	BcyType              string      `json:"bcy_type,omitempty"`
	AniDBAID             int         `json:"anidb_aid,omitempty"`
	PawooID              int         `json:"pawoo_id,omitempty"`
	PawooUserAccount     string      `json:"pawoo_user_acct,omitempty"`
	PawooUserUsername    string      `json:"pawoo_user_username,omitempty"`
	PawooUserDisplayName string      `json:"pawoo_user_display_name,omitempty"`
	SeigaID              int         `json:"seiga_id,omitempty"`
	SankakuID            int         `json:"sankaku_id,omitempty"`
	Creator              interface{} `json:"creator,omitempty"`
	Material             string      `json:"material,omitempty"`
	Characters           string      `json:"characters,omitempty"`
	DanbooruID           int         `json:"danbooru_id,omitempty"`
}

SearchResultData contains sources and additional information about the. search result

type SearchResultHeader

type SearchResultHeader struct {
	Similarity string `json:"similarity"`
	Thumbnail  string `json:"thumbnail"`
	IndexID    int    `json:"index_id"`
	IndexName  string `json:"index_name"`
}

SearchResultHeader contains meta information about the search result, such as the similarity, the thumbnail and index ID and name.

type Settings

type Settings struct {
	MaxResults int
	APIKey     string
	APIUrl     string
}

Settings is a structure that contains the settings of the Gophersauce client

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL