catbase/util/emojy/main.go

128 lines
2.2 KiB
Go
Raw Permalink Normal View History

package main
import (
"encoding/json"
"flag"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
2019-03-07 16:35:42 +00:00
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
var (
token = flag.String("token", "", "Slack API token")
path = flag.String("path", "./files", "Path to save files")
)
func main() {
flag.Parse()
2019-03-07 16:35:42 +00:00
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
if *token == "" {
2019-03-07 16:35:42 +00:00
log.Fatal().Msg("No token provided.")
return
}
files := getFiles()
for n, f := range files {
downloadFile(n, f)
}
}
func getFiles() map[string]string {
files := fileResp{}
2019-03-07 16:35:42 +00:00
log.Debug().Msgf("Getting files")
body := mkReq("https://slack.com/api/emoji.list",
"token", *token,
)
err := json.Unmarshal(body, &files)
checkErr(err)
2019-03-07 16:35:42 +00:00
log.Debug().Msgf("Ok: %v", files.Ok)
if !files.Ok {
2019-03-07 16:35:42 +00:00
log.Debug().Msgf("%+v", files)
}
return files.Files
}
func downloadFile(n, f string) {
url := strings.Replace(f, "\\", "", -1) // because fuck slack
if strings.HasPrefix(url, "alias:") {
2019-03-07 16:35:42 +00:00
log.Debug().Msgf("Skipping alias: %s", url)
return
}
fileNameParts := strings.Split(url, `/`)
ext := fileNameParts[len(fileNameParts)-1]
fileNameParts = strings.Split(ext, ".")
ext = fileNameParts[len(fileNameParts)-1]
fname := filepath.Join(*path, n+"."+ext)
2019-03-07 16:35:42 +00:00
log.Debug().Msgf("Downloading from: %s", url)
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
checkErr(err)
req.Header.Add("Authorization", "Bearer "+*token)
resp, err := client.Do(req)
checkErr(err)
defer resp.Body.Close()
out, err := os.Create(fname)
checkErr(err)
defer out.Close()
io.Copy(out, resp.Body)
2019-03-07 16:35:42 +00:00
log.Debug().Msgf("Downloaded %s", f)
}
func checkErr(err error) {
if err != nil {
2019-03-07 16:35:42 +00:00
log.Fatal().Err(err)
}
}
func mkReq(path string, arg ...string) []byte {
if len(arg)%2 != 0 {
2019-03-07 16:35:42 +00:00
log.Fatal().Msg("Bad request arg number.")
}
u, err := url.Parse(path)
checkErr(err)
q := u.Query()
for i := 0; i < len(arg); i += 2 {
key := arg[i]
val := arg[i+1]
if val != "" {
q.Set(key, val)
}
}
u.RawQuery = q.Encode()
resp, err := http.Get(u.String())
checkErr(err)
body, err := ioutil.ReadAll(resp.Body)
checkErr(err)
return body
}
type fileResp struct {
Ok bool `json:"ok"`
Files map[string]string `json:"emoji"`
}