summary refs log tree commit diff stats
path: root/tools/nimgrab.nim
blob: 937a5dcd47bc4dea8de59fe37a8314d10b00d7b7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
when defined(windows):
  import os, urldownloader

  proc syncDownload(url, file: string) =
    proc progress(status: DownloadStatus, progress: uint, total: uint,
                  message: string) {.procvar, gcsafe.} =
      echo "Downloading " & url
      let t = total.BiggestInt
      if t != 0:
        echo clamp(int(progress.BiggestInt*100 div t), 0, 100), "%"
      else:
        echo "0%"

    downloadToFile(url, file, {optUseCache}, progress)
    echo "100%"

else:
  import os, asyncdispatch, httpclient

  proc syncDownload(url, file: string) =
    var client = newHttpClient()
    proc onProgressChanged(total, progress, speed: BiggestInt) =
      echo "Downloading " & url & " " & $(speed div 1000) & "kb/s"
      echo clamp(int(progress*100 div total), 0, 100), "%"

    client.onProgressChanged = onProgressChanged
    client.downloadFile(url, file)
    echo "100%"

if os.paramCount() != 2:
  quit "Usage: nimgrab <url> <file>"
else:
  syncDownload(os.paramStr(1), os.paramStr(2))