GetFreeAssets returns the parsed assets and deadline. If the section exists but is empty, the returned assets slice is empty and the error is nil. If the section cannot be found after retries, ErrLimitedTimeFreeNotFound is returned.
(ctx context.Context)
| 98 | // error is nil. If the section cannot be found after retries, ErrLimitedTimeFreeNotFound |
| 99 | // is returned. |
| 100 | func (s *Scraper) GetFreeAssets(ctx context.Context) ([]Asset, *DeadlineInfo, error) { |
| 101 | if s == nil || s.Source == nil { |
| 102 | return nil, nil, errors.New("fab: missing HTML source") |
| 103 | } |
| 104 | |
| 105 | retries := s.Retries |
| 106 | if retries <= 0 { |
| 107 | retries = 1 |
| 108 | } |
| 109 | |
| 110 | for attempt := 1; attempt <= retries; attempt++ { |
| 111 | htmlDoc, err := s.fetchDocument(ctx) |
| 112 | if err != nil { |
| 113 | if attempt == retries { |
| 114 | log.Printf("Failed to fetch Limited-Time Free assets after several attempts.") |
| 115 | return nil, nil, err |
| 116 | } |
| 117 | log.Printf("Homepage parse error: %v. Retrying %d/%d...", err, attempt, retries) |
| 118 | if sleep := s.backoffDuration(attempt); sleep > 0 { |
| 119 | if err := sleepContext(ctx, sleep); err != nil { |
| 120 | return nil, nil, err |
| 121 | } |
| 122 | } |
| 123 | continue |
| 124 | } |
| 125 | |
| 126 | result, err := ParseFreeAssets(htmlDoc) |
| 127 | if err != nil { |
| 128 | if attempt == retries { |
| 129 | log.Printf("Failed to fetch Limited-Time Free assets after several attempts.") |
| 130 | return nil, nil, err |
| 131 | } |
| 132 | log.Printf("Homepage parse error: %v. Retrying %d/%d...", err, attempt, retries) |
| 133 | if sleep := s.backoffDuration(attempt); sleep > 0 { |
| 134 | if err := sleepContext(ctx, sleep); err != nil { |
| 135 | return nil, nil, err |
| 136 | } |
| 137 | } |
| 138 | continue |
| 139 | } |
| 140 | |
| 141 | if result.Found { |
| 142 | if len(result.Assets) == 0 { |
| 143 | log.Printf("Limited-Time Free section is empty on homepage.") |
| 144 | } else { |
| 145 | log.Printf("Collected %d listing cards from homepage.", len(result.Assets)) |
| 146 | } |
| 147 | return result.Assets, result.Deadline, nil |
| 148 | } |
| 149 | |
| 150 | if attempt == retries { |
| 151 | log.Printf("Failed to fetch Limited-Time Free assets after several attempts.") |
| 152 | return nil, nil, ErrLimitedTimeFreeNotFound |
| 153 | } |
| 154 | log.Printf("'Limited-Time Free' section not found. Attempt %d/%d", attempt, retries) |
| 155 | if sleep := s.backoffDuration(attempt); sleep > 0 { |
| 156 | if err := sleepContext(ctx, sleep); err != nil { |
| 157 | return nil, nil, err |
nothing calls this directly
no test coverage detected