Skip to content

Instantly share code, notes, and snippets.

@Shivam010
Created April 17, 2021 19:16
Show Gist options
  • Save Shivam010/ecee65d068ef216c71d9e44782b185ba to your computer and use it in GitHub Desktop.
Save Shivam010/ecee65d068ef216c71d9e44782b185ba to your computer and use it in GitHub Desktop.
This script can be used to download content from any link of any format
// Package is used to download content from any link of any format
package main
import (
"bytes"
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
)
var verbose bool
func main() {
w := flag.CommandLine.Output()
path, link := ".", ""
// verbose flag
for _, arg := range []string{"v", "trace"} {
flag.BoolVar(&verbose, arg, false, "Trace all the logs and calls")
}
// user email flags
for _, arg := range []string{"url", "link", "u", "l"} {
flag.StringVar(&link, arg, link, "Url/Link of content to download (required)")
}
// download at (path)
for _, arg := range []string{"path", "p"} {
flag.StringVar(&path, arg, path, "Download at path")
}
flag.Usage = func() {
write(w, "Usage of", os.Args[0])
write(w, "It is used to download content from any link of any format")
write(w, " -h, -help\n \tThis help box")
write(w, " -v, -trace\n \tTrace all the logs and calls")
write(w, " -u, -url string\n -l, -link string\n \tUrl/Link of content to download (required)")
write(w, " -p, -path string\n \tSave Downloaded file at this path (default current directory)")
write(w, "")
}
flag.Parse()
if flag.NArg() > 0 {
flag.Usage()
return
}
if link == "" {
write(w, "Download Link is required")
write(w, "")
flag.Usage()
return
}
final(link, path)
}
func final(link, path string) {
var err error
w := flag.CommandLine.Output()
log(w, "Link:", link)
log(w, "Provided Path:", path)
log(w, "Verbose:", verbose)
name := filepath.Base(link)
if path != "." {
dir, input := path, filepath.Base(path)
if strings.ContainsRune(input, '.') {
name = input
dir = filepath.Dir(path)
} else {
path = filepath.Join(path, name)
}
if dir != "." {
err = os.MkdirAll(dir, 0755)
if err != nil {
report(w, "Invalid directory path provided:", dir, err)
return
}
}
} else {
path = filepath.Join(path, name)
}
log(w, "Calculated Path:", path)
out, err := os.Create(path)
if err != nil {
report(w, "Invalid path provided:", path, err)
return
}
defer out.Close()
// Download
log(w, "Downloading download...")
res, err := Get(link)
if err != nil {
report(w, "Unable to download:", link, err)
return
}
all, err := ioutil.ReadAll(res.Body)
if err != nil {
report(w, "unable to read response:", err, "response:", res)
return
}
log(w, "Copying data...")
_, err = io.Copy(out, bytes.NewReader(all))
if err != nil {
report(w, "unable to copy response:", err, "response:", res)
return
}
write(w, "File downloaded at", path)
}
func Get(u string, cookies ...*http.Cookie) (*http.Response, error) {
req, _ := http.NewRequest(http.MethodGet, u, nil)
for _, cook := range cookies {
req.AddCookie(cook)
}
return http.DefaultClient.Do(req)
}
func log(w io.Writer, msg string, args ...interface{}) {
if verbose {
write(w, msg, args...)
}
}
func report(w io.Writer, msg string, args ...interface{}) {
write(w, msg, args...)
os.Exit(1)
}
func write(w io.Writer, msg string, args ...interface{}) {
_, _ = fmt.Fprintln(w, append([]interface{}{msg}, args...)...)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment