clipper/backend/main.go

140 lines
3.0 KiB
Go
Raw Normal View History

2021-09-04 22:29:12 +00:00
package main
import (
2021-09-05 09:49:59 +00:00
"fmt"
"io"
2021-09-04 22:29:12 +00:00
"log"
2021-09-06 09:39:43 +00:00
"net/http"
2021-09-05 09:49:59 +00:00
"os"
2021-09-06 09:39:43 +00:00
"strings"
"time"
2021-09-04 22:29:12 +00:00
"github.com/kkdai/youtube/v2"
)
2021-09-05 05:24:51 +00:00
const (
2021-09-06 09:39:43 +00:00
ContentTypeAudioM4A = "audio/m4a"
ItagM4AAudio = 140
DefaultHTTPBindAddr = "0.0.0.0:8888"
DefaultTimeout = 5 * time.Second
2021-09-05 05:24:51 +00:00
)
2021-09-05 09:49:59 +00:00
type AudioDownloader struct {
videoID string
ytClient youtube.Client
2021-09-06 09:39:43 +00:00
reader io.Reader
2021-09-05 09:49:59 +00:00
}
2021-09-06 09:39:43 +00:00
func NewAudioDownloader(videoID string) (*AudioDownloader, error) {
var (
reader io.Reader
ytClient youtube.Client
)
2021-09-05 09:49:59 +00:00
2021-09-06 09:39:43 +00:00
cachePath := fmt.Sprintf("cache/%s.m4a", videoID)
2021-09-05 09:49:59 +00:00
fetch := true
2021-09-06 09:39:43 +00:00
2021-09-05 09:49:59 +00:00
if _, err := os.Stat(cachePath); err == nil {
if fptr, err := os.Open(cachePath); err == nil {
reader = fptr
fetch = false
} else {
log.Printf("warning: error opening cache file: %v", err)
}
2021-09-04 22:29:12 +00:00
}
2021-09-05 09:49:59 +00:00
if fetch {
log.Println("fetching audio stream from youtube...")
2021-09-06 09:39:43 +00:00
video, err := ytClient.GetVideo(videoID)
2021-09-05 09:49:59 +00:00
if err != nil {
2021-09-06 09:39:43 +00:00
return nil, fmt.Errorf("error fetching video: %v", err)
2021-09-05 09:49:59 +00:00
}
format := video.Formats.FindByItag(ItagM4AAudio)
2021-09-06 09:39:43 +00:00
log.Printf("M4A format expected to contain %d bytes", format.ContentLength)
stream, _, err := ytClient.GetStream(video, format)
2021-09-05 09:49:59 +00:00
if err != nil {
2021-09-06 09:39:43 +00:00
return nil, fmt.Errorf("error fetching stream: %v", err)
2021-09-05 09:49:59 +00:00
}
2021-09-04 22:29:12 +00:00
2021-09-06 09:39:43 +00:00
// TODO: only allow the cached file to be accessed after it has been
// successfully downloaded.
2021-09-05 09:49:59 +00:00
cacheFile, err := os.Create(cachePath)
if err != nil {
2021-09-06 09:39:43 +00:00
return nil, fmt.Errorf("error creating cache file: %v", err)
2021-09-05 09:49:59 +00:00
}
reader = io.TeeReader(stream, cacheFile)
}
2021-09-04 22:29:12 +00:00
2021-09-06 09:39:43 +00:00
return &AudioDownloader{
videoID: videoID,
ytClient: ytClient,
reader: reader,
}, nil
}
2021-09-04 22:29:12 +00:00
2021-09-06 09:39:43 +00:00
func (d *AudioDownloader) Read(p []byte) (int, error) {
return d.reader.Read(p)
}
2021-09-05 05:24:51 +00:00
2021-09-06 09:39:43 +00:00
func (d *AudioDownloader) Close() error {
if rc, ok := d.reader.(io.ReadCloser); ok {
return rc.Close()
}
2021-09-05 09:49:59 +00:00
return nil
}
2021-09-06 09:39:43 +00:00
func handleRequest(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte("method not allowed"))
return
}
2021-09-05 05:24:51 +00:00
2021-09-06 09:39:43 +00:00
if !strings.HasPrefix(r.URL.Path, "/api/audio") {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("page not found"))
return
}
videoID := r.URL.Query().Get("video_id")
if videoID == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("no video ID provided"))
return
}
downloader, err := NewAudioDownloader(videoID)
2021-09-05 09:49:59 +00:00
if err != nil {
2021-09-06 09:39:43 +00:00
log.Printf("downloader error: %v", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("could not download video"))
return
}
defer downloader.Close()
w.Header().Add("Content-Type", ContentTypeAudioM4A)
2021-09-06 14:24:29 +00:00
w.Header().Add("Access-Control-Allow-Origin", "*")
2021-09-06 09:39:43 +00:00
w.WriteHeader(http.StatusOK)
n, err := io.Copy(w, downloader)
if err != nil {
log.Printf("error writing response: %v", err)
return
}
log.Printf("wrote %d bytes for video ID %s", n, videoID)
}
func main() {
srv := http.Server{
ReadTimeout: DefaultTimeout,
WriteTimeout: DefaultTimeout,
Addr: DefaultHTTPBindAddr,
Handler: http.HandlerFunc(handleRequest),
2021-09-04 22:29:12 +00:00
}
2021-09-06 09:39:43 +00:00
log.Fatal(srv.ListenAndServe())
2021-09-04 22:29:12 +00:00
}