-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
186 lines (158 loc) · 4.41 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"strings"
"time"
"github.com/spf13/cobra"
)
func main() {
var rootCmd = &cobra.Command{
Use: "gifsearch",
Short: "gifsearch is a way to find gifs",
Run: gifSearch,
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("requires at least one arg, what gif are you looking for?")
}
count, err := cmd.Flags().GetInt("count")
if err != nil {
log.Fatalf("error getting 'count' flag: %s", err)
}
if count < 0 {
return fmt.Errorf("'count' flag must not be negative, got :%d", count)
}
if count > 20 {
log.Fatalf("only up to 20 gifs at a time please, got %d", count)
}
return nil
},
}
rootCmd.Flags().IntP("count", "c", 1, "number of gifs to return")
rootCmd.Flags().StringP("engine", "e", "", "gif engine to use 'giphy' or 'tenor'. If not specified, Tenor is searched first and Gifme if there is an error from Tenor")
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func gifSearch(cmd *cobra.Command, args []string) {
searchTerm := strings.Join(args[0:], " ")
count, err := cmd.Flags().GetInt("count")
if err != nil {
log.Fatalf("error getting 'count' flag: %s", err)
}
engine, err := cmd.Flags().GetString("engine")
if err != nil {
log.Fatalf("error getting 'engine' flag: %s", err)
}
switch {
case engine == "giphy":
err := giffyGifSearch(searchTerm, count)
if err != nil {
log.Fatal("Error getting gif from Giphy:", err)
}
case engine == "tenor":
err = tenorGifSearch(searchTerm, count)
if err != nil {
log.Fatal("Error getting gif from Tenor:", err)
}
default:
err = tenorGifSearch(searchTerm, count)
if err != nil {
log.Println("Error getting gif from Tenor:", err)
// If error, try Giffy next
err = giffyGifSearch(searchTerm, count)
if err != nil {
log.Println("Error getting gif from Giphy:", err)
}
}
}
}
func giffyGifSearch(searchTerm string, count int) error {
log.Printf("Powered By Giphy")
log.Printf("Searching for %s...", searchTerm)
gifResp, err := giffyGetGifs(searchTerm, count)
if err != nil {
return err
}
gifCount := len(gifResp.Data)
if gifCount == 0 {
return fmt.Errorf("ಥ_ಥ no giffy found")
}
r := rand.New(rand.NewSource(time.Now().Unix()))
perm := r.Perm(len(gifResp.Data))
for i := 0; i < count; i++ {
gifChoice := gifResp.Data[perm[i]]
// get chosen gif
resp, err := http.Get(gifChoice.Images.Original.URL)
if err != nil {
return fmt.Errorf("Error getting original gif: %s", err.Error())
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if resp.StatusCode != 200 {
log.Printf("response code: %s", resp.Status)
log.Fatalf("response: %s", body)
}
// create tmp gif file
file, err := os.Create("/tmp/tmpGif")
if err != nil {
log.Fatalf("Error creating tmp file: %s", err.Error())
}
// write to tmp file
_, err = file.Write(body)
if err != nil {
log.Fatalf("Error writting gif to tmp file: %s", err.Error())
}
// print inline image
printImage(gifChoice.Images.Original.URL, file, body)
}
return nil
}
func tenorGifSearch(searchTerm string, count int) error {
log.Printf("Powered By Tenor")
log.Printf("Searching for %s...", searchTerm)
gifResp, err := tenorGetGifs(searchTerm, count)
if err != nil {
return err
}
return printCount(gifResp, count)
}
func printCount(gifResp TenorResponse, count int) error {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
perm := r.Perm(len(gifResp.Results))
for i := 0; i < count; i++ {
// get the next random result
result := gifResp.Results[perm[i]]
media := result.Media[0]
url := media.Gif.URL
body, err := tenorGetImage(url)
if err != nil {
return err
}
// create tmp gif file
file, err := os.Create("/tmp/tmpGif")
if err != nil {
return fmt.Errorf("Error creating tmp file: %s", err.Error())
}
// write to tmp file
_, err = file.Write(body)
if err != nil {
return fmt.Errorf("Error writting gif to tmp file: %s", err.Error())
}
// print inline image
printImage(url, file, body)
}
return nil
}
func printImage(url string, file *os.File, body []byte) {
b64FileName := base64.StdEncoding.EncodeToString([]byte(file.Name()))
b64FileContents := base64.StdEncoding.EncodeToString(body)
fmt.Printf("%s\n\033]1337;File=name=%s;inline=1:%s\a\n", url, b64FileName, b64FileContents)
}