Created
August 17, 2020 13:49
-
-
Save ashishmaurya/debb2eaabb789541758f666583ff6fd4 to your computer and use it in GitHub Desktop.
Open Directory Downloader in GoLang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"io" | |
"log" | |
"net/http" | |
"net/url" | |
"os" | |
"path" | |
"path/filepath" | |
) | |
func main() { | |
file, err := os.Open("test.csv") | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer file.Close() | |
scanner := bufio.NewScanner(file) | |
var count int | |
for scanner.Scan() { | |
urlString := scanner.Text() | |
v, err := url.Parse(urlString) | |
if err != nil { | |
continue | |
} | |
count++ | |
dirName := path.Join("G:/Torrent", filepath.Dir(v.Path)) | |
err = os.MkdirAll(dirName, os.ModeDir) | |
if err != nil { | |
log.Println(err) | |
} | |
DownloadFile(path.Join(dirName, filepath.Base(v.Path)), urlString) | |
} | |
if err := scanner.Err(); err != nil { | |
log.Fatal(err) | |
} | |
} | |
// DownloadFile will download a url to a local file. It's efficient because it will | |
// write as it downloads and not load the whole file into memory. | |
func DownloadFile(filepath string, url string) error { | |
// Get the data | |
resp, err := http.Get(url) | |
if err != nil { | |
log.Println(err) | |
return err | |
} | |
defer resp.Body.Close() | |
log.Println(filepath) | |
// Create the file | |
out, err := os.Create(filepath) | |
if err != nil { | |
log.Println(err) | |
return err | |
} | |
defer out.Close() | |
// Write the body to file | |
_, err = io.Copy(out, resp.Body) | |
if err != nil { | |
log.Println(err) | |
} | |
return err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment