Download images
parent
e813bb5493
commit
1741010449
32
scraper.go
32
scraper.go
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue