Skip to content

Commit

Permalink
fixed issue (#19), add retry option for fetch post list
Browse files Browse the repository at this point in the history
  • Loading branch information
elvis972602 committed Jul 10, 2023
1 parent 0b3aad7 commit c33c3d8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
7 changes: 3 additions & 4 deletions kemono/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,18 @@ func (k *Kemono) FetchPosts(service, id string) (posts []Post, err error) {
purl := fmt.Sprintf("%s?o=%d", url, page*perUnit)

retryCount := 0
for retryCount < 3 {
for retryCount < k.retry {
resp, err := k.Downloader.Get(purl)
if err != nil {
k.log.Printf("fetch post list error: %v", err)
// Sleep for 5 seconds before retrying
time.Sleep(5 * time.Second)
time.Sleep(k.retryInterval)
retryCount++
continue
}

if resp.StatusCode != http.StatusOK {
k.log.Printf("fetch post list error: %s", resp.Status)
time.Sleep(5 * time.Second)
time.Sleep(k.retryInterval)
retryCount++
continue
}
Expand Down
20 changes: 20 additions & 0 deletions kemono/kemono.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ type Kemono struct {
Downloader Downloader

log Log

retry int

retryInterval time.Duration
}

func NewKemono(options ...Option) *Kemono {
Expand All @@ -75,6 +79,8 @@ func NewKemono(options ...Option) *Kemono {
Banner: true,
postFilters: make(map[string][]PostFilter),
attachmentFilters: make(map[string][]AttachmentFilter),
retry: 3,
retryInterval: 5 * time.Second,
}
for _, option := range options {
option(k)
Expand Down Expand Up @@ -162,6 +168,20 @@ func SetLog(log Log) Option {
}
}

// SetRetry set retry
func SetRetry(retry int) Option {
return func(k *Kemono) {
k.retry = retry
}
}

// SetRetryInterval set retry interval
func SetRetryInterval(retryInterval time.Duration) Option {
return func(k *Kemono) {
k.retryInterval = retryInterval
}
}

// WithCreatorFilter Creator filter
func WithCreatorFilter(filter ...CreatorFilter) Option {
return func(k *Kemono) {
Expand Down
16 changes: 9 additions & 7 deletions main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,12 +346,14 @@ func main() {
log.Fatalf("retry must be greater than 0")
} else {
downloaderOptions = append(downloaderOptions, downloader.Retry(retry))
sharedOptions = append(sharedOptions, kemono.SetRetry(retry))
}

if retryInterval < 0 {
log.Fatalf("retry interval must be greater than 0")
} else {
downloaderOptions = append(downloaderOptions, downloader.RetryInterval(time.Duration(retryInterval)*time.Second))
sharedOptions = append(sharedOptions, kemono.SetRetryInterval(time.Duration(retryInterval)*time.Second))
}

// check maxDownloadGoroutine
Expand Down Expand Up @@ -559,19 +561,19 @@ func setFlag() {
date = config["date"].(int)
}
if !passedFlags["date-before"] && config["date-before"] != nil {
date = config["date-before"].(int)
dateBefore = config["date-before"].(int)
}
if !passedFlags["date-after"] && config["date-after"] != nil {
date = config["date-after"].(int)
dateAfter = config["date-after"].(int)
}
if !passedFlags["update"] && config["update"] != nil {
update = config["update"].(int)
}
if !passedFlags["update-before"] && config["update-before"] != nil {
update = config["update-before"].(int)
updateBefore = config["update-before"].(int)
}
if !passedFlags["update-after"] && config["update-after"] != nil {
update = config["update-after"].(int)
updateAfter = config["update-after"].(int)
}
if !passedFlags["extension-only"] && config["extension-only"] != nil {
extensionOnly = config["extension-only"].(string)
Expand Down Expand Up @@ -689,12 +691,12 @@ func fetchFavoriteCreators(s string, cookie []*http.Cookie) []kemono.FavoriteCre
if resp.StatusCode != 200 {
log.Fatalf("Error getting favorites: %d", resp.StatusCode)
}
var creators []kemono.FavoriteCreator
err = json.NewDecoder(resp.Body).Decode(&creators)
var favoriteCreators []kemono.FavoriteCreator
err = json.NewDecoder(resp.Body).Decode(&favoriteCreators)
if err != nil {
log.Fatalf("Error decoding favorites: %s", err)
}
return creators
return favoriteCreators
}

func fetchFavoritePosts(s string, cookie []*http.Cookie) []kemono.PostRaw {
Expand Down

0 comments on commit c33c3d8

Please sign in to comment.