Skip to content

Commit

Permalink
fix: improves error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertYoung committed Oct 20, 2022
1 parent 1d9d3e0 commit e6059d2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
19 changes: 16 additions & 3 deletions cmd/manutd-ticket-checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,30 @@ func (c *UnitedChecker) Check() {
name := event.Name()
log.Printf("checking %s", name)

event.LoadEventDetailPage(event)
err = event.LoadEventDetailPage(event)

if err != nil {
log.Print("failed to load event details page: ", err)
continue
}

pages, err := c.browser.Pages()

if err != nil {
panic(err)
log.Print("failed to get pages: ", err)
continue
}

events_page, err := pages.FindByURL("/events/")

if err != nil {
log.Print("failed to find events page: ", err)
continue
}

event_detail_page := UnitedEventDetailPage{
UnitedPage: &UnitedPage{
pages.MustFindByURL("/events/"),
Page: events_page,
},
config: c.config,
}
Expand Down
15 changes: 11 additions & 4 deletions cmd/manutd-ticket-checker/event-item.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,21 @@ func (e *UnitedEventItem) IsPremierLeagueEvent() bool {
return false
}

func (e *UnitedEventItem) BuyButton() *rod.Element {
return e.MustElement("div.addToBasket > a")
func (e *UnitedEventItem) BuyButton() (*rod.Element, error) {
return e.Timeout(5 * time.Second).Element("div.addToBasketXX > a")
}

func (e *UnitedEventItem) LoadEventDetailPage(event *UnitedEventItem) {
buy_button := event.BuyButton()
func (e *UnitedEventItem) LoadEventDetailPage(event *UnitedEventItem) error {
buy_button, err := event.BuyButton()

if err != nil {
return err
}

buy_button.MustEval(`() => this.target="_blank"`)
buy_button.MustClick()

return nil
}

func (e *UnitedEventItem) UpdateState() {
Expand Down
10 changes: 9 additions & 1 deletion cmd/manutd-ticket-checker/event-list-page.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package mutc

import (
"log"

haas "github.com/robertyoung/manutd-ticket-checker/v2/pkg/home-assistant"
)

Expand All @@ -12,7 +14,13 @@ type UnitedEventListPage struct {
}

func (c *UnitedEventListPage) FindEvents(premier_league_only bool) []*UnitedEventItem {
events := c.MustElements("#eventsList .dataItem")
events, err := c.Elements("#eventsList .dataItem")

if err != nil {
log.Print("failed to find event items: ", err)
return []*UnitedEventItem{}
}

var event_list []*UnitedEventItem

for _, element := range events {
Expand Down

0 comments on commit e6059d2

Please sign in to comment.