emojis.go 911 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package main
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "net/http"
  6. "net/url"
  7. )
  8. type Emojis []Emoji
  9. type Emoji struct {
  10. Shortcode string `json:"shortcode"`
  11. Url string `json:"url"`
  12. StaticUrl string `json:"static_url"`
  13. VisibleInPicker bool `json:"visible_in_picker"`
  14. Category string `json:"category"`
  15. }
  16. func NewEmojiList(domain string) (Emojis, error) {
  17. emojis := Emojis{}
  18. bytes, err := fetchCustomEmojis(domain)
  19. if err != nil {
  20. return emojis, err
  21. }
  22. if err := json.Unmarshal(bytes, &emojis); err != nil {
  23. return emojis, err
  24. }
  25. return emojis, nil
  26. }
  27. func fetchCustomEmojis(domain string) ([]byte, error) {
  28. u, err := url.Parse("https://example.com/api/v1/custom_emojis")
  29. if err != nil {
  30. return nil, err
  31. }
  32. u.Host = domain
  33. resp, err := http.Get(u.String())
  34. if err != nil {
  35. return nil, err
  36. }
  37. defer resp.Body.Close()
  38. return ioutil.ReadAll(resp.Body)
  39. }