/
identify.go
190 lines (159 loc) · 4.3 KB
/
identify.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
187
188
189
190
package cmd
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/Necroforger/dgrouter/exrouter"
"github.com/animenotifier/anilist"
"github.com/bwmarrin/discordgo"
"github.com/darenliang/MikuBotGo/config"
"github.com/darenliang/MikuBotGo/framework"
"github.com/disintegration/imaging"
"image/jpeg"
"net/url"
"os"
"strconv"
"strings"
)
var TraceMoeBase string
type TraceData struct {
Docs []struct {
AnilistID int `json:"anilist_id"`
At float64 `json:"at"`
Filename string `json:"filename"`
Episode interface{} `json:"episode,string"`
Tokenthumb string `json:"tokenthumb"`
Similarity float64 `json:"similarity"`
MalID int `json:"mal_id"`
} `json:"docs"`
}
func init() {
TraceMoeKey := os.Getenv("TRACEMOE")
if TraceMoeKey != "" {
TraceMoeBase = fmt.Sprintf("https://trace.moe/api/search?token=%s", TraceMoeKey)
} else {
TraceMoeBase = "https://trace.moe/api/search"
}
}
func getPostJson(data string, target interface{}) error {
form := url.Values{
"image": {data},
}
r, err := framework.HttpClient.PostForm(TraceMoeBase, form)
if r != nil {
defer r.Body.Close()
}
if err != nil {
return err
}
err = json.NewDecoder(r.Body).Decode(target)
if err != nil {
return err
}
return nil
}
// Identify command
func Identify(ctx *exrouter.Context) {
prefix := framework.PDB.GetPrefix(ctx.Msg.GuildID)
query := strings.TrimSpace(ctx.Args.After(1))
URL := ""
if len(query) != 0 {
_, err := url.ParseRequestURI(query)
if err != nil {
ctx.Reply(":thinking: The URL you have provided is not valid.")
return
}
URL = query
}
if len(ctx.Msg.Attachments) == 0 && URL == "" {
ctx.Reply(fmt.Sprintf(":information_source: Usage: `%sidentify <anime screenshot (attachment or url)>`", prefix))
return
}
if URL == "" {
URL = ctx.Msg.Attachments[0].URL
}
ctx.Ses.MessageReactionAdd(ctx.Msg.ChannelID, ctx.Msg.ID, config.Timer)
defer ctx.Ses.MessageReactionRemove(ctx.Msg.ChannelID, ctx.Msg.ID, config.Timer, ctx.Ses.State.User.ID)
image, err := framework.LoadImage(URL)
if err != nil {
ctx.Reply(":cry: There's an issue with your image.")
return
}
image = imaging.Resize(image, 0, 480, imaging.Lanczos)
var buf bytes.Buffer
err = jpeg.Encode(&buf, image, &jpeg.Options{Quality: 35})
photoBase64 := base64.StdEncoding.EncodeToString(buf.Bytes())
trace := TraceData{}
err = getPostJson(photoBase64, &trace)
if err != nil {
ctx.Reply(":cry: An error has occurred.")
return
}
if len(trace.Docs) == 0 {
ctx.Reply(":cry: We can't find any sauce from the provided image.")
return
}
if trace.Docs[0].Similarity < 0.87 {
ctx.Reply(":cry: We can't find the sauce for you.")
return
}
response := framework.TraceSearchResult{}
err = anilist.Query(framework.AnilistAnimeIDQuery(trace.Docs[0].AnilistID), &response)
if err != nil {
ctx.Reply(":cry: We can't get anime info of the sauce.")
return
}
anime := response.Data.Media
var color uint64
if anime.CoverImage.Color != "" {
color, err = strconv.ParseUint(anime.CoverImage.Color[1:], 16, 64)
if err != nil {
color = config.EmbedColor
}
} else {
color = config.EmbedColor
}
episode := fmt.Sprint(trace.Docs[0].Episode)
if episode == "" {
episode = "Unknown"
}
embed := &discordgo.MessageEmbed{
Author: &discordgo.MessageEmbedAuthor{},
Color: int(color),
Fields: []*discordgo.MessageEmbedField{
{
Name: "Anime",
Value: anime.Title.UserPreferred,
Inline: true,
},
{
Name: "Episode Number",
Value: episode,
Inline: true,
},
{
Name: "Similarity",
Value: fmt.Sprintf("%.2f%%", trace.Docs[0].Similarity*100),
Inline: true,
},
{
Name: "MAL Link",
Value: fmt.Sprintf("https://myanimelist.net/anime/%d", trace.Docs[0].MalID),
Inline: false,
},
},
Title: fmt.Sprintf("Here's the anime %s#%s", ctx.Msg.Author.Username, ctx.Msg.Author.Discriminator),
Image: &discordgo.MessageEmbedImage{
URL: fmt.Sprintf("https://trace.moe/thumbnail.php?anilist_id=%d&file=%s&t=%g&token=%s",
trace.Docs[0].AnilistID,
url.QueryEscape(trace.Docs[0].Filename),
trace.Docs[0].At,
trace.Docs[0].Tokenthumb),
},
Thumbnail: &discordgo.MessageEmbedThumbnail{
URL: anime.CoverImage.ExtraLarge,
},
}
ctx.Ses.ChannelMessageSendEmbed(ctx.Msg.ChannelID, embed)
}