🎉 It works
This commit is contained in:
commit
107968803c
5 changed files with 114 additions and 0 deletions
3
go.mod
Normal file
3
go.mod
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
module codeberg.org/danjones000/slackmojidl
|
||||||
|
|
||||||
|
go 1.22.2
|
||||||
84
main.go
Normal file
84
main.go
Normal file
|
|
@ -0,0 +1,84 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
|
||||||
|
"codeberg.org/danjones000/slackmojidl/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func die(v any) {
|
||||||
|
fmt.Fprintln(os.Stderr, v)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if len(os.Args) < 2 {
|
||||||
|
die("Missing JSON file")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(os.Args) < 3 {
|
||||||
|
die("Missing directory")
|
||||||
|
}
|
||||||
|
|
||||||
|
dir := os.Args[2]
|
||||||
|
|
||||||
|
by, err := os.ReadFile(os.Args[1])
|
||||||
|
if err != nil {
|
||||||
|
die(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
list := models.List{}
|
||||||
|
err = json.Unmarshal(by, &list)
|
||||||
|
if err != nil {
|
||||||
|
die(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, emoji := range list.Emoji {
|
||||||
|
dl(dir, emoji)
|
||||||
|
saveJson(dir, emoji)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func dl(dir string, emoji models.Emoji) {
|
||||||
|
ext := path.Ext(emoji.Url)
|
||||||
|
outPath := path.Join(dir, emoji.Name+"."+ext)
|
||||||
|
out, err := os.Create(outPath)
|
||||||
|
if err != nil {
|
||||||
|
die(err)
|
||||||
|
}
|
||||||
|
defer out.Close()
|
||||||
|
|
||||||
|
resp, err := http.Get(emoji.Url)
|
||||||
|
if err != nil {
|
||||||
|
die(err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
_, err = io.Copy(out, resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
die(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Saved", emoji.Name, "to", outPath)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func saveJson(dir string, emoji models.Emoji) {
|
||||||
|
outPath := path.Join(dir, emoji.Name+".json")
|
||||||
|
out, err := os.Create(outPath)
|
||||||
|
if err != nil {
|
||||||
|
die(err)
|
||||||
|
}
|
||||||
|
defer out.Close()
|
||||||
|
|
||||||
|
enc := json.NewEncoder(out)
|
||||||
|
enc.SetIndent("", " ")
|
||||||
|
enc.Encode(emoji)
|
||||||
|
|
||||||
|
fmt.Println("Saved data to ", outPath)
|
||||||
|
}
|
||||||
11
models/emoji.go
Normal file
11
models/emoji.go
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
package models
|
||||||
|
|
||||||
|
type Emoji struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
IsAlias uint16 `json:"is_alias"`
|
||||||
|
AliasFor string `json:"alias_for"`
|
||||||
|
Url string `json:"url"`
|
||||||
|
IsBad bool `json:"is_bad"`
|
||||||
|
Synonyms []string `json:"synonyms"`
|
||||||
|
UserDisplayName string `json:"user_display_name"`
|
||||||
|
}
|
||||||
8
models/list.go
Normal file
8
models/list.go
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
package models
|
||||||
|
|
||||||
|
type List struct {
|
||||||
|
Ok bool
|
||||||
|
Emoji []Emoji
|
||||||
|
DisabledEmoji []any `json:"disabled_emoji"`
|
||||||
|
Paging Paging
|
||||||
|
}
|
||||||
8
models/paging.go
Normal file
8
models/paging.go
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
package models
|
||||||
|
|
||||||
|
type Paging struct {
|
||||||
|
Count uint32
|
||||||
|
Total uint32
|
||||||
|
Page uint32
|
||||||
|
Pages uint32
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue