[templating] Add streamIsLive function

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2024-05-26 15:50:10 +02:00
parent 0a37873241
commit 8e8895d32e
Signed by: luzifer
SSH key fingerprint: SHA256:/xtE5lCgiRDQr8SLxHMS92ZBlACmATUmF1crK16Ks4E
2 changed files with 29 additions and 1 deletions

View file

@ -467,7 +467,7 @@ Example:
``` ```
# Your int this hour: {{ printf "%.0f" (mulf (seededRandom (list "int" .username (now | date "2006-01-02 15") | join ":")) 100) }}% # Your int this hour: {{ printf "%.0f" (mulf (seededRandom (list "int" .username (now | date "2006-01-02 15") | join ":")) 100) }}%
< Your int this hour: 82% < Your int this hour: 41%
``` ```
### `spotifyCurrentPlaying` ### `spotifyCurrentPlaying`
@ -500,6 +500,19 @@ Example:
* https://open.spotify.com/track/3HCzXf0lNpekSqsGBcGrCd * https://open.spotify.com/track/3HCzXf0lNpekSqsGBcGrCd
``` ```
### `streamIsLive`
Check whether a given channel is currently live
Syntax: `streamIsLive <username>`
Example:
```
# {{ streamIsLive "luziferus" }}
* true
```
### `streamUptime` ### `streamUptime`
Returns the duration the stream is online (causes an error if no current stream is found) Returns the duration the stream is online (causes an error if no current stream is found)

View file

@ -15,6 +15,7 @@ func init() {
regFn, regFn,
tplTwitchRecentGame, tplTwitchRecentGame,
tplTwitchRecentTitle, tplTwitchRecentTitle,
tplTwitchStreamIsLive,
tplTwitchStreamUptime, tplTwitchStreamUptime,
) )
} }
@ -55,6 +56,20 @@ func tplTwitchRecentTitle(args plugins.RegistrationArguments) {
}) })
} }
func tplTwitchStreamIsLive(args plugins.RegistrationArguments) {
args.RegisterTemplateFunction("streamIsLive", plugins.GenericTemplateFunctionGetter(func(username string) bool {
_, err := args.GetTwitchClient().GetCurrentStreamInfo(context.Background(), strings.TrimLeft(username, "#"))
return err == nil
}), plugins.TemplateFuncDocumentation{
Description: "Check whether a given channel is currently live",
Syntax: "streamIsLive <username>",
Example: &plugins.TemplateFuncDocumentationExample{
Template: `{{ streamIsLive "luziferus" }}`,
FakedOutput: "true",
},
})
}
func tplTwitchStreamUptime(args plugins.RegistrationArguments) { func tplTwitchStreamUptime(args plugins.RegistrationArguments) {
args.RegisterTemplateFunction("streamUptime", plugins.GenericTemplateFunctionGetter(func(username string) (time.Duration, error) { args.RegisterTemplateFunction("streamUptime", plugins.GenericTemplateFunctionGetter(func(username string) (time.Duration, error) {
si, err := args.GetTwitchClient().GetCurrentStreamInfo(context.Background(), strings.TrimLeft(username, "#")) si, err := args.GetTwitchClient().GetCurrentStreamInfo(context.Background(), strings.TrimLeft(username, "#"))