57 lines
1006 B
Go
57 lines
1006 B
Go
package main
|
|
|
|
import (
|
|
"WorstCaptcha"
|
|
"fmt"
|
|
"gopkg.in/gcfg.v1"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
)
|
|
|
|
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)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Get Twitter Client
|
|
twitter = WorstCaptcha.NewTwitter(config)
|
|
|
|
// Get images
|
|
for wordlink, _ := range twitter.GetImages() {
|
|
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()
|
|
}
|
|
}
|