mirror of
https://github.com/arnaucube/imgDownloader.git
synced 2026-02-07 11:26:46 +01:00
working
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
imgs
|
||||||
|
logs
|
||||||
9
errors.go
Normal file
9
errors.go
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "log"
|
||||||
|
|
||||||
|
func check(err error) {
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
1261
imagesLinks.txt
Normal file
1261
imagesLinks.txt
Normal file
File diff suppressed because it is too large
Load Diff
19
log.go
Normal file
19
log.go
Normal 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
63
main.go
Normal 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!")
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user