package api import ( "e621_to_neo4j/e621/api/models" "fmt" "io" "log" "net/http" "time" ) // GetFavorites retrieves all favorites from the e621 API. func (c *Client) GetFavorites(user string) ([]models.Post, error) { time.Sleep(2 * time.Second) var lastPostID int64 var allFavorites []models.Post for { url := fmt.Sprintf("%s/posts.json?tags=fav:%s+status:any&page=b%d", baseURL, user, lastPostID) req, err := http.NewRequest("GET", url, nil) if err != nil { return nil, err } req.Header.Set("User-Agent", "FavGetter (by Selloo)") req.Header.Add("Accept", "application/json") req.SetBasicAuth(c.username, c.apiKey) resp, err := c.client.Do(req) if err != nil { return nil, err } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { return nil, err } if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("failed to retrieve posts: %s", resp.Status) } fetchedFavorites, err := models.UnmarshalE621Post(body) if err != nil { log.Printf(err.Error()) } // Append the fetched posts to the result slice allFavorites = append(allFavorites, fetchedFavorites.Posts...) // If no more posts are returned, return the accumulated favorites if len(fetchedFavorites.Posts) == 0 { return allFavorites, nil } // Update the last post ID for the next page request lastPostID = fetchedFavorites.Posts[len(fetchedFavorites.Posts)-1].ID } }