Skip to content

Commit

Permalink
Cache yt-dlp version
Browse files Browse the repository at this point in the history
  • Loading branch information
rroller committed Oct 20, 2024
1 parent 43230aa commit 5fd2d64
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func main() {
log.Panic().Msgf("%s\n", err.Error())
}

media.GetInstalledVersion()
go startYtDlpUpdater()

// The HTTP Server
Expand Down Expand Up @@ -86,14 +87,14 @@ func startYtDlpUpdater() {
ticker := time.NewTicker(12 * time.Hour)

// Do one update now
_, _ = media.UpdateYtDlp()
_ = media.UpdateYtDlp()

quit := make(chan struct{})
go func() {
for {
select {
case <-ticker.C:
_, _ = media.UpdateYtDlp()
_ = media.UpdateYtDlp()
log.Info().Msgf("yt-dlp version: %s", media.GetInstalledVersion())
case <-quit:
ticker.Stop()
Expand Down
2 changes: 1 addition & 1 deletion src/media/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var idCharSet = regexp.MustCompile(`^[a-zA-Z0-9]+$`).MatchString

func Index(w http.ResponseWriter, _ *http.Request) {
data := map[string]string{
"ytDlpVersion": GetInstalledVersion(),
"ytDlpVersion": CachedYtDlpVersion,
}
if err := fetchIndexTmpl.Execute(w, data); err != nil {
log.Error().Msgf("Error rendering template: %v", err)
Expand Down
20 changes: 12 additions & 8 deletions src/media/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
"sync"
)

func UpdateYtDlp() (string, error) {
var CachedYtDlpVersion = ""

func UpdateYtDlp() error {
log.Info().Msgf("Updateing yt-dlp")

cmd := exec.Command("yt-dlp",
Expand All @@ -29,7 +31,7 @@ func UpdateYtDlp() (string, error) {
err := cmd.Start()
if err != nil {
log.Error().Msgf("Error starting command: %v", err)
return "", err
return err
}

var wg sync.WaitGroup
Expand All @@ -46,15 +48,16 @@ func UpdateYtDlp() (string, error) {
err = cmd.Wait()
if err != nil {
log.Error().Msgf("cmd.Run() failed with %s", err)
return "", err
return err
} else if errStdout != nil {
log.Error().Msgf("failed to capture stdout: %v", errStdout)
} else if errStderr != nil {
log.Error().Msgf("failed to capture stderr: %v", errStderr)
}
log.Info().Msgf("Done updating yt-dlp")

return "", nil
log.Info().Msgf("Done updating yt-dlp. Version=%s", GetInstalledVersion())

return nil
}

func GetInstalledVersion() string {
Expand All @@ -69,8 +72,9 @@ func GetInstalledVersion() string {
}

version := strings.TrimSpace(string(s.Bytes()))
if version != "" {
return version
if version == "" {
version = "unknown"
}
return "unknown"
CachedYtDlpVersion = version
return version
}

0 comments on commit 5fd2d64

Please sign in to comment.