giphy

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2022 License: MIT Imports: 8 Imported by: 13

README

giphy

Go library for the Giphy API

Build status Go Report Card GoDoc License MIT

You will need an API key from Giphy, instructions to get one can be found here: https://developers.giphy.com/docs/api#quick-start-guide

Command line tool

Installation
go get -u github.com/peterhellberg/giphy/cmd/giphy
Usage
Commands:
	search, s           [args]
	gif, id             [args]
	random, rand, r     [args]
	translate, trans, t [args]
	trending, trend, tr [args]
GIPHY_API_KEY=[your-api-key] GIPHY_LIMIT=4 giphy search computer
http://media3.giphy.com/media/wvHC7zyCedEI0/giphy.gif
http://media3.giphy.com/media/gr8K2b72UefvO/giphy.gif
http://media2.giphy.com/media/L2u68v1MmZv5m/giphy.gif
http://media1.giphy.com/media/4WOs6Af0nOOeQ/giphy.gif

Examples of using the library

translate.go
package main

import (
  "fmt"
  "os"

  "github.com/peterhellberg/giphy"
)

func main() {
  if len(os.Args) < 2 {
    return
  }

  g := giphy.DefaultClient

  res, err := g.Translate(os.Args[1:])
  if err != nil {
    fmt.Println(err)
    os.Exit(1)
  }

  fmt.Println(res.Data.MediaURL())
}

package main

import (
  "fmt"

  "github.com/peterhellberg/giphy"
)

func main() {
  g := giphy.DefaultClient

  if trending, err := g.Trending(); err == nil {
    for i, d := range trending.Data {
      fmt.Println(i, "-", d.MediaURL())
    }
  }
}
Run an example
GIPHY_API_KEY=[your-api-key] GIPHY_RATING=pg-13 go run example.go

License (MIT)

Copyright (c) 2015-2022 Peter Hellberg

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Computer

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoImageFound is the error returned when no image was found
	ErrNoImageFound = errors.New("no image found")

	// ErrUnknown is used for unknown errors from the Giphy API
	ErrUnknown = errors.New("unknown error")

	// ErrNoTrendingImagesFound is returned when no trending images were found
	ErrNoTrendingImagesFound = errors.New("no trending images found")

	// ErrNoRawData is returned if there was no data property in response
	ErrNoRawData = errors.New("no raw data")
)
View Source
var DefaultClient = NewClient()

DefaultClient is the default Giphy API client

Functions

func Env

func Env(key, fallback string) string

Env returns a string from the ENV, or fallback variable

func EnvBool

func EnvBool(key string, fallback bool) bool

EnvBool returns a bool from the ENV, or fallback variable

func EnvInt

func EnvInt(key string, fallback int) int

EnvInt returns an int from the ENV, or fallback variable

Types

type ActionURL added in v0.0.2

type ActionURL struct {
	URL string `json:"url"`
}

ActionURL contains a pingback URL for an action such as SEEN, CLICK or SENT

type Analytics added in v0.0.2

type Analytics struct {
	Onload  ActionURL `json:"onload"`
	Onclick ActionURL `json:"onclick"`
	Onsent  ActionURL `json:"onsent"`
}

Analytics represents the analytics section in a Giphy API response

type Client

type Client struct {
	// APIKey is the key used for requests to the Giphy API
	APIKey string

	// Limit is the limit used for requests to the Giphy API
	Limit int

	// Rating is the rating used for requests to the Giphy API
	Rating string

	// BaseURL is the base url for Giphy API.
	BaseURL *url.URL

	// BasePath is the base path for the gifs endpoints
	BasePath string

	// User agent used for HTTP requests to Giphy API.
	UserAgent string
	// contains filtered or unexported fields
}

A Client communicates with the Giphy API.

func NewClient

func NewClient(options ...Option) *Client

NewClient returns a new Giphy API client. If HTTPClient were not provided then http.DefaultClient is used.

func (*Client) Do

func (c *Client) Do(req *http.Request, v interface{}) (*http.Response, error)

Do sends an API request and returns the API response. The API response is decoded and stored in the value pointed to by v, or returned as an error if an API error has occurred.

func (*Client) GIF

func (c *Client) GIF(id string) (GIF, error)

GIF returns a ID response from the Giphy API

func (*Client) NewRequest

func (c *Client) NewRequest(s string) (*http.Request, error)

NewRequest creates an API request.

func (*Client) Random

func (c *Client) Random(args []string) (Random, error)

Random returns a random response from the Giphy API

func (*Client) Search

func (c *Client) Search(args []string) (Search, error)

Search returns a search response from the Giphy API

func (*Client) Translate

func (c *Client) Translate(args []string) (Translate, error)

Translate returns a translate response from the Giphy API

func (*Client) Trending

func (c *Client) Trending(args ...[]string) (Trending, error)

Trending returns a trending response from the Giphy API

type Data

