nimgrab.nim 1001 B

123456789101112131415161718192021222324252627282930313233343536
  1. when defined(windows):
  2. import os, urldownloader
  3. proc syncDownload(url, file: string) =
  4. proc progress(status: DownloadStatus, progress: uint, total: uint,
  5. message: string) {.procvar, gcsafe.} =
  6. echo "Downloading " & url
  7. let t = total.BiggestInt
  8. if t != 0:
  9. echo clamp(int(progress.BiggestInt*100 div t), 0, 100), "%"
  10. else:
  11. echo "0%"
  12. downloadToFile(url, file, {optUseCache}, progress)
  13. echo "100%"
  14. else:
  15. import os, asyncdispatch, httpclient
  16. proc syncDownload(url, file: string) =
  17. var client = newHttpClient()
  18. proc onProgressChanged(total, progress, speed: BiggestInt) =
  19. echo "Downloading " & url & " " & $(speed div 1000) & "kb/s"
  20. echo clamp(int(progress*100 div total), 0, 100), "%"
  21. client.onProgressChanged = onProgressChanged
  22. client.downloadFile(url, file)
  23. echo "100%"
  24. if os.paramCount() != 2:
  25. quit "Usage: nimgrab <url> <file>"
  26. else:
  27. syncDownload(os.paramStr(1), os.paramStr(2))