This commit is contained in:
arnaucode
2017-10-18 21:03:38 +02:00
parent 55c1ec39d5
commit cb543a7108
5 changed files with 1354 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
imgs
logs

9
errors.go Normal file
View File

@@ -0,0 +1,9 @@
package main
import "log"
func check(err error) {
if err != nil {
log.Println(err)
}
}

1261
imagesLinks.txt Normal file

File diff suppressed because it is too large Load Diff

19
log.go Normal file
View File

@@ -0,0 +1,19 @@
package main
import (
"io"
"log"
"os"
"time"
)
func savelog() {
timeS := time.Now().String()
_ = os.Mkdir("logs", os.ModePerm)
logFile, err := os.OpenFile("logs/log-"+timeS+".log", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
if err != nil {
panic(err)
}
mw := io.MultiWriter(os.Stdout, logFile)
log.SetOutput(mw)
}

63
main.go Normal file
View File

@@ -0,0 +1,63 @@
package main
import (
"bufio"
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
)
func main() {
//http://www.image-net.org/search?q=hotdog
savelog()
//create directory webOutput
_ = os.Mkdir("imgs", os.ModePerm)
readLinks("imagesLinks.txt")
}
func readLinks(path string) {
inFile, _ := os.Open(path)
defer inFile.Close()
scanner := bufio.NewScanner(inFile)
scanner.Split(bufio.ScanLines)
var i int
for scanner.Scan() {
log.Println(scanner.Text())
saveLinkToImg(scanner.Text(), i)
//log.Println(strconv.Itoa(i) + "/" + strconv.Itoa(nLines))
log.Println(i)
i++
}
}
func countLines(scanner *bufio.Scanner) int {
var count int
for scanner.Scan() {
count++
fmt.Println(count)
}
return count
}
func saveLinkToImg(url string, i int) {
response, err := http.Get(url)
if err != nil {
log.Println(err)
return
}
defer response.Body.Close()
//open a file for writing
file, err := os.Create("imgs/" + strconv.Itoa(i) + ".png")
check(err)
// Use io.Copy to just dump the response body to the file. This supports huge files
_, err = io.Copy(file, response.Body)
check(err)
file.Close()
fmt.Println("Success!")
}