From 5c12007cb2cca9556d2822cb99f35901bdb0169e Mon Sep 17 00:00:00 2001 From: Scotow Date: Sat, 4 Apr 2020 23:26:02 +0200 Subject: [PATCH] Update to megatools 1.11.0 --- PKGBUILD | 2 +- README.md | 20 +++++++++++--------- mego.go | 25 ++++++++++++++++++------- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/PKGBUILD b/PKGBUILD index bf583e3..5ea07c1 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -1,7 +1,7 @@ # Maintainer: Benjamin Lopez pkgname=mego -pkgver=1.2.1 +pkgver=1.3.0 pkgrel=1 pkgdesc="A simple megadl wrapper with auto-retry and download list" arch=('x86_64') diff --git a/README.md b/README.md index 6fdcfd6..81656f3 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # mego 💾 -Mego is a simple [megatools](https://github.com/megous/megatools) command wrapper, allowing you to use the `megadl` command with a download list of links and add an auto-try tool. +Mego is a simple [megatools](https://megatools.megous.com) command wrapper, allowing you to use the `megatools dl` command with a download list of links and add an auto-try tool. ### Ideas @@ -8,32 +8,34 @@ Megatools is a collection of programs for accessing Mega.nz service from a comma #### Auto-retry -While using the `megadl` command to download a bunch of large files, I often found myself being blocked by Mega because I exceeded the bandwidth limit (aka. error 509). +While using the `megatools dl` command to download a bunch of large files, I often found myself being blocked by Mega because I exceeded the bandwidth limit (aka. error 509). Indeed, Mega allows users to download a few (apparently not fixed) number of GB per day (once again, apparently not fixed). -By default `megadl` only retries 3 times when this error occurred, preventing the download of file during the night or while being away from the computer. To fix this problem, `mego` check the error code returned by `megadl`, and retry if the command failed. +By default `megatools dl` only retries 3 times when this error occurred, preventing the download of file during the night or while being away from the computer. To fix this problem, `mego` check the error code returned by `megatools dl`, and retry if the command failed. #### List of files -Another problem that I found while using `megadl` is the lack of options to download multiple files at once, and keeping track of the done ones. +Another problem that I found while using `megatools dl` is the lack of options to download multiple files at once, and keeping track of the done ones. To solve this problem, `mego` accepts as arguments a path of file(s) containing a list of Mega download links. `mego` will open the file and start downloading the files listed in it. Once the download of the file successfully terminated, `mego` will add a `#` before the link and write it in the file, preventing the next execution to re-download the file. `mego` will also mark invalid links it found with the `#-` string. #### Compatibility -Because this script is a wrapper around the `megadl` command, it heavily depends on the outputs of the command. If you have problems using this script, be sure to use the version 1.10.2 of `megatools`. +Because this script is a wrapper around the `megatools` command, it heavily depends on the outputs of the command. If you have problems using this script, be sure to use the version 1.11.0 (04.04.20) of `megatools`. You can download the latest version [here](https://megatools.megous.com/builds/experimental/). ### Usage ``` Usage of mego: -mego [-l SPEED] [-r DURATION] [-p] MEGA_LINK... LIST_PATH... + mego [-c COMMAND_PATH] [-s SPEED] [-p] [-r INTERVAL] LINK... LINK_PATH... - -l uint speed limit passed to megadl as --limit-speed - -r duration interval between two retries (default 1m0s) - -p pipe megadl's stdout and stderr +Application Options: + -s, --speed-limit=SPEED Speed limit passed to megatools dl as --limit-speed (default: 0) + -p, --pipe-outputs Pipe megatools's stdout and stderr + -r, --retry=INTERVAL Interval between two retries (default: 15min) + -c, --command=COMMAND_PATH Path to the megatools command (default: megatools) ``` NB: The whole content of a *list file* is read and kept in memory. Every time a file is downloaded, the content of the *list file* will be overwritten. So please do not use a *list file* as a queue during execution. diff --git a/mego.go b/mego.go index d21bfe6..c1cab55 100644 --- a/mego.go +++ b/mego.go @@ -16,14 +16,15 @@ import ( ) type options struct { - Speed uint `short:"s" long:"speed-limit" description:"Speed limit passed to megadl as --limit-speed" default:"0" value-name:"SPEED"` - Pipe bool `short:"p" long:"pipe-outputs" description:"Pipe megadl's stdout and stderr"` - Retry time.Duration `short:"r" long:"retry" description:"Interval between two retries" default:"15min" value-name:"INTERVAL"` + Speed uint `short:"s" long:"speed-limit" description:"Speed limit passed to megatools dl as --limit-speed" default:"0" value-name:"SPEED"` + Pipe bool `short:"p" long:"pipe-outputs" description:"Pipe megatools's stdout and stderr"` + Retry time.Duration `short:"r" long:"retry" description:"Interval between two retries" default:"15min" value-name:"INTERVAL"` + CommandPath string `short:"c" long:"command" description:"Path to the megatools command" default:"megatools" value-name:"COMMAND_PATH"` } var ( opts options - linkRegex = regexp.MustCompile(`^(?:https?://)?mega\.nz/#.+$`) + linkRegex = regexp.MustCompile(`^(?:https?://)?mega\.nz/(?:(?:file|folder).+)?#.+$`) ) var ( @@ -40,7 +41,7 @@ func isAlreadyDownloadedError(line, link string) bool { return true } // Typo in the original program. - if strings.HasPrefix(line, fmt.Sprintf("ERROR: Download failed for '%s': Can't rename donwloaded temporary file ", link)) { + if strings.HasPrefix(line, fmt.Sprintf("ERROR: Download failed for '%s': Local file already exists:", link)) { return true } return false @@ -56,7 +57,7 @@ func downloadRepeat(link string) { } func downloadCommand(link string) bool { - cmd := exec.Command("megadl", fmt.Sprintf("--limit-speed=%d", opts.Speed), link) + cmd := exec.Command(opts.CommandPath, "dl", fmt.Sprintf("--limit-speed=%d", opts.Speed), link) var errBuff bytes.Buffer if opts.Pipe { @@ -131,9 +132,14 @@ func writeFilesList(path string, links []string) { } } +func commandExists(cmd string) bool { + _, err := exec.LookPath(cmd) + return err == nil +} + func main() { parser := flags.NewParser(&opts, flags.Default) - parser.Usage = "[-s SPEED] [-p] [-r INTERVAL] LINK... LINK_PATH..." + parser.Usage = "[-c COMMAND_PATH] [-s SPEED] [-p] [-r INTERVAL] LINK... LINK_PATH..." args, err := parser.Parse() if err != nil { @@ -149,6 +155,11 @@ func main() { os.Exit(1) } + if !commandExists(opts.CommandPath) { + errLogger.Printf("Cannot find command \"%s\".\n", opts.CommandPath) + os.Exit(1) + } + for _, arg := range args { if isValidLink(arg) { downloadRepeat(arg)