Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Stream Server Project
Stream Server
Commits
93581d8e
Commit
93581d8e
authored
Aug 21, 2019
by
Simon Kirsten
Browse files
Added support to search games
parent
207b0004
Changes
1
Hide whitespace changes
Inline
Side-by-side
internal/twitch/twitch.go
View file @
93581d8e
...
...
@@ -2,6 +2,7 @@ package twitch
import
(
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
...
...
@@ -54,17 +55,18 @@ type Stream struct {
// twitchGame is the structure for games that we get from twitch.
type
twitchGame
struct
{
Viewers
int
`json:"viewers"`
Game
struct
{
Name
string
`json:"name"`
Box
struct
{
Large
string
`json:"large"`
}
`json:"box"`
Logo
struct
{
Large
string
`json:"large"`
}
`json:"logo"`
}
// Viewers int `json:"viewers"`
// Game struct {
Name
string
`json:"name"`
Box
struct
{
Large
string
`json:"large"`
}
`json:"box"`
Logo
struct
{
Large
string
`json:"large"`
}
`json:"logo"`
Popularity
int
`json:"popularity"`
// }
}
// Game is the structure for games that we return to the user.
...
...
@@ -96,10 +98,10 @@ func (s *twitchStream) toSimplified() Stream {
// toSimplified simplifies a twitchGame (that we got from twitch) to a Game (that we return to the user).
func
(
g
*
twitchGame
)
toSimplified
()
Game
{
return
Game
{
Name
:
g
.
Game
.
Name
,
Viewers
:
g
.
Viewers
,
BoxImgURL
:
g
.
Game
.
Box
.
Large
,
LogoImgURL
:
g
.
Game
.
Logo
.
Large
,
Name
:
g
.
Name
,
Viewers
:
g
.
Popularity
,
BoxImgURL
:
g
.
Box
.
Large
,
LogoImgURL
:
g
.
Logo
.
Large
,
}
}
...
...
@@ -123,6 +125,11 @@ func twitchRequest(endpoint string, query url.Values, output interface{}) error
}
defer
resp
.
Body
.
Close
()
if
resp
.
StatusCode
!=
http
.
StatusOK
{
return
errors
.
New
(
resp
.
Status
)
}
body
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
if
err
!=
nil
{
return
err
...
...
@@ -270,8 +277,15 @@ func streamsTopHandleFunc(w http.ResponseWriter, r *http.Request) {
func
gamesTopHandleFunc
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
if
q
:=
r
.
URL
.
Query
()
.
Get
(
"query"
);
q
!=
""
{
gamesSearchHandleFunc
(
w
,
r
)
return
}
var
twitchTopGamesResponse
struct
{
Top
[]
twitchGame
`json:"top"`
Top
[]
struct
{
Game
twitchGame
}
`json:"top"`
}
err
:=
twitchRequest
(
"/games/top"
,
url
.
Values
{},
&
twitchTopGamesResponse
)
...
...
@@ -284,6 +298,45 @@ func gamesTopHandleFunc(w http.ResponseWriter, r *http.Request) {
topGamesResponse
:=
make
([]
Game
,
0
)
for
_
,
game
:=
range
twitchTopGamesResponse
.
Top
{
topGamesResponse
=
append
(
topGamesResponse
,
game
.
Game
.
toSimplified
())
}
_
,
err
=
util
.
ServeJSON
(
w
,
&
topGamesResponse
)
if
err
!=
nil
{
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusInternalServerError
)
log
.
Println
(
err
)
return
}
}
func
gamesSearchHandleFunc
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
var
twitchSearchGamesResponse
struct
{
Games
[]
twitchGame
`json:"games"`
}
query
:=
r
.
URL
.
Query
()
if
q
:=
query
.
Get
(
"query"
);
q
==
""
{
err
:=
errors
.
New
(
"Parameter 'query' required"
)
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusBadRequest
)
log
.
Println
(
err
)
return
}
params
:=
url
.
Values
{}
params
.
Set
(
"query"
,
query
.
Get
(
"query"
))
err
:=
twitchRequest
(
"/search/games"
,
params
,
&
twitchSearchGamesResponse
)
if
err
!=
nil
{
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusInternalServerError
)
log
.
Println
(
err
)
return
}
topGamesResponse
:=
make
([]
Game
,
0
)
for
_
,
game
:=
range
twitchSearchGamesResponse
.
Games
{
topGamesResponse
=
append
(
topGamesResponse
,
game
.
toSimplified
())
}
...
...
@@ -295,12 +348,15 @@ func gamesTopHandleFunc(w http.ResponseWriter, r *http.Request) {
}
}
// TODO: implement handler for search streams
// Handler returns a http.Handler that serves requests for the /twitch backend
func
Handler
()
http
.
Handler
{
mux
:=
http
.
NewServeMux
()
// mux.Handle("/", http.NotFoundHandler()) // if the others don't match, return not found
mux
.
HandleFunc
(
"/twitch/games/top"
,
gamesTopHandleFunc
)
// mux.HandleFunc("/twitch/games/search", gamesSearchHandleFunc)
mux
.
HandleFunc
(
"/twitch/streams/top"
,
streamsTopHandleFunc
)
mux
.
HandleFunc
(
"/twitch/streams/featured"
,
streamsFeaturedHandleFunc
)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment