idiotbox-go

youtube web application in Go
git clone git://git.codemadness.org/idiotbox-go
Log | Files | Refs | README | LICENSE

commit e5f789f941a4c96ed01814944efe93cc76542d0d
parent 3a8963498b96c169b2545c9d0c4fe828decec927
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Fri, 15 Sep 2017 12:01:08 +0200

allow to specify search result order (hidden option for now)

Diffstat:
jewtoob.go | 5++++-
main.go | 12+++++++++++-
2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/jewtoob.go b/jewtoob.go @@ -66,7 +66,7 @@ type SearchResult struct { Videolist *VideoList } -func SearchVideos(q, next string) (*SearchResult, error) { +func SearchVideos(q, next, order string) (*SearchResult, error) { u := "https://www.googleapis.com/youtube/v3/search?key=" u += url.QueryEscape(config_apikey) u += "&maxResults=10" @@ -78,6 +78,9 @@ func SearchVideos(q, next string) (*SearchResult, error) { if len(next) > 0 { u += "&pageToken=" + url.QueryEscape(next) } + if len(order) > 0 { + u += "&order=" + url.QueryEscape(order) + } data, err := fetchdata(u) if err != nil { return nil, err diff --git a/main.go b/main.go @@ -145,8 +145,18 @@ func SearchHandler(w http.ResponseWriter, r *http.Request) error { next := r.FormValue("next") q := r.FormValue("q") + o := r.FormValue("o") + + switch o { + case "", "date", "rating", "relevance", "title", "videoCount", "viewCount": + default: + BadRequest(w, "Invalid order parameter, valid parameters are: "+ + `"date", "rating", "relevance", "title", "videoCount", "viewCount"`) + return nil + } + if len(q) > 0 { - searchresult, err = SearchVideos(q, next) + searchresult, err = SearchVideos(q, next, o) if err != nil { // NOTE: don't show error to the user (may contain sensitive data). http.Error(w, "500 Internal Server Error", http.StatusInternalServerError)