-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
171 lines (130 loc) · 3.49 KB
/
main.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"bufio"
"context"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"strings"
//"net/http"
"google.golang.org/api/option"
youtube "google.golang.org/api/youtube/v3"
)
type Liver struct {
Name string `json:"name,omniempty"`
Slug string `json:"slug,omniempty"`
Affiliation string `json:"affiliation,omniempty"`
EnglishName string `json:"english_name,omniempty"`
YoutubeURL string `json:"youtube_ch,omniempty"`
TwitterURL string `json:"twitter,omniempty"`
}
type Video struct {
Title string
URL string
}
type Service struct {
*youtube.Service
}
var (
query = flag.String("query", "Google", "Search term")
maxResults = flag.Int64("max-results", 25, "Max YouTube results")
)
//func getChannel(client *http.Client)
func makeLiverData() map[string]Liver {
liverData := make(map[string]Liver)
file, err := os.Open("livers.txt")
if err != nil {
log.Fatalf("failed to open file")
}
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
var lines []string
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
file.Close()
for _, line := range lines {
var liver Liver
info := strings.SplitN(line, ":", 2)
err = json.Unmarshal([]byte(info[1]), &liver)
if err != nil {
fmt.Println(err)
}
liverData[info[0]] = liver
}
return liverData
}
func getYoutubeChannelID(liver Liver) string {
if liver.YoutubeURL == "" {
return ""
}
ch := strings.Split(liver.YoutubeURL, "/")
return ch[len(ch)-1]
}
func initializeYoutubeService() *Service {
apiKey, err := os.ReadFile("./secrets/youtube_api_key")
if err != nil {
log.Fatalf("error reading file: %v", err)
}
service, err := youtube.NewService(context.Background(), option.WithAPIKey(string(apiKey)))
if err != nil {
log.Fatalf("error creating youtube client: %v", err)
}
return &Service{service}
}
// TODO figure out how to use less calls to API (maybe use eventType parameter to call once then search)
func (s *Service) getUpcomingStreams(liverData map[string]Liver) map[string][]Video {
upcomingStreams := make(map[string][]Video)
for liver, data := range liverData {
channelId := getYoutubeChannelID(data)
if channelId != "" {
call := s.Search.List([]string{"id,snippet"}).ChannelId(channelId).Order("date").MaxResults(50)
response, err := call.Do()
if err != nil {
log.Fatal(err)
}
for _, item := range response.Items {
if item.Id.Kind == "youtube#video" && item.Snippet.LiveBroadcastContent == "upcoming" {
video := Video{
Title: item.Snippet.Title,
URL: makeYoutubeVideoURL(item.Id.VideoId),
}
if streams, ok := upcomingStreams[liver]; ok {
upcomingStreams[liver] = append(streams, video)
} else {
upcomingStreams[liver] = []Video{video}
}
}
}
}
}
return upcomingStreams
}
//func (s *Service) findVideosByChannelId()
func makeYoutubeVideoURL(videoId string) string {
return "https://www.youtube.com/watch?v=" + videoId
}
func youtubeTest() {
liverData := makeLiverData()
service := initializeYoutubeService()
streams := service.getUpcomingStreams(liverData)
printStreams(streams)
}
func main() {
youtubeTest()
//http.Handle("/", http.FileServer(http.Dir("./views")))
//http.ListenAndServe(":3000", nil)
}
func printStreams(streams map[string][]Video) {
fmt.Printf("%v:\n\n", "Upcoming")
for name, streams := range streams {
fmt.Printf("%v:\n", name)
for _, stream := range streams {
fmt.Printf("Title: %v URL: %v\n", stream.Title, stream.URL)
}
fmt.Println()
}
fmt.Printf("\n\n")
}