-
Notifications
You must be signed in to change notification settings - Fork 1
/
youtube.go
102 lines (88 loc) · 2.9 KB
/
youtube.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"google.golang.org/api/googleapi/transport"
"google.golang.org/api/youtube/v3"
"log"
"net/http"
"runtime/debug"
)
type Video struct {
Id string `json:"id"`
Link string `json:"link"`
Description string `json:"description"`
Title string `json:"title"`
IsLive bool `json:"live"`
ThumbnailURL string `json:"thumbnail_url"`
ChannelURL string `json:"channel_url"`
}
type YoutubeHandler struct {
service youtube.Service
}
func newYouTubeHandler(youTubeApiKey string) YoutubeHandler {
transport := &transport.APIKey{
Key: youTubeApiKey,
}
client := &http.Client{Transport: transport}
var err error
service, err := youtube.New(client)
if err != nil {
log.Panicf("ERROR in creating youtube New client ", err)
}
return YoutubeHandler{service: *service}
}
func (handler YoutubeHandler) fetchLastVideos() func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
defer func() {
if r := recover(); r != nil {
w.WriteHeader(500)
w.Write([]byte(fmt.Sprintf("{\"Error\": \"%+v\"}", r)))
fmt.Println("recovered from ", r)
debug.PrintStack()
}
}()
vars := mux.Vars(r)
channelName := vars["channel"]
resp := handler.getLastVideos(channelName)
res, _ := json.Marshal(resp)
w.WriteHeader(200)
w.Write(res)
}
}
func (handler YoutubeHandler) getLastVideos(channelName string) ([]Video) {
channelId := handler.getChannelIdByChannelName(channelName)
searchVideosResponse, _ := handler.getLastVideosInChannel(channelId, 5)
items := searchVideosResponse.Items
var resp []Video
for _, item := range items {
video := Video{
Id: item.Id.VideoId,
Link: "https://www.youtube.com/watch?v=" + item.Id.VideoId,
Description: item.Snippet.Description,
Title: item.Snippet.Title,
IsLive: item.Snippet.LiveBroadcastContent == "live",
ThumbnailURL: item.Snippet.Thumbnails.High.Url,
ChannelURL: "https://www.youtube.com/channel/" + channelId,
}
resp = append(resp, video)
}
return resp
}
func (handler YoutubeHandler) searchChannels(part string, query string, maxResults int64) (*youtube.SearchListResponse, error) {
return handler.service.Search.List(part).Type("channel").Q(query).MaxResults(maxResults).Do()
}
func (handler YoutubeHandler) getLastVideosInChannel(channelId string, maxResults int64) (*youtube.SearchListResponse, error) {
return handler.service.Search.List("snippet").Type("video").ChannelId(channelId).MaxResults(maxResults).Order("date").Do()
}
func (handler YoutubeHandler) getChannelIdByChannelName(channelName string) string {
searchListResponse, err := handler.searchChannels("snippet", channelName, 1)
if err != nil {
panic("Error in channel search")
}
if len(searchListResponse.Items) == 0 {
panic("Channel not found")
}
return searchListResponse.Items[0].Snippet.ChannelId
}