Created
November 4, 2020 08:10
-
-
Save anhnd3/d27055936aa737d41d8032b8dd30555c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io" | |
"net/http" | |
"os" | |
) | |
func main() { | |
for i := 1; i < 20000; i++ { | |
fileName := fmt.Sprintf("app_%v.png", i) | |
fileURL := fmt.Sprintf("https://stcstg.zalopay.com.vn/img/history/3x/%v", fileName) | |
err := DownloadFile(fileName, fileURL) | |
if err != nil { | |
fmt.Println(err) | |
} else { | |
fmt.Println("Downloaded: " + fileURL) | |
} | |
} | |
} | |
// DownloadFile will download a url to a local file. It's efficient because it will | |
// write as it downloads and not load the whole file into memory. | |
func DownloadFile(filepath string, url string) error { | |
// Get the data | |
resp, err := http.Get(url) | |
if err != nil { | |
return err | |
} | |
defer resp.Body.Close() | |
if resp.ContentLength == -1 || resp.ContentLength == 305 { | |
return fmt.Errorf("content-length error: %v", resp.ContentLength) | |
} | |
// Create the file | |
out, err := os.Create(filepath) | |
if err != nil { | |
return err | |
} | |
defer out.Close() | |
// Write the body to file | |
_, err = io.Copy(out, resp.Body) | |
return err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment