Skip to content

Commit

Permalink
only cache the better format (again)
Browse files Browse the repository at this point in the history
  • Loading branch information
birabittoh committed Jan 13, 2024
1 parent 50c43a7 commit 0c53037
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 50 deletions.
8 changes: 8 additions & 0 deletions invidious/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ func (c *Client) fetchVideo(videoId string) (*Video, int) {
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
}

Expand Down
42 changes: 2 additions & 40 deletions invidious/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,12 @@ func CacheVideoDB(v Video) error {
}
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
}

Expand All @@ -81,7 +67,7 @@ func GetVideoDB(videoId string) (*Video, error) {
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
Expand All @@ -92,30 +78,6 @@ func GetVideoDB(videoId string) (*Video, error) {
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
}

Expand Down
4 changes: 1 addition & 3 deletions invidious/invidious.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ type Client struct {
}

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"`
Expand All @@ -47,6 +44,7 @@ type Video struct {
Formats []Format `json:"formatStreams"`
Timestamp time.Time
Expire time.Time
Url string
}

func filter[T any](ss []T, test func(T) bool) (ret []T) {
Expand Down
8 changes: 4 additions & 4 deletions invidious/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"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
Expand Down Expand Up @@ -43,11 +43,11 @@ func (c *Client) proxyUrl(url string) (*bytes.Buffer, int64, int) {
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
}
Expand All @@ -63,5 +63,5 @@ func (c *Client) ProxyVideoId(videoId string) (*bytes.Buffer, int64, int) {
return nil, 0, http.StatusBadRequest
}

return c.proxyVideo(video)
return c.urlToBuffer(video.Url)
}
5 changes: 2 additions & 3 deletions invidious/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CREATE TABLE IF NOT EXISTS videos (
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
);`
Expand All @@ -23,9 +24,7 @@ CREATE TABLE IF NOT EXISTS formats (
);`

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;"

0 comments on commit 0c53037

Please sign in to comment.