Skip to content
This repository has been archived by the owner on Feb 13, 2020. It is now read-only.

Commit

Permalink
downloader: Show error when rate limit is exceeded
Browse files Browse the repository at this point in the history
Update #20
  • Loading branch information
n10v committed Jul 24, 2018
1 parent d58ee18 commit 747d23f
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package downloader

import (
"encoding/json"
"errors"
"fmt"
"io"
Expand All @@ -21,6 +22,12 @@ import (
"github.com/valyala/fasthttp"
)

var errRateLimitExceeded = errors.New(`Unfortunately, rate limit is exceeded. Please try tomorrow ¯\_(ツ)_/¯`)

type errorResponse struct {
errors []struct{}
}

type Downloader struct {
// dist is the folder, where tracks will be downloaded.
dist string
Expand All @@ -38,7 +45,7 @@ func NewConfiguredDownloader() *Downloader {

func (downloader Downloader) DownloadAll(tracks []track.Track) {
if len(tracks) == 0 {
logs.FATAL.Println("there are no tracks to download")
logs.FATAL.Fatalln("there are no tracks to download")
}

var errors []string
Expand All @@ -47,6 +54,10 @@ func (downloader Downloader) DownloadAll(tracks []track.Track) {
track := tracks[i]
err := downloader.download(track)
if err != nil {
if err == errRateLimitExceeded {
logs.FEEDBACK.Println()
logs.FATAL.Fatalln(err)
}
errors = append(errors, track.Fullname()+": "+err.Error())
logs.FEEDBACK.Println("✘")
logs.ERROR.Printf("error while downloading %q: %v", track.Fullname(), err)
Expand Down Expand Up @@ -123,6 +134,12 @@ func (downloader Downloader) download(t track.Track) error {

wg.Wait()

// Check if rate limit is not exceeded.
errResp := new(errorResponse)
if e := json.Unmarshal(trackBuf, errResp); e == nil {
return errRateLimitExceeded
}

// Write track to track file.
if _, e := trackFile.Write(trackBuf); e != nil {
return fmt.Errorf("couldn't write track to file: %v", e)
Expand Down

0 comments on commit 747d23f

Please sign in to comment.