Browse Source

Refactor emoji download and parsing logic, now we can download Misskey emoji packs

poesty 1 year ago
parent
commit
3b13276661
2 changed files with 35 additions and 27 deletions
  1. 12 17
      emoji-pack.go
  2. 23 10
      emojis.go

+ 12 - 17
emoji-pack.go

@@ -33,7 +33,11 @@ func NewEmojiPack() EmojiPack {
 
 func (p *EmojiPack) SetFiles(es Emojis) {
 	for _, e := range es {
-		p.Files[e.Shortcode] = e.Url
+		k := e.Shortcode
+		if k == "" {
+			k = e.Name
+		}
+		p.Files[k] = e.Url
 	}
 }
 
@@ -54,13 +58,15 @@ func (p *EmojiPack) GenerateEmojiPack(outputDir string) error {
 		}
 	}
 
-	c := make(chan EmojiResult, 30)
+	c := make(chan EmojiResult, 20)
 	limitCh := make(chan struct{}, 20)
 	defer close(c)
 	defer close(limitCh)
 
 	for code, ru := range p.Files {
-		go downloadEmojiFile(outputDir, code, ru, c, limitCh)
+		limitCh <- struct{}{}
+		go downloadEmojiFile(outputDir, code, ru, c)
+		<-limitCh
 	}
 
 	files := map[string]string{}
@@ -98,26 +104,21 @@ func mkdir(name string) error {
 	return err
 }
 
-func downloadEmojiFile(outputDir string, shortcode string, fileUrl string, result chan EmojiResult, limit chan struct{}) {
-	limit <- struct{}{}
-
+func downloadEmojiFile(outputDir string, shortcode string, fileUrl string, result chan EmojiResult) {
 	r := EmojiResult{
 		Shortcode: shortcode,
 	}
+	defer func() { result <- r }()
 
 	var u *url.URL
 	u, r.Error = url.Parse(fileUrl)
 	if r.Error != nil {
-		<-limit
-		result <- r
 		return
 	}
 
 	var resp *http.Response
 	resp, r.Error = http.Get(u.String())
 	if r.Error != nil {
-		<-limit
-		result <- r
 		return
 	}
 	defer resp.Body.Close()
@@ -125,22 +126,16 @@ func downloadEmojiFile(outputDir string, shortcode string, fileUrl string, resul
 	var ueu string
 	ueu, r.Error = url.PathUnescape(u.String())
 	if r.Error != nil {
-		<-limit
-		result <- r
 		return
 	}
-	r.File = filepath.Base(ueu)
+	r.File = shortcode + filepath.Ext(filepath.Base(ueu))
 
 	var f *os.File
 	f, r.Error = os.Create(filepath.Join(outputDir, r.File))
 	if r.Error != nil {
-		<-limit
-		result <- r
 		return
 	}
 	defer f.Close()
 
 	_, r.Error = io.Copy(f, resp.Body)
-	<-limit
-	result <- r
 }

+ 23 - 10
emojis.go

@@ -7,29 +7,37 @@ import (
 	"net/url"
 )
 
+type Emojisp struct {
+	Emojis Emojis `json:"emojis"`
+}
+
 type Emojis []Emoji
 
 type Emoji struct {
-	Shortcode       string `json:"shortcode"`
-	Url             string `json:"url"`
-	StaticUrl       string `json:"static_url"`
-	VisibleInPicker bool   `json:"visible_in_picker"`
-	Category        string `json:"category"`
+	Shortcode string `json:"shortcode"`
+	Name      string `json:"name"`
+	Url       string `json:"url"`
+	// StaticUrl       string `json:"static_url"`
+	// VisibleInPicker bool   `json:"visible_in_picker"`
+	Category string `json:"category"`
 }
 
 func NewEmojiList(domain string) (Emojis, error) {
-	emojis := Emojis{}
+	emojis := Emojisp{}
 
 	bytes, err := fetchCustomEmojis(domain)
 	if err != nil {
-		return emojis, err
+		return emojis.Emojis, err
 	}
 
-	if err := json.Unmarshal(bytes, &emojis); err != nil {
-		return emojis, err
+	// fmt.Printf("%s\n", bytes)
+	if err := json.Unmarshal(bytes, &emojis.Emojis); err != nil {
+		if err = json.Unmarshal(bytes, &emojis); err != nil {
+			return emojis.Emojis, err
+		}
 	}
 
-	return emojis, nil
+	return emojis.Emojis, nil
 }
 
 func fetchCustomEmojis(domain string) ([]byte, error) {
@@ -40,6 +48,11 @@ func fetchCustomEmojis(domain string) ([]byte, error) {
 	u.Host = domain
 
 	resp, err := http.Get(u.String())
+	if resp.StatusCode != http.StatusOK {
+		// Try Misskey API Endpoint
+		u.Path = "/api/emojis"
+		resp, err = http.Get(u.String())
+	}
 	if err != nil {
 		return nil, err
 	}