Skip to content

Commit

Permalink
Update to megatools 1.11.0
Browse files Browse the repository at this point in the history
  • Loading branch information
scotow committed Apr 4, 2020
1 parent 7e820f4 commit 5c12007
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
2 changes: 1 addition & 1 deletion PKGBUILD
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Maintainer: Benjamin Lopez <[email protected]>

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')
Expand Down
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,41 @@
# 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

Megatools is a collection of programs for accessing Mega.nz service from a command line of your desktop or server.

#### 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.
25 changes: 18 additions & 7 deletions mego.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit 5c12007

Please sign in to comment.