masto-emoji-pack.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package main
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "strings"
  6. )
  7. type Result struct {
  8. Server string
  9. Output []string
  10. Error error
  11. }
  12. func main() {
  13. opts := parseOptions()
  14. c := make(chan Result, 5)
  15. defer close(c)
  16. for _, d := range opts.Servers {
  17. go saveEmojiList(d, opts, c)
  18. }
  19. for _, _ = range opts.Servers {
  20. if r := <-c; r.Error != nil {
  21. fmt.Println(r.Error)
  22. } else {
  23. fmt.Printf("Success: %s\n", r.Server)
  24. for _, name := range r.Output {
  25. fmt.Println(name)
  26. }
  27. }
  28. }
  29. }
  30. func saveEmojiList(domain string, opts Options, c chan Result) {
  31. r := Result{
  32. Server: domain,
  33. }
  34. var es Emojis
  35. es, r.Error = NewEmojiList(domain)
  36. if r.Error != nil {
  37. c <- r
  38. return
  39. }
  40. out := filepath.Join(opts.OutputDir, strings.Replace(domain, ".", "_", -1))
  41. if !opts.Split {
  42. p := NewEmojiPack()
  43. p.SetFiles(es)
  44. r.Error = p.GenerateEmojiPack(out)
  45. r.Output = append(r.Output, out)
  46. c <- r
  47. return
  48. }
  49. var ces = map[string]Emojis{}
  50. for _, e := range es {
  51. ces[e.Category] = append(ces[e.Category], e)
  52. }
  53. for c, es := range ces {
  54. p := NewEmojiPack()
  55. p.SetFiles(es)
  56. dir := filepath.Join(out, c)
  57. if err := p.GenerateEmojiPack(dir); err != nil {
  58. r.Error = err
  59. }
  60. r.Output = append(r.Output, dir)
  61. }
  62. c <- r
  63. }