|
@@ -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
|
|
|
}
|