Skip to content

Commit

Permalink
ticketmaster: Support retrieving more than 1k events
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanConnell committed Dec 30, 2023
1 parent 1a1f0dc commit 9961c30
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
48 changes: 37 additions & 11 deletions pkg/ticketmaster/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,48 @@ func NewReader(apiKey string) *Reader {

func (r *Reader) GetEvents(params map[string]string) ([]*Event, error) {
// Construct the URL from the params
url := fmt.Sprintf(getEventsURL, r.apiKey)
rootURL := fmt.Sprintf(getEventsURL, r.apiKey)
for key, value := range params {
url += fmt.Sprintf("&%s=%s", key, value)
rootURL += fmt.Sprintf("&%s=%s", key, value)
}

// TODO: Offset by startDate=events[-1].date to avoid the 1k results limit.
url := rootURL
var events []*Event
for {
resp := &getEventsResponse{}
err := query(url, resp)
if err != nil {
return nil, err
// The ticketmaster API only lets us paginate until we hit 1000 results. To circumvent this
// we will make use of the startDate of the last event we found to start a new search.
var eventsFromPagination int
for {
resp := &getEventsResponse{}
err := query(url, resp)
if err != nil {
return nil, err
}
if len(resp.Embedded.Events) == 0 {
break
}
events = append(events, resp.Embedded.Events...)
eventsFromPagination += len(resp.Embedded.Events)

next, ok := resp.Links["next"]
if !ok {
break
}

// If we continue to query after we've retrieved 1000 results we'll hit an error.
if eventsFromPagination >= 1000 {
break
}
url = fmt.Sprintf("%s%s&apikey=%s", apiURL, next.URL, r.apiKey)
}
events = append(events, resp.Embedded.Events...)

next, ok := resp.Links["next"]
if !ok {
// If we haven't found a multiple of 1000 then we weren't limited by ticketmaster and
// instead simply ran out of events.
if len(events)%1000 != 0 {
break
}
url = fmt.Sprintf("%s%s&apikey=%s", apiURL, next.URL, r.apiKey)

url = fmt.Sprintf("%s&startDateTime=%s", rootURL, events[len(events)-1].Dates.Start.DateTime)
}

return events, nil
Expand All @@ -72,6 +94,10 @@ func query[T any](url string, target T) error {
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("%q returned %q", url, resp.Status)
}

if err := json.NewDecoder(resp.Body).Decode(&target); err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions pkg/ticketmaster/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Venue struct {
type Date struct {
LocalDate string `json:"localDate"`
LocalTime string `json:"localTime"`
DateTime string `json:"dateTime"`
}

type Event struct {
Expand Down

0 comments on commit 9961c30

Please sign in to comment.