all repos — fixyoutube-go @ c9e6e46c2ff2b9496e106388db2f66bef0879330

A better way to embed YouTube videos everywhere (inspired by FixTweet).

more options
Marco Andronaco andronacomarco@gmail.com
Tue, 16 Jan 2024 01:27:21 +0100
commit

c9e6e46c2ff2b9496e106388db2f66bef0879330

parent

5f9d33f75666cde5e6b40a48e034d0d9e6ade442

4 files changed, 44 insertions(+), 18 deletions(-)

jump to
M .env.example.env.example

@@ -1,2 +1,5 @@

+API_KEY=itsme PORT=3000 -API_KEY=changeme +CACHE_DURATION_MINUTES=5 +TIMEOUT_DURATION_MINUTES=10 +MAX_SIZE_MB=20
M fixyoutube.gofixyoutube.go

@@ -149,6 +149,23 @@ io.Copy(w, vb.Buffer)

} } +func getenvDefault(key string, def string) string { + res := os.Getenv(key) + if res == "" { + return def + } + return res +} + +func getenvDefaultParse(key string, def string) float64 { + value := getenvDefault(key, def) + res, err := strconv.ParseFloat(value, 64) + if err != nil { + logger.Fatal(err) + } + return res +} + func main() { logger.SetLevel(logrus.DebugLevel) err := godotenv.Load()

@@ -156,18 +173,19 @@ if err != nil {

logger.Info("No .env file provided.") } - port := os.Getenv("PORT") - if port == "" { - port = "3000" - } + apiKey = getenvDefault("API_KEY", "itsme") + port := getenvDefault("PORT", "3000") + cacheDuration := getenvDefaultParse("CACHE_DURATION_MINUTES", "5") + timeoutDuration := getenvDefaultParse("TIMEOUT_DURATION_MINUTES", "10") + maxSizeMB := getenvDefaultParse("MAX_SIZE_MB", "20") - apiKey = os.Getenv("API_KEY") - if apiKey == "" { - apiKey = "itsme" + myClient := &http.Client{Timeout: 10 * time.Second} + options := invidious.ClientOptions{ + CacheDuration: time.Duration(cacheDuration) * time.Minute, + TimeoutDuration: time.Duration(timeoutDuration) * time.Minute, + MaxSizeBytes: int64(maxSizeMB * 1000000), } - - myClient := &http.Client{Timeout: 10 * time.Second} - videoapi := invidious.NewClient(myClient) + videoapi := invidious.NewClient(myClient, options) r := mux.NewRouter() r.HandleFunc("/", indexHandler)
M invidious/invidious.goinvidious/invidious.go

@@ -10,15 +10,18 @@ "github.com/BiRabittoh/fixyoutube-go/volatile"

"github.com/sirupsen/logrus" ) -const cacheDuration = 5 * time.Minute // 5 m -const timeoutDuration = 10 * time.Minute -const maxSizeBytes = 20000000 // 20 MB const instancesEndpoint = "https://api.invidious.io/instances.json?sort_by=api,type" const videosEndpoint = "https://%s/api/v1/videos/%s?fields=videoId,title,description,author,lengthSeconds,size,formatStreams" var expireRegex = regexp.MustCompile(`(?i)expire=(\d+)`) var logger = logrus.New() +type ClientOptions struct { + CacheDuration time.Duration + TimeoutDuration time.Duration + MaxSizeBytes int64 +} + type VideoBuffer struct { Buffer *bytes.Buffer Length int64

@@ -29,6 +32,7 @@ http *http.Client

timeouts *volatile.Volatile[string, error] buffers *volatile.Volatile[string, VideoBuffer] Instance string + Options ClientOptions } type Video struct {

@@ -109,14 +113,15 @@ }

return video, nil } -func NewClient(httpClient *http.Client) *Client { +func NewClient(httpClient *http.Client, options ClientOptions) *Client { InitDB() - timeouts := volatile.NewVolatile[string, error](timeoutDuration) - buffers := volatile.NewVolatile[string, VideoBuffer](cacheDuration) + timeouts := volatile.NewVolatile[string, error](options.TimeoutDuration) + buffers := volatile.NewVolatile[string, VideoBuffer](options.CacheDuration) client := &Client{ http: httpClient, timeouts: timeouts, buffers: buffers, + Options: options, } err := client.NewInstance() if err != nil {
M invidious/proxy.goinvidious/proxy.go

@@ -31,7 +31,7 @@ if resp.ContentLength == 0 {

return nil, http.StatusNoContent } - if resp.ContentLength > maxSizeBytes { + if resp.ContentLength > c.Options.MaxSizeBytes { logger.Debug("Content-Length exceeds max size.") return nil, http.StatusBadRequest }