Skip to content

Commit

Permalink
Merge pull request #17 from AlexPresso/prominent_color
Browse files Browse the repository at this point in the history
Cleaning and add prominent color
  • Loading branch information
AlexPresso authored Aug 5, 2024
2 parents a26fce5 + 40d331a commit 92ed850
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 9 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/alexpresso/zunivers-webhooks
go 1.17

require (
github.com/edlinorg/prominentcolor v1.0.0
github.com/go-co-op/gocron v1.37.0
github.com/spf13/viper v1.18.2
gorm.io/driver/sqlite v1.5.4
Expand All @@ -16,9 +17,12 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-sqlite3 v1.14.19 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect
github.com/oliamb/cutter v0.2.2 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,8 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8Yc
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/edlinorg/prominentcolor v1.0.0 h1:tDn/MqBkoLz7tQpcmrRtJm99muxUJrKTv75u27pa3bg=
github.com/edlinorg/prominentcolor v1.0.0/go.mod h1:21CfEEX5oq6dMJx3nPciSeMA0+odFt+piKOpmzUoq+Q=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand Down Expand Up @@ -1455,6 +1457,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/lyft/protoc-gen-star v0.6.0/go.mod h1:TGAoBVkt8w7MPG72TrKIu85MIdXwDuzJYeZuUPFPNwA=
github.com/lyft/protoc-gen-star v0.6.1/go.mod h1:TGAoBVkt8w7MPG72TrKIu85MIdXwDuzJYeZuUPFPNwA=
github.com/lyft/protoc-gen-star/v2 v2.0.1/go.mod h1:RcCdONR2ScXaYnQC5tUzxzlpA3WVYF7/opLeUgcQs/o=
Expand Down Expand Up @@ -1502,6 +1506,10 @@ github.com/nats-io/nats.go v1.31.0/go.mod h1:di3Bm5MLsoB4Bx61CBTsxuarI36WbhAwOm8
github.com/nats-io/nkeys v0.4.5/go.mod h1:XUkxdLPTufzlihbamfzQ7mw/VGx6ObUs+0bN5sNvt64=
github.com/nats-io/nkeys v0.4.6/go.mod h1:4DxZNzenSVd1cYQoAa8948QY3QDjrHfcfVADymtkpts=
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
github.com/oliamb/cutter v0.2.2 h1:Lfwkya0HHNU1YLnGv2hTkzHfasrSMkgv4Dn+5rmlk3k=
github.com/oliamb/cutter v0.2.2/go.mod h1:4BenG2/4GuRBDbVm/OPahDVqbrOemzpPiG5mi1iryBU=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
Expand Down
52 changes: 45 additions & 7 deletions services/webhookService.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,27 @@ import (
"fmt"
"github.com/alexpresso/zunivers-webhooks/structures/discord"
"github.com/alexpresso/zunivers-webhooks/utils"
"github.com/edlinorg/prominentcolor"
"github.com/spf13/viper"
"net/http"
"reflect"
"regexp"
"strings"
)

const EmojiPattern = `:\w+:`
const ValueChangeFormat = "`%s` → "
const NameValueEntryFormat = "__%s__ : %s%s\n"
const EntityPageFormat = "\n[Page de l'entité](%s)"

func DispatchEmbeds(embeds *[]discord.Embed) {
if len(*embeds) == 0 {
return
}

var themeColor uint32 = 374272
calculateThemeColor(&themeColor)

var formDatas []*discord.WebhookFormData
for i := 0; i < len(*embeds); i += 10 {
end := i + 10
Expand All @@ -34,6 +43,8 @@ func DispatchEmbeds(embeds *[]discord.Embed) {
ping := ""
for _, embed := range (*embeds)[i:end] {
embed := embed
embed.Color = themeColor

*formData.Embeds = append(*formData.Embeds, embed)

if len(embed.Role) > 0 && !strings.Contains(ping, embed.Role) {
Expand Down Expand Up @@ -165,7 +176,7 @@ func fillEmbed(embed *discord.Embed, oldObject, newObject interface{}) {
}

if url != "" {
embedField.Value += fmt.Sprintf("\n[Page de l'entité](%s)", strings.ReplaceAll(url, " ", "-"))
embedField.Value += fmt.Sprintf(EntityPageFormat, strings.ReplaceAll(url, " ", "-"))
}

MakeFooter(embedField)
Expand All @@ -183,14 +194,14 @@ func processDisplay(field *discord.EmbedField, oldValue, newValue interface{}, p
if oldValue != nil {
if utils.IsTime(oldValue) {
if utils.TimeDifference(oldValue, newValue) {
oldValueText = fmt.Sprintf("`%s` → ", fmt.Sprintf(format, oldValue))
oldValueText = fmt.Sprintf(ValueChangeFormat, fmt.Sprintf(format, oldValue))
}
} else if oldValue != newValue {
oldValueText = fmt.Sprintf("`%s` → ", fmt.Sprintf(format, oldValue))
oldValueText = fmt.Sprintf(ValueChangeFormat, fmt.Sprintf(format, oldValue))
}
}

value := fmt.Sprintf("__%s__ : %s%s\n", split[0], oldValueText, fmt.Sprintf(format, newValue))
value := fmt.Sprintf(NameValueEntryFormat, split[0], oldValueText, fmt.Sprintf(format, newValue))
field.Value += value
}

Expand All @@ -206,10 +217,8 @@ func processImage(embed *discord.Embed, newValue interface{}, parts []string) {
}

func ProcessEmojis(parts []string) {
emojiPattern := `:\w+:`

for i := range parts {
re, err := regexp.Compile(emojiPattern)
re, err := regexp.Compile(EmojiPattern)
if err != nil {
continue
}
Expand All @@ -230,3 +239,32 @@ func ProcessEmojis(parts []string) {
func MakeFooter(field *discord.EmbedField) {
field.Value += fmt.Sprintf("\n-# Développé avec %s par Alex'Presso", viper.GetString("emojis.heart"))
}

func calculateThemeColor(themeColor *uint32) {
logo, err := FetchLogo()
if err != nil {
utils.Log("An error occurred while fetching logo: " + err.Error())
return
}

res, err := prominentcolor.KmeansWithAll(
1,
logo,
prominentcolor.ArgumentDefault,
uint(prominentcolor.DefaultSize),
prominentcolor.GetDefaultMasks(),
)

if err != nil {
utils.Log("An error occurred while calculating prominent color: " + err.Error())
return
}

if len(res) == 0 {
utils.Log("No prominent color found")
return
}

color := res[0].Color
*themeColor = color.R<<16 | color.G<<8 | color.B
}
6 changes: 6 additions & 0 deletions services/zuniversService.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package services
import (
"github.com/alexpresso/zunivers-webhooks/structures"
"github.com/alexpresso/zunivers-webhooks/utils"
"image"
"net/url"
)

Expand Down Expand Up @@ -70,3 +71,8 @@ func FetchShop() (entries []structures.ShopEntry, err error) {
err = utils.Request("/public/shop", "GET", nil, &entries)
return
}

func FetchLogo() (logo image.Image, err error) {
err = utils.Request("/assets/logo-mini.png", "GET", nil, &logo)
return
}
34 changes: 32 additions & 2 deletions utils/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ package utils

import (
"bytes"
"compress/gzip"
"encoding/json"
"github.com/spf13/viper"
"image"
_ "image/jpeg"
_ "image/png"
"io"
"net/http"
"time"
)
Expand All @@ -13,7 +18,15 @@ func Request(uri, method string, body []byte, structure interface{}) (err error)
Timeout: viper.GetDuration("api.timeout") * time.Second,
}

req, err := http.NewRequest(method, viper.GetString("api.baseUrl")+uri, bytes.NewBuffer(body))
var baseUrl string
switch structure.(type) {
case *image.Image:
baseUrl = viper.GetString("frontBaseUrl")
default:
baseUrl = viper.GetString("api.baseUrl")
}

req, err := http.NewRequest(method, baseUrl+uri, bytes.NewBuffer(body))
if err != nil {
return
}
Expand All @@ -38,7 +51,24 @@ func Request(uri, method string, body []byte, structure interface{}) (err error)
}

defer r.Body.Close()
var reader io.Reader = r.Body
if r.Header.Get("Content-Encoding") == "gzip" {
reader, err = gzip.NewReader(r.Body)
if err != nil {
return
}
defer reader.(*gzip.Reader).Close()
}

switch structure.(type) {
case *image.Image:
*(structure.(*image.Image)), _, err = image.Decode(r.Body)
if err != nil {
return err
}
default:
err = json.NewDecoder(r.Body).Decode(structure)
}

err = json.NewDecoder(r.Body).Decode(structure)
return
}

0 comments on commit 92ed850

Please sign in to comment.