arnaucube
arnaucube/imgDownloaderPublic Self-hosted repository
imgDownloader/main.go
main.goRaw
 1package main
 2
 3import (
 4	"bufio"
 5	"fmt"
 6	"io"
 7	"log"
 8	"net/http"
 9	"os"
10	"strconv"
11)
12
13func main() {
14	//http://www.image-net.org/search?q=hotdog
15	savelog()
16	//create directory webOutput
17	_ = os.Mkdir("imgs", os.ModePerm)
18	readLinks("imagesLinks.txt")
19
20}
21func readLinks(path string) {
22	inFile, _ := os.Open(path)
23	defer inFile.Close()
24	scanner := bufio.NewScanner(inFile)
25	scanner.Split(bufio.ScanLines)
26
27	var i int
28	for scanner.Scan() {
29		log.Println(scanner.Text())
30		saveLinkToImg(scanner.Text(), i)
31		//log.Println(strconv.Itoa(i) + "/" + strconv.Itoa(nLines))
32		log.Println(i)
33		i++
34	}
35}
36
37func countLines(scanner *bufio.Scanner) int {
38	var count int
39	for scanner.Scan() {
40		count++
41		fmt.Println(count)
42	}
43	return count
44}
45
46func saveLinkToImg(url string, i int) {
47	response, err := http.Get(url)
48	if err != nil {
49		log.Println(err)
50		return
51	}
52
53	defer response.Body.Close()
54
55	//open a file for writing
56	file, err := os.Create("imgs/" + strconv.Itoa(i) + ".png")
57	check(err)
58	// Use io.Copy to just dump the response body to the file. This supports huge files
59	_, err = io.Copy(file, response.Body)
60	check(err)
61	file.Close()
62	fmt.Println("Success!")
63}