Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check if cookies are valid #54

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions pkg/client/imdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,27 @@ func authenticateUser(browser *rod.Browser, config *appconfig.IMDb) error {
if *config.Auth == appconfig.IMDbAuthMethodNone {
return nil
}
if *config.Auth == appconfig.IMDbAuthMethodCookies {
return setBrowserCookies(browser, config)
}
tab, err := stealth.Page(browser)
if err != nil {
return fmt.Errorf("failure opening browser tab: %w", err)
}
defer tab.MustClose()
if *config.Auth == appconfig.IMDbAuthMethodCookies {
if err = setBrowserCookies(browser, config); err != nil {
return err
}
if tab, err = navigateAndValidateResponse(tab, imdbPathBase); err != nil {
return fmt.Errorf("failure navigating and validating response: %w", err)
}
authenticated, _, err := tab.Has("#nblogout")
if err != nil {
return fmt.Errorf("failure finding logout div")
}
if !authenticated {
return fmt.Errorf("failure authenticating with the provided cookies")
}
return nil
}
if tab, err = navigateAndValidateResponse(tab, imdbPathBase+imdbPathSignIn); err != nil {
return fmt.Errorf("failure navigating and validating response: %w", err)
}
Expand Down Expand Up @@ -481,10 +494,13 @@ func (c *IMDbClient) lidsScrape() ([]string, error) {
if tab, err = navigateAndValidateResponse(tab, imdbPathBase+imdbPathLists); err != nil {
return nil, fmt.Errorf("failure navigating and validating response: %w", err)
}
listCountDiv, err := tab.Element("div[data-testid='list-page-mc-total-items']")
hasLists, listCountDiv, err := tab.Has("div[data-testid='list-page-mc-total-items']")
if err != nil {
return nil, fmt.Errorf("failure finding list count div: %w", err)
}
if !hasLists {
return make([]string, 0), nil
}
listCountText, err := listCountDiv.Text()
if err != nil {
return nil, fmt.Errorf("failure extracting list count text from div: %w", err)
Expand Down Expand Up @@ -630,7 +646,7 @@ func navigateAndValidateResponse(tab *rod.Page, url string) (*rod.Page, error) {
if status := event.Response.Status; status != http.StatusOK {
return nil, fmt.Errorf("navigating to %s produced %d status", url, status)
}
if err := tab.WaitStable(time.Second); err != nil {
if err := tab.WaitLoad(); err != nil {
return nil, fmt.Errorf("failure waiting for tab to load: %w", err)
}
return tab, nil
Expand Down