Browse Source

working

master
arnaucode 7 years ago
parent
commit
cb543a7108
5 changed files with 1354 additions and 0 deletions
  1. +2
    -0
      .gitignore
  2. +9
    -0
      errors.go
  3. +1261
    -0
      imagesLinks.txt
  4. +19
    -0
      log.go
  5. +63
    -0
      main.go

+ 2
- 0
.gitignore

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

+ 9
- 0
errors.go

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

+ 1261
- 0
imagesLinks.txt
File diff suppressed because it is too large
View File


+ 19
- 0
log.go

@ -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
- 0
main.go

@ -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!")
}

Loading…
Cancel
Save