random commit
This commit is contained in:
@@ -3,7 +3,7 @@ package twitchapi
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
|
||||
helix "github.com/nicklaw5/helix"
|
||||
)
|
||||
|
||||
@@ -11,28 +11,28 @@ type Client struct {
|
||||
api *helix.Client
|
||||
}
|
||||
|
||||
func NewClient(clientID, ClientSecret string) (*Client, error) {
|
||||
func NewClient(clientID, clientSecret string) (*Client, error) {
|
||||
apiClient, err := helix.NewClient(&helix.Options{
|
||||
ClientID: clientID,
|
||||
ClientSecret: clientSecret,
|
||||
})
|
||||
if err != nil {
|
||||
return nill, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err := apiClient.RequestAppAccessToken([]string{})
|
||||
if err != nil || resp.StatusCode != 200 {
|
||||
return nil, fmt.Errorf("Failed to get app access token: %v", err)
|
||||
}
|
||||
apiClient.SetUserAccessToken(resp.Data.AcessToken)
|
||||
apiClient.SetUserAccessToken(resp.Data.AccessToken)
|
||||
|
||||
return &Client{api: apiClient}, nil
|
||||
}
|
||||
|
||||
// Twitch API helpers
|
||||
|
||||
func getUserID(client *helix.Client, login String) (string, error) {
|
||||
resp, err := client.GetUsers(&helix.UsersParams{
|
||||
func (c *Client) GetUserID(login string) (string, error) {
|
||||
resp, err := c.api.GetUsers(&helix.UsersParams{
|
||||
Logins: []string{login},
|
||||
})
|
||||
if err != nil {
|
||||
@@ -45,7 +45,7 @@ func getUserID(client *helix.Client, login String) (string, error) {
|
||||
}
|
||||
|
||||
func getStreamInfo(client *helix.Client, userID string) (*helix.Stream, error) {
|
||||
resp, err := clinet.GetStreams(&helix.StreamsParams{
|
||||
resp, err := client.GetStreams(&helix.StreamsParams{
|
||||
UserIDs: []string{userID},
|
||||
})
|
||||
if err != nil {
|
||||
@@ -58,7 +58,7 @@ func getStreamInfo(client *helix.Client, userID string) (*helix.Stream, error) {
|
||||
}
|
||||
|
||||
func getStreamUptime(client *helix.Client, channel string) (string, error) {
|
||||
userID, err := getUserID(client, channel)
|
||||
userID, err := GetUserID(client, channel)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -66,20 +66,18 @@ func getStreamUptime(client *helix.Client, channel string) (string, error) {
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
startedAt, err := time.Parse(time.RFC3339, stream.StartedAt)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
startedAt := stream.StartedAt
|
||||
|
||||
uptime := time.Since(startedAt).Round(time.Second)
|
||||
return uptime.String(), nil
|
||||
}
|
||||
|
||||
func getStreamTitle(client *helix.Client, channel string) (string, error) {
|
||||
userID, err := getUserID(client, channel)
|
||||
userID, err := GetUserID(client, channel)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
resp, err := client.GetChannels(&helix.GetChannelsParams{
|
||||
resp, err := client.GetChannelInformation(&helix.GetChannelInformationParams{
|
||||
BroadcasterID: userID,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -92,23 +90,23 @@ func getStreamTitle(client *helix.Client, channel string) (string, error) {
|
||||
}
|
||||
|
||||
func setStreamTitle(client *helix.Client, channel, newTitle string) error {
|
||||
userID, err := getUserID(client, channel)
|
||||
userID, err := GetUserID(client, channel)
|
||||
if err!= nil {
|
||||
return err
|
||||
}
|
||||
_, err = client.UpdateChannelInformation(&helix.UpdateChannelInformationParams {
|
||||
_, err = client.EditChannelInformation(&helix.EditChannelInformationParams {
|
||||
BroadcasterID: userID,
|
||||
Title: newTitle,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func getStreamCategory(client *helix.Client, channel string) (srting, error) {
|
||||
userID, er := getUserID(client, channel)
|
||||
func getStreamCategory(client *helix.Client, channel string) (string, error) {
|
||||
userID, err := GetUserID(client, channel)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
resp, err := client.GetChannels(&helix.GetChannelsParams{
|
||||
resp, err := client.GetChannelInformation(&helix.GetChannelInformationParams{
|
||||
BroadcasterID: userID,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -120,17 +118,17 @@ func getStreamCategory(client *helix.Client, channel string) (srting, error) {
|
||||
return resp.Data.Channels[0].GameName, nil
|
||||
}
|
||||
|
||||
func setStreamCategory(client *helix.Client, channel,, newCategory string) error {
|
||||
userID, err := getUserID(client, channel)
|
||||
func setStreamCategory(client *helix.Client, channel, newCategory string) error {
|
||||
userID, err := GetUserID(client, channel)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// get Twitch GameID by Name from API
|
||||
gameID, err := getUserIDByName(client, newCategory)
|
||||
gameID, err := getGameIDByName(client, newCategory)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = client.UpdateChannelInformation(&helix.UpdateChannelInformationParams{
|
||||
_, err = client.EditChannelInformation(&helix.EditChannelInformationParams{
|
||||
BroadcasterID: userID,
|
||||
GameID: gameID,
|
||||
})
|
||||
@@ -138,7 +136,7 @@ func setStreamCategory(client *helix.Client, channel,, newCategory string) error
|
||||
}
|
||||
|
||||
func getGameIDByName(client *helix.Client, name string) (string, error) {
|
||||
resp, err := client.GetGames(&helix.GamesParams{
|
||||
resp, err := GetGames(&helix.GamesParams{
|
||||
Names: []string{name},
|
||||
})
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user