grim/wasdead

Add a convey.yml
draft
2019-06-05, Gary Kramlich
a06926950b53
Add a convey.yml
package twitch
import (
"fmt"
"net/url"
"strings"
"github.com/nicklaw5/helix"
"bitbucket.org/TheToyz/nowdead/presence"
)
type Twitch struct {
client *helix.Client
}
func New(token string) (presence.Provider, error) {
client, err := helix.NewClient(&helix.Options{
ClientID: token,
})
if err != nil {
return nil, err
}
return &Twitch{client: client}, nil
}
func (t *Twitch) GetPresence(uri string) (presence.Presence, error) {
parsedURL, _ := url.Parse(uri)
username := strings.TrimLeft(parsedURL.Path, "/")
users, err := t.client.GetUsers(&helix.UsersParams{
Logins: []string{username},
})
if err != nil {
return presence.Presence{}, err
}
streams, err := t.client.GetStreams(&helix.StreamsParams{
UserLogins: []string{username},
})
if err != nil {
return presence.Presence{}, err
}
if len(users.Data.Users) <= 0 || len(streams.Data.Streams) <= 0 {
return presence.Presence{}, fmt.Errorf("%s is not streaming", username)
}
user := users.Data.Users[0]
stream := streams.Data.Streams[0]
profile_url := strings.Replace(stream.ThumbnailURL, "{width}", "400", -1)
profile_url = strings.Replace(profile_url, "{height}", "255", -1)
p := presence.Presence{
StreamID: stream.ID,
Username: user.DisplayName,
UserID: user.ID,
Title: stream.Title,
Viewers: int64(stream.ViewerCount),
Language: stream.Language,
ProfileImageURL: profile_url,
ThumbnailURL: user.ProfileImageURL,
URL: uri,
}
return p, nil
}