add youtube abilities
j3s j3s@c3f.net
Wed, 17 Mar 2021 16:29:21 -0500
1 files changed,
43 insertions(+),
22 deletions(-)
jump to
M
main.go
→
main.go
@@ -6,6 +6,7 @@ "fmt"
"io" "os" "path/filepath" + "regexp" "sort" "strings"@@ -45,9 +46,40 @@ fmt.Println("stopping")
stream.Stop() return } - if strings.HasPrefix(e.Message, "<a href=\"https://youtube") == true { + if e.Message == "list" { + // send list to sender + fmt.Println("listing") + var keys []string + for k, _ := range files { + if strings.HasPrefix(k, "s-") { + continue + } + keys = append(keys, k) + } + sort.Strings(keys) + list := strings.Join(keys, "\n") + e.Sender.Send(list) + } + var file string + matched, err := regexp.MatchString(`https:\/\/(www.)?youtube\.com\/watch\?v=[A-z_0-9]{11}`, e.Message) + if err != nil { + fmt.Printf("regex match blew up: %s", err) + return + } + if matched { + m := regexp.MustCompile(`https:\/\/(www.)?youtube\.com\/watch\?v=[A-z_0-9]{11}`) + videoUrl := m.FindString(e.Message) fmt.Println("downloading...") - videoID := "BaW_jenozKc" + parts := strings.Split(videoUrl, "?v=") + fmt.Printf("%v", parts) + if len(parts) != 2 { + fmt.Println("youtube url is weird") + return + } + fmt.Println("trash") + fmt.Println(parts[1]) + // videoID := parts[1] + videoID := parts[1] client := youtube.Client{} video, err := client.GetVideo(videoID) if err != nil {@@ -62,36 +94,25 @@ return
} defer resp.Body.Close() - file, err := os.Create("video.mp4") + wfile, err := os.Create("video.mp4") if err != nil { fmt.Println(err) return } - defer file.Close() + defer wfile.Close() - _, err = io.Copy(file, resp.Body) + _, err = io.Copy(wfile, resp.Body) if err != nil { fmt.Println(err) return } - } - if e.Message == "list" { - // send list to sender - fmt.Println("listing") - var keys []string - for k, _ := range files { - if strings.HasPrefix(k, "s-") { - continue - } - keys = append(keys, k) + file = "video.mp4" + } else { + var ok bool + file, ok = files[e.Message] + if !ok { + return } - sort.Strings(keys) - list := strings.Join(keys, "\n") - e.Sender.Send(list) - } - file, ok := files[e.Message] - if !ok { - return } if stream != nil && stream.State() == gumbleffmpeg.StatePlaying { fmt.Println("something else is playing already")