grim/wasdead
Clone
Summary
Browse
Changes
Graph
hide the youtube command as it doesn't quite work correctly and I don't want to fix it right now
draft
2020-09-26, Gary Kramlich
b05edb50a737
hide the youtube command as it doesn't quite work correctly and I don't want to fix it right now
package
twitch
import
(
"net/url"
"strings"
"github.com/nicklaw5/helix"
"bitbucket.org/rw_grim/wasdead/presence"
)
type
Twitch
struct
{
client
*
helix
.
Client
}
func
New
(
clientID
,
clientSecret
string
)
(
presence
.
Provider
,
error
)
{
client
,
err
:=
helix
.
NewClient
(
&
helix
.
Options
{
ClientID
:
clientID
,
ClientSecret
:
clientSecret
,
})
if
err
!=
nil
{
return
nil
,
err
}
resp
,
err
:=
client
.
RequestAppAccessToken
([]
string
{})
if
err
!=
nil
{
return
nil
,
err
}
client
.
SetAppAccessToken
(
resp
.
Data
.
AccessToken
)
return
&
Twitch
{
client
:
client
},
nil
}
func
(
t
*
Twitch
)
HandleURL
(
uri
string
)
bool
{
parsedURL
,
_
:=
url
.
Parse
(
uri
)
for
_
,
host
:=
range
[]
string
{
"twitch.tv"
,
"www.twitch.tv"
}
{
if
parsedURL
.
Host
==
host
{
return
true
}
}
return
false
}
func
(
t
*
Twitch
)
GetPresenceFromURL
(
uri
string
)
(
presence
.
Presence
,
error
)
{
parsedURL
,
_
:=
url
.
Parse
(
uri
)
username
:=
strings
.
TrimLeft
(
parsedURL
.
Path
,
"/"
)
return
t
.
GetPresenceFromUsername
(
username
)
}
func
(
t
*
Twitch
)
GetPresenceFromUsername
(
username
string
)
(
presence
.
Presence
,
error
)
{
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
{
Provider
:
"twitch"
,
Username
:
username
,
},
nil
}
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
)
// find the game
games
,
err
:=
t
.
client
.
GetGames
(
&
helix
.
GamesParams
{
IDs
:
[]
string
{
stream
.
GameID
},
})
if
err
!=
nil
{
return
presence
.
Presence
{},
err
}
game
:=
games
.
Data
.
Games
[
0
]
p
:=
presence
.
Presence
{
Provider
:
"twitch"
,
Live
:
true
,
StreamID
:
stream
.
ID
,
Username
:
user
.
DisplayName
,
UserID
:
user
.
ID
,
Title
:
stream
.
Title
,
Game
:
game
.
Name
,
Viewers
:
int64
(
stream
.
ViewerCount
),
Language
:
stream
.
Language
,
ProfileImageURL
:
profile_url
,
Started
:
stream
.
StartedAt
,
ThumbnailURL
:
user
.
ProfileImageURL
,
URL
:
"https://twitch.tv/"
+
username
,
}
return
p
,
nil
}