only cache the better format (again)
Marco Andronaco andronacomarco@gmail.com
Sat, 13 Jan 2024 14:58:51 +0100
5 files changed,
17 insertions(+),
50 deletions(-)
M
invidious/api.go
→
invidious/api.go
@@ -47,6 +47,14 @@ logger.Error(err)
return nil, http.StatusInternalServerError } res.Expire = time.Unix(expireTimestamp, 0) + + _, l, i := c.findCompatibleFormat(res) + if l == 0 { + logger.Warn("No compatible formats found for video.") + res.Url = "" + } + + res.Url = res.Formats[i].Url return res, http.StatusOK }
M
invidious/cache.go
→
invidious/cache.go
@@ -46,26 +46,12 @@ return err
} defer cacheVideo.Close() - _, err = cacheVideo.Exec(v.VideoId, v.Title, v.Description, v.Uploader, v.Duration, v.Expire) + _, err = cacheVideo.Exec(v.VideoId, v.Title, v.Description, v.Uploader, v.Duration, v.Url, v.Expire) if err != nil { logger.Error("Could not cache video: ", err) return err } - for _, f := range v.Formats { - cacheFormat, err := db.Prepare(cacheFormatQuery) - if err != nil { - logger.Error("Could not cache format: ", err) - return err - } - defer cacheVideo.Close() - - _, err = cacheFormat.Exec(v.VideoId, f.Name, f.Height, f.Width, f.Url) - if err != nil { - logger.Error("Could not cache format: ", err) - return err - } - } return nil }@@ -81,7 +67,7 @@ }
defer getVideo.Close() v := &Video{} - err = getVideo.QueryRow(videoId).Scan(&v.VideoId, &v.Title, &v.Description, &v.Uploader, &v.Duration, &v.Timestamp, &v.Expire) + err = getVideo.QueryRow(videoId).Scan(&v.VideoId, &v.Title, &v.Description, &v.Uploader, &v.Duration, &v.Url, &v.Timestamp, &v.Expire) if err != nil { logger.Debug("Could not get video:", err) return nil, err@@ -90,30 +76,6 @@
if v.Timestamp.After(v.Expire) { logger.Info("Video has expired.") return nil, fmt.Errorf("expired") - } - - getFormat, err := db.Prepare(getFormatQuery) - if err != nil { - logger.Error("Could not get format: ", err) - return nil, err - } - defer getFormat.Close() - - response, err := getFormat.Query(videoId) - if err != nil { - logger.Error("Could not get formats: ", err) - return nil, err - } - defer response.Close() - - for response.Next() { - f := Format{} - err := response.Scan(&f.VideoId, &f.Name, &f.Height, &f.Width, &f.Url) - if err != nil { - logger.Error("Could not get formats: ", err) - return nil, err - } - v.Formats = append(v.Formats, f) } return v, nil
M
invidious/invidious.go
→
invidious/invidious.go
@@ -29,10 +29,7 @@ Instance string
} type Format struct { - VideoId string Name string `json:"qualityLabel"` - Height int - Width int Url string `json:"url"` Container string `json:"container"` Size string `json:"size"`@@ -47,6 +44,7 @@ Duration int `json:"lengthSeconds"`
Formats []Format `json:"formatStreams"` Timestamp time.Time Expire time.Time + Url string } func filter[T any](ss []T, test func(T) bool) (ret []T) {
M
invidious/proxy.go
→
invidious/proxy.go
@@ -6,7 +6,7 @@ "io"
"net/http" ) -func (c *Client) proxyUrl(url string) (*bytes.Buffer, int64, int) { +func (c *Client) urlToBuffer(url string) (*bytes.Buffer, int64, int) { req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { logger.Error(err) // bad request@@ -43,11 +43,11 @@
return b, l, http.StatusOK } -func (c *Client) proxyVideo(video *Video) (*bytes.Buffer, int64, int) { +func (c *Client) findCompatibleFormat(video *Video) (*bytes.Buffer, int64, int) { for i := len(video.Formats) - 1; i >= 0; i-- { url := video.Formats[i].Url logger.Debug(url) - b, l, httpStatus := c.proxyUrl(url) + b, l, httpStatus := c.urlToBuffer(url) if httpStatus == http.StatusOK { return b, l, i }@@ -63,5 +63,5 @@ logger.Info("Cannot proxy a video that is not cached: https://youtu.be/", videoId)
return nil, 0, http.StatusBadRequest } - return c.proxyVideo(video) + return c.urlToBuffer(video.Url) }
M
invidious/queries.go
→
invidious/queries.go
@@ -7,6 +7,7 @@ title TEXT NOT NULL,
description TEXT NOT NULL, uploader TEXT NOT NULL, duration int NOT NULL, + url TEXT NOT NULL, timestamp DATETIME DEFAULT (datetime('now')), expire DATETIME NOT NULL );`@@ -23,9 +24,7 @@ FOREIGN KEY(videoId) REFERENCES videos(videoId)
);` const getVideoQuery = "SELECT * FROM videos WHERE videoId = (?);" -const getFormatQuery = "SELECT * FROM formats WHERE videoId = (?)" -const cacheVideoQuery = "INSERT OR REPLACE INTO videos (videoId, title, description, uploader, duration, expire) VALUES (?, ?, ?, ?, ?, ?);" -const cacheFormatQuery = "INSERT OR REPLACE INTO formats (videoId, name, height, width, url) VALUES (?, ?, ?, ?, ?);" +const cacheVideoQuery = "INSERT OR REPLACE INTO videos (videoId, title, description, uploader, duration, url, expire) VALUES (?, ?, ?, ?, ?, ?, ?);" const clearQuery = "DELETE FROM videos;"