Add main to download files.

master
Fabien Freling 2014-12-16 21:35:43 +01:00
parent 8e9231d402
commit 5a19118dae
2 changed files with 80 additions and 0 deletions

2
.gitignore vendored
View File

@ -22,3 +22,5 @@ _testmain.go
*.exe
*.test
*.prof
*.swp

78
main.go Normal file
View File

@ -0,0 +1,78 @@
package main
import (
"bufio"
"flag"
"fmt"
"io"
"net/http"
"os"
"path"
"strings"
"time"
)
var (
input = flag.String("input", "", "Input file containing all the urls")
output = flag.String("output", "/tmp", "Output directory")
worker = flag.Int("worker", 4, "Number of workers downloading the images")
)
func main() {
flag.Parse()
file, err := os.Open(*input)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v.\n", err)
os.Exit(1)
}
defer file.Close()
c := make(chan string, *worker)
for i := 0; i < *worker; i++ {
go processFileEntry(c)
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
c <- scanner.Text()
}
close(c)
// Let's wait 10 sec for the last images to download.
time.Sleep(10 * 1e9)
}
func processFileEntry(c <-chan string) {
for str := range c {
downloadFile(str)
}
}
func downloadFile(url string) {
resp, err := http.Get(url)
if err != nil {
fmt.Fprintf(os.Stderr, "Error GET %v: %v", url, err)
return
}
defer resp.Body.Close()
splits := strings.Split(url, "/")
filename := splits[len(splits)-1]
outFile := path.Join(*output, filename)
out, err := os.Create(outFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating %v: %v", out, err)
return
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
fmt.Fprintf(os.Stderr, "Error copying %v to %v", filename, outFile)
return
}
fmt.Printf("Successfully downloading %v to %v\n", url, outFile)
}