From 548e7effb7f95d87383ff1ec46f0e9f1d33ce8f5 Mon Sep 17 00:00:00 2001 From: Nonoo Date: Sun, 1 Oct 2023 11:41:15 +0200 Subject: [PATCH] Auto update yt-dlp --- main.go | 19 +++++++++++++++++-- vercheck.go | 8 ++++---- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 70cd668..fee1384 100644 --- a/main.go +++ b/main.go @@ -176,14 +176,29 @@ func main() { fmt.Println("telegram connection up") - ytdlpVersionCheckStr, _ := ytdlpVersionCheckGetStr(ctx) + ytdlpVersionCheckStr, updateNeeded, _ := ytdlpVersionCheckGetStr(ctx) + if updateNeeded { + goutubedl.Path, err = ytdlpDownloadLatest(ctx) + if err != nil { + panic(fmt.Sprint("error: ", err)) + } + ytdlpVersionCheckStr, _, _ = ytdlpVersionCheckGetStr(ctx) + } sendTextToAdmins(ctx, "🤖 Bot started, "+ytdlpVersionCheckStr) go func() { for { time.Sleep(24 * time.Hour) - if s, updateNeededOrError := ytdlpVersionCheckGetStr(ctx); updateNeededOrError { + s, updateNeeded, gotError := ytdlpVersionCheckGetStr(ctx) + if gotError { sendTextToAdmins(ctx, s) + } else if updateNeeded { + goutubedl.Path, err = ytdlpDownloadLatest(ctx) + if err != nil { + panic(fmt.Sprint("error: ", err)) + } + ytdlpVersionCheckStr, _, _ = ytdlpVersionCheckGetStr(ctx) + sendTextToAdmins(ctx, "🤖 Bot updated, "+ytdlpVersionCheckStr) } } }() diff --git a/vercheck.go b/vercheck.go index e77200e..fbc679c 100644 --- a/vercheck.go +++ b/vercheck.go @@ -120,19 +120,19 @@ func ytdlpVersionCheck(ctx context.Context) (latestVersion, currentVersion strin return } -func ytdlpVersionCheckGetStr(ctx context.Context) (res string, updateNeededOrError bool) { +func ytdlpVersionCheckGetStr(ctx context.Context) (res string, updateNeeded, gotError bool) { verCheckCtx, verCheckCtxCancel := context.WithTimeout(ctx, ytdlpVersionCheckTimeout) defer verCheckCtxCancel() var latestVersion, currentVersion string var err error if latestVersion, currentVersion, err = ytdlpVersionCheck(verCheckCtx); err != nil { - return errorStr + ": " + err.Error(), true + return errorStr + ": " + err.Error(), false, true } - updateNeededOrError = currentVersion != latestVersion + updateNeeded = currentVersion != latestVersion res = "yt-dlp version: " + currentVersion - if updateNeededOrError { + if updateNeeded { res = "📢 " + res + " 📢 Update needed! Latest version is " + latestVersion + " 📢" } else { res += " (up to date)"