add lastfm/phonetic commands

This commit is contained in:
lyx0 2024-01-11 02:52:29 +01:00
parent 2e6c2e3704
commit 74c9f4f81d
5 changed files with 140 additions and 0 deletions

View file

@ -45,6 +45,20 @@ func (app *application) ParseCommand(evt *event.Event) {
return
}
case "lastfm":
if msgLen == 1 {
reply = "Not enough arguments provided. Usage: ()lastfm <username>"
} else {
reply = commands.LastFmUserRecent(cmdParams[1], app.Log)
}
case "phonetic":
if msgLen == 1 {
reply = "Not enough arguments provided. Usage: ()phonetic <text to translate>"
} else {
reply, _ = commands.Phonetic(evt.Content.AsMessage().Body[10:len(evt.Content.AsMessage().Body)])
}
case "preview":
if msgLen < 2 {
reply = "Not enough arguments provided. Usage: !preview [stream]"

1
go.mod
View file

@ -15,6 +15,7 @@ require (
github.com/google/uuid v1.5.0
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/shkh/lastfm-go v0.0.0-20191215035245-89a801c244e0
github.com/tidwall/gjson v1.17.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect

2
go.sum
View file

@ -28,6 +28,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A=
github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/shkh/lastfm-go v0.0.0-20191215035245-89a801c244e0 h1:cgqwZtnR+IQfUYDLJ3Kiy4aE+O/wExTzEIg8xwC4Qfs=
github.com/shkh/lastfm-go v0.0.0-20191215035245-89a801c244e0/go.mod h1:n3nudMl178cEvD44PaopxH9jhJaQzthSxUzLO5iKMy4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.17.0 h1:/Jocvlh98kcTfpN2+JzGQWQcqrPQwDrVEMApx/M5ZwM=

34
pkg/commands/lastfm.go Normal file
View file

@ -0,0 +1,34 @@
package commands
import (
"fmt"
"os"
"github.com/joho/godotenv"
"github.com/rs/zerolog"
"github.com/shkh/lastfm-go/lastfm"
)
func LastFmUserRecent(user string, log zerolog.Logger) string {
err := godotenv.Load()
if err != nil {
log.Error().Err(err).Msg("Error loading lastfm api keys from .env file")
}
apiKey := os.Getenv("LAST_FM_API_KEY")
apiSecret := os.Getenv("LAST_FM_SECRET")
api := lastfm.New(apiKey, apiSecret)
result, _ := api.User.GetRecentTracks(lastfm.P{"user": user}) //discarding error
var reply string
for i, track := range result.Tracks {
// The 0th result is the most recent one since it goes from most recent
// to least recent.
if i == 0 {
reply = fmt.Sprintf("Most recently played track for user %v: %v - %v", user, track.Artist.Name, track.Name)
}
}
return reply
}

89
pkg/commands/phonetic.go Normal file
View file

@ -0,0 +1,89 @@
package commands
import "fmt"
var cm = map[string]string{
"`": "ё",
"~": "Ё",
"=": "ъ",
"+": "Ъ",
"[": "ю",
"]": "щ",
`\`: "э",
"{": "Ю",
"}": "Щ",
"|": "Э",
";": "ь",
":": "Ь",
"'": "ж",
`"`: "Ж",
"q": "я",
"w": "ш",
"e": "е",
"r": "р",
"t": "т",
"y": "ы",
"u": "у",
"i": "и",
"o": "о",
"p": "п",
"a": "а",
"s": "с",
"d": "д",
"f": "ф",
"g": "г",
"h": "ч",
"j": "й",
"k": "к",
"l": "л",
"z": "з",
"x": "х",
"c": "ц",
"v": "в",
"b": "б",
"n": "н",
"m": "м",
"Q": "Я",
"W": "Ш",
"E": "Е",
"R": "Р",
"T": "Т",
"Y": "Ы",
"U": "У",
"I": "И",
"O": "О",
"P": "П",
"A": "А",
"S": "С",
"D": "Д",
"F": "Ф",
"G": "Г",
"H": "Ч",
"J": "Й",
"K": "К",
"L": "Л",
"Z": "З",
"X": "Х",
"C": "Ц",
"V": "В",
"B": "Б",
"N": "Н",
"M": "М",
}
func Phonetic(message string) (string, error) {
var ts string
for _, c := range message {
if _, ok := cm[string(c)]; ok {
ts = ts + cm[string(c)]
} else {
ts = ts + string(c)
}
}
return fmt.Sprint(ts), nil
}