-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
116 lines (94 loc) · 2.86 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
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/joho/godotenv"
"google.golang.org/api/option"
"google.golang.org/api/youtube/v3"
"bytes"
"encoding/json"
"net/http"
)
type SentimentResponse struct {
Sentiment string `json:"sentiment"`
}
func analyzeSentiment(comment string) (string, error) {
requestBody, err := json.Marshal(map[string]string{
"text": comment,
})
if err != nil {
return "", err
}
resp, err := http.Post("http://localhost:5000/analyze", "application/json", bytes.NewBuffer(requestBody))
if err != nil {
return "", err
}
defer resp.Body.Close()
var result SentimentResponse
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return "", err
}
return result.Sentiment, nil
}
func getComments(service *youtube.Service, videoID string) ([]string, error) {
var comments []string
call := service.CommentThreads.List([]string{"snippet"}).VideoId(videoID).TextFormat("plainText")
response, err := call.Do()
if err != nil {
return nil, err
}
for _, item := range response.Items {
comment := item.Snippet.TopLevelComment.Snippet.TextDisplay
comments = append(comments, comment)
}
return comments, nil
}
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
ctx := context.Background()
apiKey := os.Getenv("API_KEY")
if apiKey == "" {
log.Fatal("API_KEY environment variable not set")
}
service, err := youtube.NewService(ctx, option.WithAPIKey(apiKey))
if err != nil {
log.Fatalf("Error creating YouTube client: %v", err)
}
videoID := "youtube_video_id" // Set video ID here
comments, err := getComments(service, videoID)
if err != nil {
log.Fatalf("Error fetching comments: %v", err)
}
//fetch comments
for _, comment := range comments {
fmt.Println(comment)
}
var positiveComments, negativeComments []string
for _, comment := range comments {
sentiment, err := analyzeSentiment(comment)
if err != nil {
log.Printf("Error analyzing sentiment for comment '%s': %v", comment, err)
continue
}
if sentiment == "Positive" {
positiveComments = append(positiveComments, comment)
} else if sentiment == "Negative" {
negativeComments = append(negativeComments, comment)
}
}
fmt.Printf("Total Positive Comments: %d\n", len(positiveComments))
fmt.Printf("Total Negative Comments: %d\n", len(negativeComments))
fmt.Println("\nPositive Comments:")
for _, comment := range positiveComments {
fmt.Println(comment)
}
fmt.Println("\nNegative Comments:")
for _, comment := range negativeComments {
fmt.Println(comment)
}
}