Skip to content

Commit

Permalink
Fix #1
Browse files Browse the repository at this point in the history
  • Loading branch information
mitakeck committed Feb 20, 2017
1 parent 0cf17ad commit 992f7c0
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions download.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (d *Downloader) createURI(category string) ([]string, error) {

func (d *Downloader) download(wg *sync.WaitGroup, q chan string, directory string) {
defer wg.Done()
client := &http.Client{}

for {
url, ok := <-q
Expand All @@ -63,28 +64,35 @@ func (d *Downloader) download(wg *sync.WaitGroup, q chan string, directory strin
continue
}

defer output.Close()

req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Printf("Unable to create request (%s) : %v\n", url, err)
continue
}

client := &http.Client{}
res, resErr := client.Do(req)
if resErr != nil {
fmt.Printf("Error while downloading %s : %v\n", url, err)
continue
}
defer res.Body.Close()
if res.StatusCode == 404 {
fmt.Printf("Error while downloading %s : HTTP Status 404 \n", url)
RmErr := os.Remove(fileName)
if RmErr != nil {
fmt.Printf("Error while remove %s : %v", fileName, err)
}
continue
}

_, err = io.Copy(output, res.Body)
if err != nil {
fmt.Printf("Error while downloading %s : %v\n", url, err)
continue
}

// clean up
output.Close()
res.Body.Close()
// delay
time.Sleep(time.Duration(waitSec) * time.Second)
}
Expand Down

0 comments on commit 992f7c0

Please sign in to comment.