forked from danielmiessler/yt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyt.go
201 lines (175 loc) · 5.28 KB
/
yt.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"github.com/anaskhan96/soup"
"github.com/joho/godotenv"
"google.golang.org/api/googleapi/transport"
"google.golang.org/api/youtube/v3"
)
func getVideoID(url string) string {
pattern := `(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})`
re := regexp.MustCompile(pattern)
match := re.FindStringSubmatch(url)
if len(match) > 1 {
return match[1]
}
return ""
}
func getTranscript(videoID string) (string, error) {
url := "https://www.youtube.com/watch?v=" + videoID
resp, err := soup.Get(url)
if err != nil {
return "", err
}
doc := soup.HTMLParse(resp)
scriptTags := doc.FindAll("script")
for _, scriptTag := range scriptTags {
if strings.Contains(scriptTag.Text(), "captionTracks") {
regex := regexp.MustCompile(`"captionTracks":(\[.*?\])`)
match := regex.FindStringSubmatch(scriptTag.Text())
if len(match) > 1 {
var captionTracks []struct {
BaseURL string `json:"baseUrl"`
}
json.Unmarshal([]byte(match[1]), &captionTracks)
if len(captionTracks) > 0 {
transcriptURL := captionTracks[0].BaseURL
transcriptResp, err := soup.Get(transcriptURL)
if err != nil {
return "", err
}
return transcriptResp, nil
}
}
}
}
return "", fmt.Errorf("transcript not found")
}
func getComments(service *youtube.Service, videoID string) []string {
var comments []string
call := service.CommentThreads.List([]string{"snippet", "replies"}).VideoId(videoID).TextFormat("plainText").MaxResults(100)
response, err := call.Do()
if err != nil {
log.Printf("Failed to fetch comments: %v", err)
return comments
}
for _, item := range response.Items {
topLevelComment := item.Snippet.TopLevelComment.Snippet.TextDisplay
comments = append(comments, topLevelComment)
if item.Replies != nil {
for _, reply := range item.Replies.Comments {
replyText := reply.Snippet.TextDisplay
comments = append(comments, " - "+replyText)
}
}
}
return comments
}
func parseDuration(durationStr string) (int, error) {
matches := regexp.MustCompile(`(?i)PT(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?`).FindStringSubmatch(durationStr)
if len(matches) == 0 {
return 0, fmt.Errorf("invalid duration string: %s", durationStr)
}
hours, _ := strconv.Atoi(matches[1])
minutes, _ := strconv.Atoi(matches[2])
seconds, _ := strconv.Atoi(matches[3])
return hours*60 + minutes + seconds/60, nil
}
func mainFunction(url string, options *Options) {
home_dir, err := os.UserHomeDir()
if err != nil {
log.Fatal("Error getting user home directory")
}
env_file := home_dir + "/.config/fabric/.env"
err = godotenv.Load(env_file)
if err != nil {
log.Fatal("Error loading .env file")
}
apiKey := os.Getenv("YOUTUBE_API_KEY")
if apiKey == "" {
log.Fatal("Error: YOUTUBE_API_KEY not found in ~/.config/fabric/.env. To add please run \"echo YOUTUBE_API_KEY=\"[Your API Key]\" >> ~/.config/fabric/.env\".")
}
videoID := getVideoID(url)
if videoID == "" {
log.Fatal("Invalid YouTube URL")
}
client := &http.Client{
Transport: &transport.APIKey{Key: apiKey},
}
service, err := youtube.New(client)
if err != nil {
log.Fatalf("Error creating YouTube client: %v", err)
}
videoResponse, err := service.Videos.List([]string{"contentDetails"}).Id(videoID).Do()
if err != nil {
log.Fatalf("Error getting video details: %v", err)
}
durationStr := videoResponse.Items[0].ContentDetails.Duration
durationMinutes, err := parseDuration(durationStr)
if err != nil {
log.Fatalf("Error parsing video duration: %v", err)
}
var comments []string
if options.Comments {
comments = getComments(service, videoID)
}
var transcriptText string
transcript, err := getTranscript(videoID)
if err != nil {
transcriptText = fmt.Sprintf("Transcript not available. (%v)", err)
} else {
// Parse the XML transcript
doc := soup.HTMLParse(transcript)
// Extract the text content from the <text> tags
textTags := doc.FindAll("text")
var textBuilder strings.Builder
for _, textTag := range textTags {
textBuilder.WriteString(textTag.Text())
textBuilder.WriteString(" ")
transcriptText = textBuilder.String()
}
}
if options.Duration {
fmt.Println(durationMinutes)
} else if options.Transcript {
fmt.Println(transcriptText)
} else if options.Comments {
jsonComments, _ := json.MarshalIndent(comments, "", " ")
fmt.Println(string(jsonComments))
} else {
output := map[string]interface{}{
"transcript": transcriptText,
"duration": durationMinutes,
"comments": comments,
}
jsonOutput, _ := json.MarshalIndent(output, "", " ")
fmt.Println(string(jsonOutput))
}
}
type Options struct {
Duration bool
Transcript bool
Comments bool
Lang string
}
func main() {
options := &Options{}
flag.BoolVar(&options.Duration, "duration", false, "Output only the duration")
flag.BoolVar(&options.Transcript, "transcript", false, "Output only the transcript")
flag.BoolVar(&options.Comments, "comments", false, "Output the comments on the video")
flag.StringVar(&options.Lang, "lang", "en", "Language for the transcript (default: English)")
flag.Parse()
if flag.NArg() == 0 {
log.Fatal("Error: No URL provided.")
}
url := flag.Arg(0)
mainFunction(url, options)
}