Skip to content

Instantly share code, notes, and snippets.

@HamzaAnis
Last active February 6, 2018 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HamzaAnis/8d37f3c76fb270a43a04029e33742c54 to your computer and use it in GitHub Desktop.
Save HamzaAnis/8d37f3c76fb270a43a04029e33742c54 to your computer and use it in GitHub Desktop.
Download the images from the link programatically
package main
import (
"bufio"
"encoding/csv"
"fmt"
"io"
"net/http"
"os"
"strings"
)
func downloadFromUrl(url string) {
tokens := strings.Split(url, "/")
fileName := tokens[len(tokens)-1]
fmt.Println("Downloading", url, "to", fileName)
// TODO: check file existence first with io.IsExist
output, err := os.Create(fileName)
if err != nil {
fmt.Println("Error while creating", fileName, "-", err)
return
}
defer output.Close()
response, err := http.Get(url)
if err != nil {
fmt.Println("Error while downloading", url, "-", err)
return
}
defer response.Body.Close()
n, err := io.Copy(output, response.Body)
if err != nil {
fmt.Println("Error while downloading", url, "-", err)
return
}
fmt.Println(n, "bytes downloaded.")
}
func ReadCsvFile(filePath string) {
// Load a csv file.
f, _ := os.Open(filePath)
// Create a new reader.
r := csv.NewReader(bufio.NewReader(f))
for {
record, err := r.Read()
// Stop at EOF.
if err == io.EOF {
break
}
// Display record.
// ... Display record length.
// ... Display all individual elements of the slice.
// fmt.Println(record)
for value := range record {
fmt.Printf(" %v\n", record[value])
downloadFromUrl(record[value])
}
}
}
func main() {
ReadCsvFile("Images.csv")
// countries := []string{"GB", "FR", "ES", "DE", "CN", "CA", "ID", "US"}
// for i := 0; i < len(countries); i++ {
// url := "http://download.geonames.org/export/dump/" + countries[i] + ".zip"
// downloadFromUrl("https://itslondon.s3.amazonaws.com/p/xl/ARMSDSBSETOO1FL.jpg")
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment