-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
119 lines (93 loc) · 2.91 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
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"sync"
"github.com/joho/godotenv"
"github.com/sashabaranov/go-openai"
"google.golang.org/api/option"
"google.golang.org/api/youtube/v3"
)
var YOUTUBE_API_KEY string
var OPENAI_API_KEY string
// Global map to store the active channel listeners
var channelListeners = make(map[string]chan struct{})
var mu sync.Mutex
func getChannelID(service *youtube.Service, username string) (string, error) {
call := service.Channels.List([]string{"id"}).ForHandle(username)
response, err := call.Do()
if err != nil {
return "", err
}
if len(response.Items) > 0 {
return response.Items[0].Id, nil
}
return "", fmt.Errorf("no channel found")
}
func removeChannel(w http.ResponseWriter, r *http.Request) {
username := r.URL.Query().Get("username")
if username == "" {
http.Error(w, "Username is required", http.StatusBadRequest)
return
}
ctx := context.Background()
apiKey := os.Getenv("YOUTUBE_API_KEY")
service, err := youtube.NewService(ctx, option.WithAPIKey(apiKey))
if err != nil {
http.Error(w, fmt.Sprintf("Error creating YouTube service: %s", err.Error()), http.StatusInternalServerError)
return
}
channelID, err := getChannelID(service, username)
if err != nil {
http.Error(w, fmt.Sprintf("Error fetching channel ID: %s", err.Error()), http.StatusInternalServerError)
return
}
stopChan := channelListeners[channelID]
stopChan <- struct{}{}
}
func addChannel(w http.ResponseWriter, r *http.Request) {
username := r.URL.Query().Get("username")
if username == "" {
http.Error(w, "Username is required", http.StatusBadRequest)
return
}
ctx := context.Background()
ytService, err := youtube.NewService(ctx, option.WithAPIKey(YOUTUBE_API_KEY))
if err != nil {
http.Error(w, fmt.Sprintf("Error creating YouTube service: %s", err.Error()), http.StatusInternalServerError)
return
}
channelID, err := getChannelID(ytService, username)
if err != nil {
http.Error(w, fmt.Sprintf("Error fetching channel ID: %s", err.Error()), http.StatusInternalServerError)
return
}
mu.Lock()
defer mu.Unlock()
if _, exists := channelListeners[channelID]; exists {
http.Error(w, "Channel is already being listened to", http.StatusConflict)
return
}
stopChan := make(chan struct{})
channelListeners[channelID] = stopChan
openAIClient := openai.NewClient(OPENAI_API_KEY)
go uploadListener(ytService, openAIClient, channelID, stopChan)
fmt.Fprintf(w, "Started listening to channel %s", channelID)
}
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
YOUTUBE_API_KEY = os.Getenv("YOUTUBE_API_KEY")
OPENAI_API_KEY = os.Getenv("OPENAI_API_KEY")
http.HandleFunc("/addChannel", addChannel)
http.HandleFunc("/removeChannel", removeChannel)
fmt.Println("Starting server at port 8080")
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatalf("Could not start server: %s\n", err.Error())
}
}