feat: added favorites
Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
parent
6d82c3d134
commit
d9fb7dfb88
@ -33,11 +33,11 @@ _Low Level API_
|
||||
|:-----------------|:------------------:|:----|
|
||||
| Authentication | :x: | |
|
||||
| Posts | :heavy_check_mark: | |
|
||||
| Tags | :heavy_minus_sign: | |
|
||||
| Tags | :heavy_check_mark: | |
|
||||
| Tag aliases | :x: | |
|
||||
| Tag implications | :x: | |
|
||||
| Notes | :heavy_check_mark: | |
|
||||
| Pools | :heavy_check_mark: | |
|
||||
| Users | :heavy_check_mark: | |
|
||||
| Favorites | :x: | |
|
||||
| Favorites | :heavy_check_mark: | |
|
||||
| DB export | :x: | |
|
60
example/lowlevel/favorite.go
Normal file
60
example/lowlevel/favorite.go
Normal file
@ -0,0 +1,60 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"git.dragse.it/anthrove/e621-to-graph/pkg/e621/endpoints"
|
||||
"git.dragse.it/anthrove/e621-to-graph/pkg/e621/model"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Define the request context with essential information.
|
||||
requestContext := model.RequestContext{
|
||||
Host: "https://e621.net",
|
||||
UserAgent: "Go-e621-SDK (@username)",
|
||||
Username: "", // Replace with your username
|
||||
APIKey: "", // Replace with your API key
|
||||
}
|
||||
|
||||
// Log: Getting favorites from the API.
|
||||
log.Println("Getting favorites API user: ")
|
||||
|
||||
// Initialize an HTTP client.
|
||||
client := http.Client{}
|
||||
|
||||
// Define query parameters for retrieving favorites.
|
||||
query := map[string]string{
|
||||
"limit": "5",
|
||||
}
|
||||
|
||||
// Call the GetFavorites function to retrieve favorite posts.
|
||||
posts, err := endpoints.GetFavorites(client, requestContext, query)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
} else {
|
||||
// Log the URL of the first favorite post.
|
||||
log.Println(posts[0].File.URL)
|
||||
}
|
||||
log.Println("----------")
|
||||
|
||||
// Log: Getting favorites for a specific user.
|
||||
log.Println("Getting favorites for user: ")
|
||||
|
||||
// Update the query parameters to include a specific user's ID.
|
||||
query = map[string]string{
|
||||
"user_id": "25483", // Replace with the desired user's ID
|
||||
"limit": "5",
|
||||
}
|
||||
|
||||
// Call the GetFavorites function to retrieve favorite posts for the specified user.
|
||||
posts, err = endpoints.GetFavorites(client, requestContext, query)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
} else {
|
||||
// Log the URL of the first favorite post.
|
||||
log.Println(posts[0].File.URL)
|
||||
}
|
||||
log.Println("----------")
|
||||
}
|
66
pkg/e621/endpoints/favorite.go
Normal file
66
pkg/e621/endpoints/favorite.go
Normal file
@ -0,0 +1,66 @@
|
||||
package endpoints
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.dragse.it/anthrove/e621-to-graph/internal/utils"
|
||||
"git.dragse.it/anthrove/e621-to-graph/pkg/e621/model"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// GetFavorites retrieves a user's favorite posts from the e621 API.
|
||||
//
|
||||
// The user_id parameter is required to get the favorites of a user.
|
||||
//
|
||||
// Parameters:
|
||||
// - client: An HTTP client used to make the API request.
|
||||
// - requestContext: The context for the API request, including the host, user agent, username, and API key.
|
||||
// - query: A map containing additional query parameters for the API request.
|
||||
//
|
||||
// Returns:
|
||||
// - []model.Post: A slice of favorite posts.
|
||||
// - error: An error, if any, encountered during the API request or response handling.
|
||||
func GetFavorites(client http.Client, requestContext model.RequestContext, query map[string]string) ([]model.Post, error) {
|
||||
// Create a new HTTP GET request.
|
||||
r, err := http.NewRequest("GET", fmt.Sprintf("%s/favorites.json", requestContext.Host), nil)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
}
|
||||
|
||||
// Append query parameters to the request URL.
|
||||
q := r.URL.Query()
|
||||
for k, v := range query {
|
||||
q.Add(k, v)
|
||||
}
|
||||
r.URL.RawQuery = q.Encode()
|
||||
|
||||
r.Header.Set("User-Agent", requestContext.UserAgent)
|
||||
r.Header.Add("Accept", "application/json")
|
||||
r.SetBasicAuth(requestContext.Username, requestContext.APIKey)
|
||||
|
||||
// Send the request using the provided HTTP client.
|
||||
resp, err := client.Do(r)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
}
|
||||
|
||||
// Check if the HTTP response status code indicates success (2xx range).
|
||||
if resp.StatusCode < 200 || resp.StatusCode > 300 {
|
||||
// If the status code is outside the 2xx range, return an error based on the status code.
|
||||
return nil, utils.StatusCodesToError(resp.StatusCode)
|
||||
}
|
||||
|
||||
// Initialize a User struct to store the response data.
|
||||
var favoriteResponse model.PostResponse
|
||||
|
||||
// Decode the JSON response into the user struct.
|
||||
err = json.NewDecoder(resp.Body).Decode(&favoriteResponse)
|
||||
if err != nil {
|
||||
// Log the error and return an empty User struct and the error.
|
||||
log.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return favoriteResponse.Posts, nil
|
||||
}
|
143
pkg/e621/endpoints/favorite_test.go
Normal file
143
pkg/e621/endpoints/favorite_test.go
Normal file
@ -0,0 +1,143 @@
|
||||
package endpoints
|
||||
|
||||
import (
|
||||
"git.dragse.it/anthrove/e621-to-graph/pkg/e621/model"
|
||||
"github.com/jarcoal/httpmock"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetFavorites(t *testing.T) {
|
||||
httpmock.Activate()
|
||||
defer httpmock.DeactivateAndReset()
|
||||
|
||||
postResponse := model.PostResponse{
|
||||
Post: model.Post{},
|
||||
Posts: []model.Post{
|
||||
{
|
||||
ID: 658415636580,
|
||||
CreatedAt: "2023-10-15T03:58:27.272-04:00",
|
||||
UpdatedAt: "2023-10-19T02:47:33.597-04:00",
|
||||
File: model.File{
|
||||
Width: 759,
|
||||
Height: 980,
|
||||
EXT: "jpg",
|
||||
Size: 640942,
|
||||
Md5: "36e229e910638c7aebe65a500f16f3ee",
|
||||
URL: "https://static1.e621.net/data/36/e2/36e229e910638c7aebe65a500f16f3ee.jpg",
|
||||
},
|
||||
Preview: model.Preview{},
|
||||
Sample: model.Sample{},
|
||||
Score: model.Score{},
|
||||
Tags: model.Tags{},
|
||||
LockedTags: nil,
|
||||
ChangeSeq: 0,
|
||||
Flags: model.Flags{},
|
||||
Rating: "",
|
||||
FavCount: 0,
|
||||
Sources: nil,
|
||||
Pools: nil,
|
||||
Relationships: model.Relationships{},
|
||||
ApproverID: nil,
|
||||
UploaderID: 0,
|
||||
Description: "",
|
||||
CommentCount: 0,
|
||||
IsFavorited: false,
|
||||
HasNotes: false,
|
||||
Duration: nil,
|
||||
},
|
||||
{
|
||||
ID: 698418695,
|
||||
CreatedAt: "2023-10-15T03:58:27.272-04:00",
|
||||
UpdatedAt: "2023-10-19T02:47:33.597-04:00",
|
||||
File: model.File{
|
||||
Width: 759,
|
||||
Height: 980,
|
||||
EXT: "jpg",
|
||||
Size: 640942,
|
||||
Md5: "36e229e910638c7aebe65a500f16f3ee",
|
||||
URL: "https://static1.e621.net/data/36/e2/36e229e910638c7aebe65a500f16f3ee.jpg",
|
||||
},
|
||||
Preview: model.Preview{},
|
||||
Sample: model.Sample{},
|
||||
Score: model.Score{},
|
||||
Tags: model.Tags{},
|
||||
LockedTags: nil,
|
||||
ChangeSeq: 0,
|
||||
Flags: model.Flags{},
|
||||
Rating: "",
|
||||
FavCount: 0,
|
||||
Sources: nil,
|
||||
Pools: nil,
|
||||
Relationships: model.Relationships{},
|
||||
ApproverID: nil,
|
||||
UploaderID: 0,
|
||||
Description: "",
|
||||
CommentCount: 0,
|
||||
IsFavorited: false,
|
||||
HasNotes: false,
|
||||
Duration: nil,
|
||||
},
|
||||
{
|
||||
ID: 48976516894,
|
||||
CreatedAt: "2023-10-15T03:58:27.272-04:00",
|
||||
UpdatedAt: "2023-10-19T02:47:33.597-04:00",
|
||||
File: model.File{
|
||||
Width: 759,
|
||||
Height: 980,
|
||||
EXT: "jpg",
|
||||
Size: 640942,
|
||||
Md5: "36e229e910638c7aebe65a500f16f3ee",
|
||||
URL: "https://static1.e621.net/data/36/e2/36e229e910638c7aebe65a500f16f3ee.jpg",
|
||||
},
|
||||
Preview: model.Preview{},
|
||||
Sample: model.Sample{},
|
||||
Score: model.Score{},
|
||||
Tags: model.Tags{},
|
||||
LockedTags: nil,
|
||||
ChangeSeq: 0,
|
||||
Flags: model.Flags{},
|
||||
Rating: "",
|
||||
FavCount: 0,
|
||||
Sources: nil,
|
||||
Pools: nil,
|
||||
Relationships: model.Relationships{},
|
||||
ApproverID: nil,
|
||||
UploaderID: 0,
|
||||
Description: "",
|
||||
CommentCount: 0,
|
||||
IsFavorited: false,
|
||||
HasNotes: false,
|
||||
Duration: nil,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
jsonResponser, err := httpmock.NewJsonResponder(200, postResponse)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
httpmock.RegisterResponder("GET", "https://e621.net/favorites.json", jsonResponser)
|
||||
|
||||
requestContext := model.RequestContext{
|
||||
Host: "https://e621.net",
|
||||
UserAgent: "Go-e621-SDK (@username)",
|
||||
Username: "memo",
|
||||
APIKey: "123456",
|
||||
}
|
||||
|
||||
client := http.Client{}
|
||||
posts, err := GetFavorites(client, requestContext, map[string]string{})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
if len(posts) == 3 && posts[0].ID == postResponse.Posts[0].ID && posts[1].File.Md5 == postResponse.Posts[1].File.Md5 && posts[2].File.EXT == postResponse.Posts[2].File.EXT {
|
||||
return
|
||||
}
|
||||
|
||||
t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", posts, postResponse)
|
||||
|
||||
}
|
@ -1 +0,0 @@
|
||||
package endpoints
|
Reference in New Issue
Block a user