-
Notifications
You must be signed in to change notification settings - Fork 0
/
tenor.go
107 lines (90 loc) · 2.09 KB
/
tenor.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
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/osm/irc"
)
// Define Tenor errors
var (
TenorNoAPIKey = errors.New("You need to set a Tenor API key")
TenorNothingFound = errors.New("Nothing found")
)
// iniTenorDefaults sets default values for all settings.
func (b *bot) initTenorDefaults() {
if b.IRC.TenorCmd == "" {
b.IRC.TenorCmd = "!tenor"
}
if b.IRC.TenorLang == "" {
b.IRC.TenorLang = "en_US"
}
if b.IRC.TenorMsgNothingFound == "" {
b.IRC.TenorMsgNothingFound = "nothing found"
}
}
// tenorHandler handles the Tenor integration.
func (b *bot) tenorHandler(m *irc.Message) {
a := b.parseAction(m).(*privmsgAction)
if !a.validChannel {
return
}
if a.cmd != b.IRC.TenorCmd {
return
}
if b.shouldIgnore(m) {
return
}
if b.IRC.TenorAPIKey == "" {
b.logger.Printf("tenorHandler: you need to set a Tenor API key\n")
return
}
url, err := b.tenorSearch(a.msg)
if err == TenorNothingFound {
b.privmsg(b.IRC.TenorMsgNothingFound)
} else if url != "" {
b.privmsg(url)
}
}
// tenorSearch search and return a random gif for the given query.
func (b *bot) tenorSearch(query string) (string, error) {
if b.IRC.TenorAPIKey == "" {
b.logger.Printf("tenorSearch: you need to set a Tenor API key\n")
return "", TenorNoAPIKey
}
url := fmt.Sprintf(
"https://api.tenor.com/v1/random?key=%s&q=%s&locale=%s&media_filter=minimal",
b.IRC.TenorAPIKey,
strings.Replace(strings.Replace(query, fmt.Sprintf("%s ", b.IRC.TenorCmd), "", 1), " ", "%20", -1),
b.IRC.TenorLang,
)
res, err := http.Get(url)
if err != nil {
b.logger.Printf("tenor: %v", err)
return "", err
}
defer res.Body.Close()
data, err := ioutil.ReadAll(res.Body)
if err != nil {
b.logger.Printf("tenor: %v", err)
return "", err
}
var d struct {
Results []struct {
Media []map[string]struct {
URL string `json:"url"`
}
}
}
err = json.Unmarshal(data, &d)
if err != nil {
b.logger.Printf("tenor: %v", err)
return "", err
}
if len(d.Results) == 0 {
return "", TenorNothingFound
}
return d.Results[0].Media[0]["gif"].URL, nil
}