type Data struct {
	Type             string    `json:"type"`
	ID               string    `json:"id"`
	URL              string    `json:"url"`
	BitlyGifURL      string    `json:"bitly_gif_url"`
	BitlyURL         string    `json:"bitly_url"`
	EmbedURL         string    `json:"embed_url"`
	Username         string    `json:"username"`
	Source           string    `json:"source"`
	Rating           string    `json:"rating"`
	Caption          string    `json:"caption"`
	ContentURL       string    `json:"content_url"`
	ImportDatetime   string    `json:"import_datetime"`
	TrendingDatetime string    `json:"trending_datetime"`
	Images           Images    `json:"images"`
	Analytics        Analytics `json:"analytics"`
}

Data contains all the fields in a data response from the Giphy API

func (Data) MediaURL

func (d Data) MediaURL() string

MediaURL points directly to GIF image on media.giphy.com

type GIF

type GIF struct {
	Data    Data
	RawData json.RawMessage `json:"data"`
	Meta    Meta            `json:"meta"`
}

GIF represents a ID response from the Giphy API

type Image

type Image struct {
	Height   string `json:"height"`
	Width    string `json:"width"`
	Size     string `json:"size"`
	URL      string `json:"url"`
	Mp4Size  string `json:"mp4_size"`
	Mp4      string `json:"mp4"`
	WebpSize string `json:"webp_size"`
	Webp     string `json:"webp"`
}

Image represents an image

type Images

type Images struct {
	FixedHeight            Image `json:"fixed_height"`
	FixedHeightStill       Image `json:"fixed_height_still"`
	FixedHeightDownsampled Image `json:"fixed_height_downsampled"`
	FixedWidth             Image `json:"fixed_width"`
	FixedWidthStill        Image `json:"fixed_width_still"`
	FixedWidthDownsampled  Image `json:"fixed_width_downsampled"`
	Downsized              Image `json:"downsized"`
	DownsizedStill         Image `json:"downsized_still"`
	Original               Image `json:"original"`
	OriginalStill          Image `json:"original_still"`
}

Images represents all the different types of images

type Meta

type Meta struct {
	Status int    `json:"status"`
	Msg    string `json:"msg"`
}

Meta represents the meta section in a Giphy API response

type Option

type Option func(*Client)

Option function used by the Giphy client

func APIKey

func APIKey(apiKey string) Option

APIKey option used by the Giphy client

func HTTPClient

func HTTPClient(httpClient *http.Client) Option

HTTPClient option used by the Giphy client

func Limit

func Limit(limit int) Option

Limit option used by the Giphy client

func Rating

func Rating(rating string) Option

Rating option used by the Giphy client

type Pagination

type Pagination struct {
	TotalCount int `json:"total_count"`
	Count      int `json:"count"`
	Offset     int `json:"offset"`
}

Pagination represents the pagination section in a Giphy API response

type Random

type Random struct {
	Data    RandomData
	RawData json.RawMessage `json:"data"`
	Meta    Meta            `json:"meta"`
}

Random represents a random response from the Giphy API

type RandomData

type RandomData struct {
	Type                         string   `json:"type"`
	ID                           string   `json:"id"`
	URL                          string   `json:"url"`
	ImageOriginalURL             string   `json:"image_original_url"`
	ImageURL                     string   `json:"image_url"`
	ImageMp4URL                  string   `json:"image_mp4_url"`
	ImageFrames                  string   `json:"image_frames"`
	ImageWidth                   string   `json:"image_width"`
	ImageHeight                  string   `json:"image_height"`
	FixedHeightDownsampledURL    string   `json:"fixed_height_downsampled_url"`
	FixedHeightDownsampledWidth  string   `json:"fixed_height_downsampled_width"`
	FixedHeightDownsampledHeight string   `json:"fixed_height_downsampled_height"`
	FixedWidthDownsampledURL     string   `json:"fixed_width_downsampled_url"`
	FixedWidthDownsampledWidth   string   `json:"fixed_width_downsampled_width"`
	FixedWidthDownsampledHeight  string   `json:"fixed_width_downsampled_height"`
	Rating                       string   `json:"rating"`
	Username                     string   `json:"username"`
	Caption                      string   `json:"caption"`
	Tags                         []string `json:"tags"`
}

RandomData represents data section in random response from the Giphy API

func (RandomData) MediaURL

func (rd RandomData) MediaURL() string

MediaURL points directly to GIF image on media.giphy.com

type Search struct {
	Data       []Data     `json:"data"`
	Meta       Meta       `json:"meta"`
	Pagination Pagination `json:"pagination"`
}

Search represents a search response from the Giphy API

type Translate

type Translate struct {
	Data
	RawData json.RawMessage `json:"data"`
	Meta    Meta            `json:"meta"`
}

Translate represents a translate response from the Giphy API

type Trending struct {
	Data       []Data     `json:"data"`
	Meta       Meta       `json:"meta"`
	Pagination Pagination `json:"pagination"`
}

Trending represents a trending response from the Giphy API

Directories

Path Synopsis
cmd
giphy
A command line client for the Giphy API
A command line client for the Giphy API

Jump to

Keyboard shortcuts

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