Update the emoji data up to Unicode Version 1.14. Remove the emoji generation logic from lib. (#3)

* Update the emoji data up to Unicode Version 1.14. Remove the emoji generation logic from lib.

Co-authored-by: Vlad Gukasov <v.gukasov@space307.com>
This commit is contained in:
Vlad Gukasov 2021-12-26 16:24:03 +03:00 committed by GitHub
parent 5fcf0e4051
commit e4ab28b27c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 5259 additions and 4957 deletions

View File

@ -53,7 +53,7 @@ func main() {
```
## Get all
The function returns all existed emojis. You can do whatever you need with the list.
The function returns all existing emojis. You can do whatever you need with the list.
```go
package main

View File

@ -1,81 +0,0 @@
// The CLI tool generates fetches fresh emojis from the Openemoji and saves them in data.go.
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"text/template"
"unicode/utf8"
"github.com/forPelevin/gomoji"
)
const (
outputFile = "../../data.go"
)
var (
flagAPIKey = flag.String("apikey", "", "Openemoji API key")
)
func main() {
flag.Parse()
if *flagAPIKey == "" {
log.Fatalf("invalid Openemoji API key")
}
s := gomoji.NewService(gomoji.NewOpenEmojiProvider(*flagAPIKey))
emojis, err := s.AllEmojis(context.Background())
if err != nil {
log.Fatalf("all emojis: %s", err)
}
emojiMap := make(map[int32]gomoji.Emoji, len(emojis))
for _, e := range emojis {
r, _ := utf8.DecodeRune([]byte(e.Character))
emojiMap[r] = e
}
tplFile, err := template.New("").Parse(textTplFile)
if err != nil {
log.Fatalf("failed to get current directory path: %s", err)
}
output, err := os.Create(outputFile)
if err != nil {
log.Fatalf("failed to create output file: %s", err)
}
if err = tplFile.Execute(output, emojiMap); err != nil {
log.Fatalf("failed to execute template: %s", err)
}
fmt.Println("Emoji entities successfully generated")
}
const (
textTplFile = `// Code generated by generator.go ; DO NOT EDIT.
package gomoji
var (
emojiMap = map[int32]Emoji{
{{ range $index, $val := . }}
{{ $index }}: {
Slug: "{{ $val.Slug }}",
Character: "{{ $val.Character }}",
UnicodeName: "{{ $val.UnicodeName }}",
CodePoint: "{{ $val.CodePoint }}",
Group: "{{ $val.Group }}",
SubGroup: "{{ $val.SubGroup }}",
},
{{ end }}
}
)`
)

9526
data.go

File diff suppressed because it is too large Load Diff

View File

@ -1 +0,0 @@
openemoji

View File

@ -1,6 +0,0 @@
openemoji: clean
go get -u github.com/forPelevin/gomoji
go build
clean:
rm -f openemoji

View File

@ -1,24 +0,0 @@
# Using Open Emoji API service provider example
* Check whether a string contains an emoji.
* Get all available emojis
# How to build
```
make
```
# How to run
```
./openemoji -accessKey=XXXXX
```
You can get access key [here](https://emoji-api.com) after signing up
# Example output
```sh
String: Hello world!, contains emoji: false
String: Hello world 🤗, contains emoji: true
Emojis count: 1357
```

View File

@ -1,50 +0,0 @@
package main
import (
"context"
"flag"
"fmt"
"log"
"github.com/forPelevin/gomoji"
)
var (
accessKey = flag.String("accessKey", "", "Access key to interact with Open Emoji API. You can get it from https://emoji-api.com.")
)
func main() {
flag.Parse()
s := gomoji.NewService(gomoji.NewOpenEmojiProvider(*accessKey))
containsEmoji(s)
allEmojis(s)
}
func containsEmoji(s *gomoji.Service) {
stringWithoutEmoji := "Hello world!"
res, err := s.ContainsEmoji(context.Background(), stringWithoutEmoji)
if err != nil {
log.Fatalf("Contains emoji: %s", err)
}
fmt.Printf("String: %s, contains emoji: %t\n", stringWithoutEmoji, res) // false
stringWithEmoji := "Hello world 🤗"
res, err = s.ContainsEmoji(context.Background(), stringWithEmoji)
if err != nil {
log.Fatalf("Contains emoji: %s", err)
}
fmt.Printf("String: %s, contains emoji: %t\n", stringWithEmoji, res) // true
}
func allEmojis(s *gomoji.Service) {
emojis, err := s.AllEmojis(context.Background())
if err != nil {
log.Fatalf("All emojis: %s", err)
}
fmt.Printf("Emojis count: %d\n", len(emojis))
}

18
go.mod
View File

@ -1,19 +1,5 @@
module github.com/forPelevin/gomoji
go 1.13
go 1.17
require (
github.com/andybalholm/brotli v1.0.1 // indirect
github.com/golangci/errcheck v0.0.0-20181223084120-ef45e06d44b6 // indirect
github.com/golangci/gocyclo v0.0.0-20180528144436-0a533e8fa43d // indirect
github.com/golangci/golangci-lint v1.41.1 // indirect
github.com/golangci/ineffassign v0.0.0-20190609212857-42439a7714cc // indirect
github.com/golangci/prealloc v0.0.0-20180630174525-215b22d4de21 // indirect
github.com/jmoiron/sqlx v1.2.1-0.20190826204134-d7d95172beb5 // indirect
github.com/klauspost/compress v1.11.4 // indirect
github.com/shirou/gopsutil v0.0.0-20190901111213-e4ec7b275ada // indirect
github.com/tidwall/gjson v1.6.7
github.com/tomarrell/wrapcheck v0.0.0-20200807122107-df9e8bcb914d // indirect
github.com/tommy-muehle/go-mnd v1.3.1-0.20201008215730-16041ac3fe65 // indirect
github.com/valyala/fasthttp v1.18.0
)
require github.com/golangci/golangci-lint v1.43.0 // indirect

437
go.sum

File diff suppressed because it is too large Load Diff

View File

@ -1,71 +0,0 @@
package gomoji
import (
"context"
"errors"
"fmt"
"github.com/tidwall/gjson"
"github.com/valyala/fasthttp"
)
var (
errInvalidJSONInResponse = errors.New("invalid JSON in response body")
)
// OpenEmojiProvider is an emoji provider that gets data from Open Emoji API (https://emoji-api.com).
type OpenEmojiProvider struct {
accessKey string
}
// NewOpenEmojiProvider creates a new instance of OpenEmojiProvider.
func NewOpenEmojiProvider(accessKey string) *OpenEmojiProvider {
return &OpenEmojiProvider{
accessKey: accessKey,
}
}
// AllEmojis gets all emojis from emoji-api.com.
func (o *OpenEmojiProvider) AllEmojis(ctx context.Context) ([]Emoji, error) {
respBody, err := o.doRequest("https://emoji-api.com/emojis?access_key=" + o.accessKey)
if err != nil {
return nil, fmt.Errorf("do request: %w", err)
}
if !gjson.ValidBytes(respBody) {
return nil, errInvalidJSONInResponse
}
res := gjson.ParseBytes(respBody)
gjsonEmojiList := res.Array()
emojis := make([]Emoji, len(gjsonEmojiList))
for i, gjonEmoji := range gjsonEmojiList {
emojis[i] = Emoji{
Slug: gjonEmoji.Get("slug").String(),
Character: gjonEmoji.Get("character").String(),
UnicodeName: gjonEmoji.Get("unicodeName").String(),
CodePoint: gjonEmoji.Get("codePoint").String(),
Group: gjonEmoji.Get("group").String(),
SubGroup: gjonEmoji.Get("subGroup").String(),
}
}
return emojis, nil
}
func (o *OpenEmojiProvider) doRequest(uri string) ([]byte, error) {
req := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(req)
resp := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(resp)
req.SetRequestURI(uri)
err := fasthttp.Do(req, resp)
if err != nil {
return nil, fmt.Errorf("do fasthttp request: %w", err)
}
return resp.Body(), nil
}