2018-10-26 19:55:05 +00:00
|
|
|
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"
|
2018-10-26 19:55:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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})
|
2018-10-26 19:55:05 +00:00
|
|
|
|
|
|
|
if *token == "" {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Fatal().Msg("No token provided.")
|
2018-10-26 19:55:05 +00:00
|
|
|
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")
|
2018-10-26 19:55:05 +00:00
|
|
|
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)
|
2018-10-26 19:55:05 +00:00
|
|
|
if !files.Ok {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Debug().Msgf("%+v", files)
|
2018-10-26 19:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2018-10-26 19:55:05 +00:00
|
|
|
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)
|
2018-10-26 19:55:05 +00:00
|
|
|
|
|
|
|
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)
|
2018-10-26 19:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkErr(err error) {
|
|
|
|
if err != nil {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Fatal().Err(err)
|
2018-10-26 19:55:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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.")
|
2018-10-26 19:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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"`
|
|
|
|
}
|