Add time to next stream to presence module

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2021-07-29 21:39:14 +02:00
parent fa6997f684
commit da6c0b35c2
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D
2 changed files with 42 additions and 5 deletions

View file

@ -1,6 +1,7 @@
package main package main
import ( import (
"context"
"fmt" "fmt"
"math" "math"
"strings" "strings"
@ -32,6 +33,9 @@ func (m *modPresence) Initialize(crontab *cron.Cron, discord *discordgo.Session,
if err := attrs.Expect( if err := attrs.Expect(
"fallback_text", "fallback_text",
"twitch_channel_id",
"twitch_client_id",
"twitch_token",
); err != nil { ); err != nil {
return errors.Wrap(err, "validating attributes") return errors.Wrap(err, "validating attributes")
} }
@ -47,7 +51,33 @@ func (m *modPresence) Initialize(crontab *cron.Cron, discord *discordgo.Session,
func (m modPresence) cronUpdatePresence() { func (m modPresence) cronUpdatePresence() {
var nextStream *time.Time = nil var nextStream *time.Time = nil
// FIXME: Get next stream status twitch := newTwitchAdapter(
// @attr twitch_client_id required string "" Twitch client ID the token was issued for
m.attrs.MustString("twitch_client_id", nil),
// @attr twitch_token required string "" Token for the user the `twitch_channel_id` belongs to
m.attrs.MustString("twitch_token", nil),
)
data, err := twitch.GetChannelStreamSchedule(
context.Background(),
// @attr twitch_channel_id required string "" ID (not name) of the channel to fetch the schedule from
m.attrs.MustString("twitch_channel_id", nil),
// @attr schedule_past_time optional duration "15m" How long in the past should the schedule contain an entry
ptrTime(time.Now().Add(-m.attrs.MustDuration("schedule_past_time", defaultStreamSchedulePastTime))),
)
if err != nil {
log.WithError(err).Error("Unable to fetch stream schedule")
return
}
for _, seg := range data.Data.Segments {
if seg.StartTime == nil || seg.CanceledUntil != nil {
continue
}
nextStream = seg.StartTime
break
}
// @attr fallback_text required string "" What to set the text to when no stream is found (`playing <text>`) // @attr fallback_text required string "" What to set the text to when no stream is found (`playing <text>`)
status := m.attrs.MustString("fallback_text", nil) status := m.attrs.MustString("fallback_text", nil)
@ -63,11 +93,14 @@ func (m modPresence) cronUpdatePresence() {
} }
func (m modPresence) durationToHumanReadable(d time.Duration) string { func (m modPresence) durationToHumanReadable(d time.Duration) string {
d = time.Duration(math.Abs(float64(d)))
if d > time.Hour*24 {
return fmt.Sprintf("%.0f Tagen", math.Ceil(float64(d)/float64(time.Hour*24)))
}
var elements []string var elements []string
d = time.Duration(math.Abs(float64(d)))
for div, req := range map[time.Duration]bool{ for div, req := range map[time.Duration]bool{
time.Hour * 24: false,
time.Hour: true, time.Hour: true,
time.Minute: true, time.Minute: true,
} { } {

View file

@ -23,7 +23,11 @@ Updates the presence status of the bot to display the next stream
| Attribute | Req. | Type | Default Value | Description | | Attribute | Req. | Type | Default Value | Description |
| --------- | :--: | ---- | ------------- | ----------- | | --------- | :--: | ---- | ------------- | ----------- |
| `fallback_text` | ✅ | string | | What to set the text to when no stream is found (`playing <text>`) | | `fallback_text` | ✅ | string | | What to set the text to when no stream is found (`playing <text>`) |
| `twitch_channel_id` | ✅ | string | | ID (not name) of the channel to fetch the schedule from |
| `twitch_client_id` | ✅ | string | | Twitch client ID the token was issued for |
| `twitch_token` | ✅ | string | | Token for the user the `twitch_channel_id` belongs to |
| `cron` | | string | `* * * * *` | When to execute the module | | `cron` | | string | `* * * * *` | When to execute the module |
| `schedule_past_time` | | duration | `15m` | How long in the past should the schedule contain an entry |
## Type: `schedule` ## Type: `schedule`