You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.1 KiB

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