-
Notifications
You must be signed in to change notification settings - Fork 0
/
videoList.go
66 lines (56 loc) · 1.55 KB
/
videoList.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package http
import (
"encoding/json"
"fmt"
"log"
"net/http"
"time"
player "github.com/dbond762/youtube-player-backend"
)
const ApiKey = "AIzaSyBVJgyC-x6CsM-hPCYY10VfOnGOKksDK8U"
type ApiSearchResponse struct {
Items []struct {
ID struct {
VideoID string `json:"videoId"`
} `json:"id"`
Snippet struct {
PublishedAt time.Time `json:"publishedAt"`
Title string `json:"title"`
Description string `json:"description"`
Thumbnails struct {
High struct {
URL string `json:"url"`
} `json:"high"`
} `json:"thumbnails"`
} `json:"snippet"`
} `json:"items"`
}
type VideoFinder struct{}
func (s *VideoFinder) Search(query string) (*player.VideoList, error) {
const maxResults = 25
baseUrl := fmt.Sprintf("https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=%d&q=%s&key=%s", maxResults, query, ApiKey)
resp, err := http.Get(baseUrl)
if err != nil {
log.Printf("HTTP: error on get request: %s", err)
return nil, err
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
apiResp := new(ApiSearchResponse)
if err := decoder.Decode(apiResp); err != nil {
log.Printf("HTTP: error on decoding: %s", err)
return nil, err
}
list := make(player.VideoList, len(apiResp.Items))
for i, video := range apiResp.Items {
v := player.Video{
ID: video.ID.VideoID,
Title: video.Snippet.Title,
PubDate: video.Snippet.PublishedAt,
Description: video.Snippet.Description,
Thumbnail: video.Snippet.Thumbnails.High.URL,
}
list[i].Video = v
}
return &list, nil
}