Download images

master
Elis Axelsson 2016-04-27 14:57:21 +02:00
parent e813bb5493
commit 1741010449
1 changed files with 30 additions and 2 deletions

View File

@ -5,12 +5,19 @@ import (
"fmt"
"gopkg.in/gcfg.v1"
"os"
"net/http"
"log"
"io"
)
func main() {
var config WorstCaptcha.Config
var twitter WorstCaptcha.Twitter
// Images directory path
dir, _ := os.Getwd()
imageDir := dir + "/images"
// Parse config file
if err := gcfg.ReadFileInto(&config, "worstcaptcha.gcfg"); err != nil {
fmt.Printf("Config error: %s\n", err)
@ -22,7 +29,28 @@ func main() {
// Get images
for wordlink, _ := range twitter.GetImages() {
fmt.Println("===================")
fmt.Println(wordlink)
fileName := imageDir + "/" + wordlink.Word + ".jpg"
response, err := http.Get(wordlink.Link)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
// Open file for writing
file, err := os.Create(fileName)
if err != nil {
log.Fatal(err)
}
// Use io.Copy to just dump the response body to the file. This supports
// huge files
_, err = io.Copy(file, response.Body)
if err != nil {
log.Fatal(err)
}
file.Close()
}
}