Skip to content

Commit

Permalink
minor bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
birabittoh committed Jan 12, 2024
1 parent 99d543c commit 2e2bbcb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion fixyoutube.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func videoHandler(videoId string, formatIndex int, invidiousClient *invidious.Cl
return
}

video, err := invidiousClient.GetVideo(videoId)
video, err := invidiousClient.GetVideo(videoId, true)
if err != nil {
logger.Info("Wrong video ID: ", videoId)
http.Error(w, "Wrong video ID.", http.StatusNotFound)
Expand Down
26 changes: 15 additions & 11 deletions invidious/invidious.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,21 @@ func (c *Client) fetchVideo(videoId string) (*Video, error) {
return nil, err
}
res.Expire = time.Unix(expireTimestamp, 0)

return res, err
return res, nil
}

func (c *Client) GetVideo(videoId string) (*Video, error) {
func (c *Client) GetVideo(videoId string, fromCache bool) (*Video, error) {
logger.Info("Video https://youtu.be/", videoId, " was requested.")

video, err := GetVideoDB(videoId)
if err == nil {
logger.Info("Found a valid cache entry.")
return video, nil
var video *Video
var err error

if fromCache {
video, err = GetVideoDB(videoId)
if err == nil {
logger.Info("Found a valid cache entry.")
return video, nil
}
}

video, err = c.fetchVideo(videoId)
Expand All @@ -150,7 +154,7 @@ func (c *Client) GetVideo(videoId string) (*Video, error) {
logger.Error("Could not get a new instance: ", err)
time.Sleep(10 * time.Second)
}
return c.GetVideo(videoId)
return c.GetVideo(videoId, true)
}
logger.Info("Retrieved by API.")

Expand Down Expand Up @@ -230,10 +234,10 @@ func (c *Client) ProxyVideo(w http.ResponseWriter, r *http.Request, videoId stri
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
logger.Error(err)
new_video, err := c.fetchVideo(videoId)
new_video, err := c.GetVideo(videoId, false)
if err != nil {
logger.Error("Url for", videoId, "expired:", err)
return http.StatusGone
logger.Error("Cannot get new data for video ", videoId, ":", err)
return http.StatusInternalServerError
}
return c.ProxyVideo(w, r, new_video.VideoId, formatIndex)
}
Expand Down

0 comments on commit 2e2bbcb

Please sign in to comment.