Skip to content

Commit

Permalink
Markets widget rate limit fix
Browse files Browse the repository at this point in the history
  • Loading branch information
svilenmarkov committed Feb 19, 2025
1 parent 16129c5 commit d4565ac
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions internal/glance/widget-markets.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func fetchMarketsDataFromYahoo(marketRequests []marketRequest) (marketList, erro

for i := range marketRequests {
request, _ := http.NewRequest("GET", fmt.Sprintf("https://query1.finance.yahoo.com/v8/finance/chart/%s?range=1mo&interval=1d", marketRequests[i].Symbol), nil)
setBrowserUserAgentHeader(request)
requests = append(requests, request)
}

Expand Down
12 changes: 11 additions & 1 deletion internal/glance/widget-utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import (
"errors"
"fmt"
"io"
"math/rand/v2"
"net/http"
"strconv"
"sync"
"sync/atomic"
"time"
)

Expand All @@ -35,8 +38,15 @@ type requestDoer interface {
Do(*http.Request) (*http.Response, error)
}

var userAgentPersistentVersion atomic.Int32

func setBrowserUserAgentHeader(request *http.Request) {
request.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:123.0) Gecko/20100101 Firefox/123.0")
if rand.IntN(2000) == 0 {
userAgentPersistentVersion.Store(rand.Int32N(5))
}

version := strconv.Itoa(130 + int(userAgentPersistentVersion.Load()))
request.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:"+version+".0) Gecko/20100101 Firefox/"+version+".0")
}

func decodeJsonFromRequest[T any](client requestDoer, request *http.Request) (T, error) {
Expand Down

0 comments on commit d4565ac

Please # to comment.