master
pocketjawa 2024-05-30 15:14:16 -04:00
commit e1d4217ac9
1 changed files with 72 additions and 0 deletions

72
emoji2json.go Normal file
View File

@ -0,0 +1,72 @@
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"strings"
)
func fileNameWithoutExtension(fileName string) string {
if pos := strings.LastIndexByte(fileName, '.'); pos != -1 {
return fileName[:pos]
}
return fileName
}
func main() {
CategoryFlag := flag.String("c", "", "Name of the emoji category")
flag.Parse()
// Remove old meta.json
_, err := os.Stat("meta.json")
if !os.IsNotExist(err) {
err = os.Remove("meta.json")
if err != nil {
fmt.Println(err)
}
}
// Get files list
f, err := os.Open("./")
if err != nil {
fmt.Println(err)
return
}
files, err := f.Readdir(0)
if err != nil {
fmt.Println(err)
return
}
// Init metadata
meta := &MetaPackage{}
// Loop through files
for _, file := range files {
emoji := Emoji{
Downloaded: true,
FileName: file.Name(),
Emoji: EmojiInfo{
Name: fileNameWithoutExtension(file.Name()),
Category: *CategoryFlag,
},
}
meta.Emojis = append(meta.Emojis, emoji)
}
// Write JSON file
metaData, _ := json.MarshalIndent(meta, "", " ")
_ = os.WriteFile("meta.json", metaData, 0644)
}
type MetaPackage struct {
Emojis []Emoji `json:"emojis"`
}
type Emoji struct {
Downloaded bool `json:"downloaded"`
FileName string `json:"fileName"`
Emoji EmojiInfo `json:"emoji"`
}
type EmojiInfo struct {
Name string `json:"name"`
Category string `json:"category"`
// Aliases []string `json:"aliases"`
}