mirror of https://github.com/velour/catbase.git
connectors: add translation layer
* URLs can be translated to slack/discord compatible * Emojy have a connector-specific translation configuration * Advent of Code plugin respects emojy and URLs * Config can be wrapped in `` for JSON
This commit is contained in:
parent
c8e1956099
commit
9dbdd1f9a3
|
@ -142,6 +142,12 @@ type Connector interface {
|
||||||
|
|
||||||
// Profile returns a user's information given an ID
|
// Profile returns a user's information given an ID
|
||||||
Profile(string) (user.User, error)
|
Profile(string) (user.User, error)
|
||||||
|
|
||||||
|
// URL Format utility
|
||||||
|
URLFormat(title, url string) string
|
||||||
|
|
||||||
|
// Translate emojy to/from services
|
||||||
|
Emojy(string) string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Plugin interface used for compatibility with the Plugin interface
|
// Plugin interface used for compatibility with the Plugin interface
|
||||||
|
|
|
@ -113,7 +113,8 @@ func NewMockBot() *MockBot {
|
||||||
return &b
|
return &b
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mb *MockBot) GetPluginNames() []string { return nil }
|
func (mb *MockBot) GetPluginNames() []string { return nil }
|
||||||
func (mb *MockBot) RefreshPluginBlacklist() error { return nil }
|
func (mb *MockBot) RefreshPluginBlacklist() error { return nil }
|
||||||
func (mb *MockBot) RefreshPluginWhitelist() error { return nil }
|
func (mb *MockBot) RefreshPluginWhitelist() error { return nil }
|
||||||
func (mb *MockBot) GetWhitelist() []string { return []string{} }
|
func (mb *MockBot) GetWhitelist() []string { return []string{} }
|
||||||
|
func (mb *MockBot) URLFormat(title, url string) string { return title + url }
|
||||||
|
|
|
@ -144,6 +144,7 @@ func (c *Config) Unset(key string) error {
|
||||||
// Note, this is always a string. Use the SetArray for an array helper
|
// Note, this is always a string. Use the SetArray for an array helper
|
||||||
func (c *Config) Set(key, value string) error {
|
func (c *Config) Set(key, value string) error {
|
||||||
key = strings.ToLower(key)
|
key = strings.ToLower(key)
|
||||||
|
value = strings.Trim(value, "`")
|
||||||
q := `insert into config (key,value) values (?, ?)
|
q := `insert into config (key,value) values (?, ?)
|
||||||
on conflict(key) do update set value=?;`
|
on conflict(key) do update set value=?;`
|
||||||
tx, err := c.Begin()
|
tx, err := c.Begin()
|
||||||
|
|
|
@ -219,3 +219,15 @@ func (d *Discord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreate
|
||||||
|
|
||||||
d.event(d, bot.Message, msg)
|
d.event(d, bot.Message, msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *Discord) Emojy(name string) string {
|
||||||
|
e := d.config.GetMap("discord.emojy", map[string]string{})
|
||||||
|
if emojy, ok := e[name]; ok {
|
||||||
|
return emojy
|
||||||
|
}
|
||||||
|
return name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Discord) URLFormat(title, url string) string {
|
||||||
|
return fmt.Sprintf("%s (%s)", title, url)
|
||||||
|
}
|
||||||
|
|
|
@ -314,3 +314,15 @@ func (i Irc) Who(channel string) []string {
|
||||||
func (i Irc) Profile(string) (user.User, error) {
|
func (i Irc) Profile(string) (user.User, error) {
|
||||||
return user.User{}, fmt.Errorf("unimplemented")
|
return user.User{}, fmt.Errorf("unimplemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i Irc) URLFormat(title, url string) string {
|
||||||
|
return fmt.Sprintf("%s (%s)", title, url)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i Irc) Emojy(name string) string {
|
||||||
|
e := i.config.GetMap("irc.emojy", map[string]string{})
|
||||||
|
if emojy, ok := e[name]; ok {
|
||||||
|
return emojy
|
||||||
|
}
|
||||||
|
return name
|
||||||
|
}
|
||||||
|
|
|
@ -734,3 +734,15 @@ func (s *Slack) Who(id string) []string {
|
||||||
func (s *Slack) Profile(string) (user.User, error) {
|
func (s *Slack) Profile(string) (user.User, error) {
|
||||||
return user.User{}, fmt.Errorf("unimplemented")
|
return user.User{}, fmt.Errorf("unimplemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Slack) Emojy(name string) string {
|
||||||
|
e := s.config.GetMap("slack.emojy", map[string]string{})
|
||||||
|
if emojy, ok := e[name]; ok {
|
||||||
|
return emojy
|
||||||
|
}
|
||||||
|
return name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Slack) URLFormat(title, url string) string {
|
||||||
|
return fmt.Sprintf("<%s|%s>", url, title)
|
||||||
|
}
|
||||||
|
|
|
@ -682,3 +682,17 @@ func (s *SlackApp) Profile(identifier string) (user.User, error) {
|
||||||
|
|
||||||
return user.User{}, fmt.Errorf("user %s not found", err)
|
return user.User{}, fmt.Errorf("user %s not found", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *SlackApp) Emojy(name string) string {
|
||||||
|
e := s.config.GetMap("slack.emojy", map[string]string{})
|
||||||
|
if emojy, ok := e[name]; ok {
|
||||||
|
log.Debug().Msgf("Found emoji %s for %s", emojy, name)
|
||||||
|
return emojy
|
||||||
|
}
|
||||||
|
log.Debug().Msgf("Found no emojy for %s", name)
|
||||||
|
return name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SlackApp) URLFormat(title, url string) string {
|
||||||
|
return fmt.Sprintf("<%s|%s>", url, title)
|
||||||
|
}
|
||||||
|
|
|
@ -88,7 +88,8 @@ func (p *AOC) message(c bot.Connector, kind bot.Kind, message msg.Message, args
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
msg := fmt.Sprintf("AoC <https://adventofcode.com/%d/leaderboard/private/view/%d|Leaderboard>:\n", year, boardId)
|
link := c.URLFormat("leaderboard", fmt.Sprintf("https://adventofcode.com/%d/leaderboard/private/view/%d", year, boardId))
|
||||||
|
msg := fmt.Sprintf("AoC %s:\n", link)
|
||||||
for _, m := range members {
|
for _, m := range members {
|
||||||
if m.Stars == 0 {
|
if m.Stars == 0 {
|
||||||
continue
|
continue
|
||||||
|
@ -96,11 +97,11 @@ func (p *AOC) message(c bot.Connector, kind bot.Kind, message msg.Message, args
|
||||||
trophy := ""
|
trophy := ""
|
||||||
switch m.ID {
|
switch m.ID {
|
||||||
case goldID:
|
case goldID:
|
||||||
trophy = ":gold-trophy:"
|
trophy = c.Emojy(":gold-trophy:")
|
||||||
case silverID:
|
case silverID:
|
||||||
trophy = ":silver-trophy:"
|
trophy = c.Emojy(":silver-trophy:")
|
||||||
case bronzeID:
|
case bronzeID:
|
||||||
trophy = ":bronze-trophy:"
|
trophy = c.Emojy(":bronze-trophy:")
|
||||||
}
|
}
|
||||||
msg += fmt.Sprintf("%s has %d :star: for a score of %d%s\n", m.Name, m.Stars, m.LocalScore, trophy)
|
msg += fmt.Sprintf("%s has %d :star: for a score of %d%s\n", m.Name, m.Stars, m.LocalScore, trophy)
|
||||||
}
|
}
|
||||||
|
|
|
@ -117,6 +117,10 @@ func (p *CliPlugin) Send(kind bot.Kind, args ...interface{}) (string, error) {
|
||||||
func (p *CliPlugin) GetEmojiList() map[string]string { return nil }
|
func (p *CliPlugin) GetEmojiList() map[string]string { return nil }
|
||||||
func (p *CliPlugin) Serve() error { return nil }
|
func (p *CliPlugin) Serve() error { return nil }
|
||||||
func (p *CliPlugin) Who(s string) []string { return nil }
|
func (p *CliPlugin) Who(s string) []string { return nil }
|
||||||
func (s *CliPlugin) Profile(name string) (user.User, error) {
|
func (p *CliPlugin) Profile(name string) (user.User, error) {
|
||||||
return user.User{}, fmt.Errorf("unimplemented")
|
return user.User{}, fmt.Errorf("unimplemented")
|
||||||
}
|
}
|
||||||
|
func (p *CliPlugin) Emojy(name string) string { return name }
|
||||||
|
func (p *CliPlugin) URLFormat(title, url string) string {
|
||||||
|
return fmt.Sprintf("%s (%s)", title, url)
|
||||||
|
}
|
||||||
|
|
|
@ -11,25 +11,32 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
token = flag.String("token", "", "Slack API token")
|
token = flag.String("token", "", "Slack API token")
|
||||||
channel = flag.String("channel", "", "Slack channel ID")
|
channel = flag.String("channel", "", "Slack channel ID")
|
||||||
limit = flag.Int("limit", 10000, "Number of items to return")
|
limit = flag.Int("limit", 10000, "Number of items to return")
|
||||||
types = flag.String("types", "images,pdfs", "Type of object")
|
types = flag.String("types", "images,pdfs,video", "Type of object")
|
||||||
path = flag.String("path", "./", "Path to save files")
|
path = flag.String("path", "./", "Path to save files")
|
||||||
|
to = flag.String("to", "", "Time limit in '2006-01-02T15:04:05Z07:00' format. Default -30d")
|
||||||
|
rateLimit = flag.Int("rate", 1, "rate limit in seconds")
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
||||||
|
|
||||||
|
ticker := time.NewTicker(time.Second * time.Duration(*rateLimit))
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
files, count := getFiles()
|
files, count := getFiles()
|
||||||
|
log.Debug().Msgf("Got %d files, count is %d", len(files), count)
|
||||||
for _, f := range files {
|
for _, f := range files {
|
||||||
downloadFile(f)
|
downloadFile(f)
|
||||||
deleteFile(f)
|
deleteFile(f)
|
||||||
|
@ -37,27 +44,47 @@ func main() {
|
||||||
if count == 1 {
|
if count == 1 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
<-ticker.C
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getFiles() ([]slackFile, int) {
|
func getFiles() ([]slackFile, int) {
|
||||||
files := fileResp{}
|
files := fileResp{}
|
||||||
|
|
||||||
|
var toTime time.Time
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if *to == "" {
|
||||||
|
toTime = time.Now().Add(time.Hour * 24 * 30 * -1)
|
||||||
|
} else {
|
||||||
|
toTime, err = time.Parse(time.RFC3339, *to)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal().Err(err).Msg("Error reading time format")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
log.Debug().Msg("Getting files")
|
log.Debug().Msg("Getting files")
|
||||||
body := mkReq("https://slack.com/api/files.list",
|
body := mkReq("https://slack.com/api/files.list",
|
||||||
"token", *token,
|
"token", *token,
|
||||||
"count", strconv.Itoa(*limit),
|
"count", strconv.Itoa(*limit),
|
||||||
"types", *types,
|
"types", *types,
|
||||||
|
"ts_to", strconv.FormatInt(toTime.Unix(), 10),
|
||||||
)
|
)
|
||||||
|
|
||||||
err := json.Unmarshal(body, &files)
|
err = json.Unmarshal(body, &files)
|
||||||
checkErr(err)
|
checkErr(err)
|
||||||
|
|
||||||
log.Info().
|
log.Info().
|
||||||
Int("count", files.Paging.Count).
|
Int("count", files.Paging.Count).
|
||||||
Bool("ok", files.Ok)
|
Int("total", files.Paging.Total).
|
||||||
|
Bool("ok", files.Ok).
|
||||||
|
Msg("file result")
|
||||||
if !files.Ok {
|
if !files.Ok {
|
||||||
log.Error().Interface("files", files)
|
log.Error().
|
||||||
|
Interface("files", files).
|
||||||
|
Str("body", string(body)).
|
||||||
|
Msg("Error getting files")
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
return files.Files, files.Paging.Pages
|
return files.Files, files.Paging.Pages
|
||||||
|
|
Loading…
Reference in New Issue