diff options
Diffstat (limited to 'vendor/github.com/peterhellberg/giphy')
-rw-r--r-- | vendor/github.com/peterhellberg/giphy/client.go | 117 | ||||
-rw-r--r-- | vendor/github.com/peterhellberg/giphy/cmd/giphy/main.go | 140 | ||||
-rw-r--r-- | vendor/github.com/peterhellberg/giphy/env.go | 34 | ||||
-rw-r--r-- | vendor/github.com/peterhellberg/giphy/errors.go | 17 | ||||
-rw-r--r-- | vendor/github.com/peterhellberg/giphy/gif.go | 44 | ||||
-rw-r--r-- | vendor/github.com/peterhellberg/giphy/random.go | 37 | ||||
-rw-r--r-- | vendor/github.com/peterhellberg/giphy/search.go | 24 | ||||
-rw-r--r-- | vendor/github.com/peterhellberg/giphy/translate.go | 37 | ||||
-rw-r--r-- | vendor/github.com/peterhellberg/giphy/trending.go | 23 | ||||
-rw-r--r-- | vendor/github.com/peterhellberg/giphy/types.go | 116 |
10 files changed, 589 insertions, 0 deletions
diff --git a/vendor/github.com/peterhellberg/giphy/client.go b/vendor/github.com/peterhellberg/giphy/client.go new file mode 100644 index 00000000..09c005c2 --- /dev/null +++ b/vendor/github.com/peterhellberg/giphy/client.go @@ -0,0 +1,117 @@ +package giphy + +import ( + "encoding/json" + "fmt" + "net/http" + "net/url" +) + +// DefaultClient is the default Giphy API client +var DefaultClient = NewClient() + +// PublicBetaKey is the public beta key for the Giphy API +var PublicBetaKey = "dc6zaTOxFJmzC" + +// A Client communicates with the Giphy API. +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 + + // HTTP client used to communicate with the Giphy API. + httpClient *http.Client +} + +// NewClient returns a new Giphy API client. +// If no *http.Client were provided then http.DefaultClient is used. +func NewClient(httpClients ...*http.Client) *Client { + var httpClient *http.Client + + if len(httpClients) > 0 && httpClients[0] != nil { + httpClient = httpClients[0] + } else { + cloned := *http.DefaultClient + httpClient = &cloned + } + + c := &Client{ + APIKey: Env("GIPHY_API_KEY", PublicBetaKey), + Rating: Env("GIPHY_RATING", "g"), + Limit: EnvInt("GIPHY_LIMIT", 10), + BaseURL: &url.URL{ + Scheme: Env("GIPHY_BASE_URL_SCHEME", "https"), + Host: Env("GIPHY_BASE_URL_HOST", "api.giphy.com"), + }, + BasePath: Env("GIPHY_BASE_PATH", "/v1"), + UserAgent: Env("GIPHY_USER_AGENT", "giphy.go"), + httpClient: httpClient, + } + + return c +} + +// NewRequest creates an API request. +func (c *Client) NewRequest(s string) (*http.Request, error) { + rel, err := url.Parse(c.BasePath + s) + if err != nil { + return nil, err + } + + q := rel.Query() + q.Set("api_key", c.APIKey) + q.Set("rating", c.Rating) + rel.RawQuery = q.Encode() + + u := c.BaseURL.ResolveReference(rel) + + if EnvBool("GIPHY_VERBOSE", false) { + fmt.Println("giphy: GET", u.String()) + } + + req, err := http.NewRequest("GET", u.String(), nil) + if err != nil { + return nil, err + } + + req.Header.Add("User-Agent", c.UserAgent) + return req, nil +} + +// 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 (c *Client) Do(req *http.Request, v interface{}) (*http.Response, error) { + // Make sure to close the connection after replying to this request + req.Close = true + + resp, err := c.httpClient.Do(req) + if err != nil { + return resp, err + } + defer resp.Body.Close() + + if v != nil { + err = json.NewDecoder(resp.Body).Decode(v) + } + + if err != nil { + return nil, fmt.Errorf("error reading response from %s %s: %s", req.Method, req.URL.RequestURI(), err) + } + + return resp, nil +} diff --git a/vendor/github.com/peterhellberg/giphy/cmd/giphy/main.go b/vendor/github.com/peterhellberg/giphy/cmd/giphy/main.go new file mode 100644 index 00000000..b621f092 --- /dev/null +++ b/vendor/github.com/peterhellberg/giphy/cmd/giphy/main.go @@ -0,0 +1,140 @@ +/* + +A command line client for the Giphy API + +Installation + +Just go get the command: + + go get -u github.com/peterhellberg/giphy/cmd/giphy + +Configuration + +The command line client can be used straight out of the box, but +there are also a few environment variables that you can use in order +to override the default configuration. + + Environment variable | Default value + ----------------------|-------------- + GIPHY_API_KEY | dc6zaTOxFJmzC + GIPHY_RATING | g + GIPHY_LIMIT | 10 + GIPHY_BASE_URL_SCHEME | http + GIPHY_BASE_URL_HOST | api.giphy.com + GIPHY_BASE_PATH | /v1 + GIPHY_USER_AGENT | giphy.go + +Usage + +The command line client consists of a few sub commands. + + Commands: + search, s [args] + gif, id [args] + random, rand, r [args] + translate, trans, t [args] + trending, trend, tr [args] + +*/ +package main + +import ( + "fmt" + "os" + "strings" + + "github.com/peterhellberg/giphy" +) + +func main() { + g := giphy.DefaultClient + + if len(os.Args) < 2 { + fmt.Println(strings.Join([]string{ + "Commands:", + "search, s [args]", + "gif, id [args]", + "random, rand, r [args]", + "translate, trans, t [args]", + "trending, trend, tr [args]", + }, "\n\t")) + + return + } + + args := os.Args[1:] + + switch args[0] { + default: + search(g, args) + case "search", "s": + search(g, args[1:]) + case "gif", "id": + gif(g, args[1:]) + case "random", "rand", "r": + random(g, args[1:]) + case "translate", "trans", "t": + translate(g, args[1:]) + case "trending", "trend", "tr": + trending(g, args[1:]) + } +} + +func search(c *giphy.Client, args []string) { + res, err := c.Search(args) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + for _, d := range res.Data { + fmt.Println(d.Images.Original.URL) + } +} + +func gif(c *giphy.Client, args []string) { + if len(args) == 0 { + fmt.Println("missing Giphy id") + os.Exit(1) + } + + res, err := c.GIF(args[0]) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + fmt.Println(res.Data.Images.Original.URL) +} + +func random(c *giphy.Client, args []string) { + res, err := c.Random(args) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + fmt.Println(res.Data.ImageOriginalURL) +} + +func translate(c *giphy.Client, args []string) { + res, err := c.Translate(args) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + fmt.Println(res.Data.Images.Original.URL) +} + +func trending(c *giphy.Client, args []string) { + res, err := c.Trending(args) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + for _, d := range res.Data { + fmt.Println(d.Images.Original.URL) + } +} diff --git a/vendor/github.com/peterhellberg/giphy/env.go b/vendor/github.com/peterhellberg/giphy/env.go new file mode 100644 index 00000000..b41944ce --- /dev/null +++ b/vendor/github.com/peterhellberg/giphy/env.go @@ -0,0 +1,34 @@ +package giphy + +import ( + "os" + "strconv" +) + +// Env returns a string from the ENV, or fallback variable +func Env(key, fallback string) string { + v := os.Getenv(key) + if v != "" { + return v + } + + return fallback +} + +// EnvBool returns a bool from the ENV, or fallback variable +func EnvBool(key string, fallback bool) bool { + if b, err := strconv.ParseBool(os.Getenv(key)); err == nil { + return b + } + + return fallback +} + +// EnvInt returns an int from the ENV, or fallback variable +func EnvInt(key string, fallback int) int { + if i, err := strconv.Atoi(os.Getenv(key)); err == nil { + return i + } + + return fallback +} diff --git a/vendor/github.com/peterhellberg/giphy/errors.go b/vendor/github.com/peterhellberg/giphy/errors.go new file mode 100644 index 00000000..a98bc2f8 --- /dev/null +++ b/vendor/github.com/peterhellberg/giphy/errors.go @@ -0,0 +1,17 @@ +package giphy + +import "errors" + +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") +) diff --git a/vendor/github.com/peterhellberg/giphy/gif.go b/vendor/github.com/peterhellberg/giphy/gif.go new file mode 100644 index 00000000..8c317c97 --- /dev/null +++ b/vendor/github.com/peterhellberg/giphy/gif.go @@ -0,0 +1,44 @@ +package giphy + +import ( + "encoding/json" + "fmt" + "strings" +) + +// GIF returns a ID response from the Giphy API +func (c *Client) GIF(id string) (GIF, error) { + if strings.ContainsAny(id, "/&?") { + return GIF{}, fmt.Errorf("Invalid giphy id: `%v`", id) + } + + req, err := c.NewRequest("/gifs/" + id) + if err != nil { + return GIF{}, err + } + + var gif GIF + if _, err = c.Do(req, &gif); err != nil { + return GIF{}, err + } + + if gif.RawData == nil || gif.RawData[0] == '[' { + return GIF{}, ErrNoImageFound + } + + // Check if the first character in Data is a { + if gif.RawData[0] == '{' { + var d Data + + err = json.Unmarshal(gif.RawData, &d) + if err != nil { + return GIF{}, err + } + + gif.Data = d + + return gif, nil + } + + return GIF{}, ErrUnknown +} diff --git a/vendor/github.com/peterhellberg/giphy/random.go b/vendor/github.com/peterhellberg/giphy/random.go new file mode 100644 index 00000000..1374ed24 --- /dev/null +++ b/vendor/github.com/peterhellberg/giphy/random.go @@ -0,0 +1,37 @@ +package giphy + +import ( + "encoding/json" + "strings" +) + +// Random returns a random response from the Giphy API +func (c *Client) Random(args []string) (Random, error) { + argsStr := strings.Join(args, " ") + + req, err := c.NewRequest("/gifs/random?tag=" + argsStr) + if err != nil { + return Random{}, err + } + + var random Random + if _, err = c.Do(req, &random); err != nil { + return Random{}, err + } + + // Check if the first character in Data is a [ + if random.RawData == nil || random.RawData[0] == '[' { + return Random{}, ErrNoImageFound + } + + var d RandomData + + err = json.Unmarshal(random.RawData, &d) + if err != nil { + return Random{}, err + } + + random.Data = d + + return random, nil +} diff --git a/vendor/github.com/peterhellberg/giphy/search.go b/vendor/github.com/peterhellberg/giphy/search.go new file mode 100644 index 00000000..ca5af117 --- /dev/null +++ b/vendor/github.com/peterhellberg/giphy/search.go @@ -0,0 +1,24 @@ +package giphy + +import ( + "fmt" + "strings" +) + +// Search returns a search response from the Giphy API +func (c *Client) Search(args []string) (Search, error) { + argsStr := strings.Join(args, " ") + + path := fmt.Sprintf("/gifs/search?limit=%v&q=%s", c.Limit, argsStr) + req, err := c.NewRequest(path) + if err != nil { + return Search{}, err + } + + var search Search + if _, err = c.Do(req, &search); err != nil { + return Search{}, err + } + + return search, nil +} diff --git a/vendor/github.com/peterhellberg/giphy/translate.go b/vendor/github.com/peterhellberg/giphy/translate.go new file mode 100644 index 00000000..e3454b28 --- /dev/null +++ b/vendor/github.com/peterhellberg/giphy/translate.go @@ -0,0 +1,37 @@ +package giphy + +import ( + "encoding/json" + "strings" +) + +// Translate returns a translate response from the Giphy API +func (c *Client) Translate(args []string) (Translate, error) { + argsStr := strings.Join(args, " ") + + req, err := c.NewRequest("/gifs/translate?s=" + argsStr) + if err != nil { + return Translate{}, err + } + + var translate Translate + if _, err = c.Do(req, &translate); err != nil { + return Translate{}, err + } + + if len(translate.RawData) == 0 { + return Translate{}, ErrNoRawData + } + + // Check if the first character in Data is a [ + if translate.RawData[0] == '[' { + return Translate{}, ErrNoImageFound + } + + err = json.Unmarshal(translate.RawData, &translate.Data) + if err != nil { + return Translate{}, err + } + + return translate, nil +} diff --git a/vendor/github.com/peterhellberg/giphy/trending.go b/vendor/github.com/peterhellberg/giphy/trending.go new file mode 100644 index 00000000..671e0d51 --- /dev/null +++ b/vendor/github.com/peterhellberg/giphy/trending.go @@ -0,0 +1,23 @@ +package giphy + +import "fmt" + +// Trending returns a trending response from the Giphy API +func (c *Client) Trending(args ...[]string) (Trending, error) { + path := fmt.Sprintf("/gifs/trending?limit=%v", c.Limit) + req, err := c.NewRequest(path) + if err != nil { + return Trending{}, err + } + + var res Trending + if _, err = c.Do(req, &res); err != nil { + return res, err + } + + if len(res.Data) == 0 { + return res, ErrNoTrendingImagesFound + } + + return res, nil +} diff --git a/vendor/github.com/peterhellberg/giphy/types.go b/vendor/github.com/peterhellberg/giphy/types.go new file mode 100644 index 00000000..e7a7ebfc --- /dev/null +++ b/vendor/github.com/peterhellberg/giphy/types.go @@ -0,0 +1,116 @@ +package giphy + +import "encoding/json" + +// Search represents a search response from the Giphy API +type Search struct { + Data []Data `json:"data"` + Meta Meta `json:"meta"` + Pagination Pagination `json:"pagination"` +} + +// GIF represents a ID response from the Giphy API +type GIF struct { + Data Data + RawData json.RawMessage `json:"data"` + Meta Meta `json:"meta"` +} + +// Random represents a random response from the Giphy API +type Random struct { + Data RandomData + RawData json.RawMessage `json:"data"` + Meta Meta `json:"meta"` +} + +// Translate represents a translate response from the Giphy API +type Translate struct { + Data + RawData json.RawMessage `json:"data"` + Meta Meta `json:"meta"` +} + +// Trending represents a trending response from the Giphy API +type Trending struct { + Data []Data `json:"data"` + Meta Meta `json:"meta"` + Pagination Pagination `json:"pagination"` +} + +// Data contains all the fields in a data response from the Giphy API +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"` +} + +// RandomData represents data section in random response from the Giphy API +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"` +} + +// Images represents all the different types of 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"` +} + +// Image represents an image +type Image struct { + URL string `json:"url"` + Width string `json:"width"` + Height string `json:"height"` + Size string `json:"size,omitempty"` + Frames string `json:"frames,omitempty"` + Mp4 string `json:"mp4,omitempty"` +} + +// Pagination represents the pagination section in a Giphy API response +type Pagination struct { + TotalCount int `json:"total_count"` + Count int `json:"count"` + Offset int `json:"offset"` +} + +// Meta represents the meta section in a Giphy API response +type Meta struct { + Status int `json:"status"` + Msg string `json:"msg"` +} |