masto-emoji-pack.go 1.3 KB

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