Skip to content

Commit acde470

Browse files
committed
feat(Coomer): Add support for non-mandatory cookies
1 parent fa35bb1 commit acde470

File tree

4 files changed

+40
-20
lines changed

4 files changed

+40
-20
lines changed

cmd/cli/umd.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ func startQuery(
3636
u := umd.New()
3737

3838
if len(cookies) > 0 {
39+
cookieHeader := fetch.CookiesToHeader(cookies)
3940
metadata := umd.Metadata{
40-
umd.SimpCity: map[string]any{
41-
"cookie": fetch.CookiesToHeader(cookies),
42-
},
41+
umd.Coomer: map[string]any{"cookie": cookieHeader},
42+
umd.SimpCity: map[string]any{"cookie": cookieHeader},
4343
}
4444

4545
u = u.WithMetadata(metadata)

cmd/gui/binding.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ func (a *App) QueryMedia(
4646
u := umd.New()
4747

4848
if len(cookies) > 0 {
49+
cookieHeader := fetch.CookiesToHeader(cookies)
4950
metadata := umd.Metadata{
50-
umd.SimpCity: map[string]any{
51-
"cookie": fetch.CookiesToHeader(cookies),
52-
},
51+
umd.Coomer: map[string]any{"cookie": cookieHeader},
52+
umd.SimpCity: map[string]any{"cookie": cookieHeader},
5353
}
5454

5555
u = u.WithMetadata(metadata)

internal/extractors/coomer/api.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package coomer
22

33
import (
44
"fmt"
5+
"maps"
56

67
"github.com/samber/lo"
78
"github.com/vegidio/go-sak/fetch"
@@ -12,10 +13,12 @@ var f = fetch.New(nil, 10)
1213
var baseUrl string
1314
var cssHeaders = map[string]string{"Accept": "text/css"}
1415

15-
func getProfile(service string, user string) (*Profile, error) {
16+
func getProfile(service string, user string, headers map[string]string) (*Profile, error) {
17+
maps.Copy(headers, cssHeaders)
18+
1619
var profile *Profile
1720
url := fmt.Sprintf(baseUrl+"/api/v1/%s/user/%s/profile", service, user)
18-
resp, err := f.GetResult(url, cssHeaders, &profile)
21+
resp, err := f.GetResult(url, headers, &profile)
1922

2023
if err != nil {
2124
return nil, err
@@ -26,16 +29,18 @@ func getProfile(service string, user string) (*Profile, error) {
2629
return profile, nil
2730
}
2831

29-
func getUser(profile Profile) <-chan types.Result[Response] {
32+
func getUser(profile Profile, headers map[string]string) <-chan types.Result[Response] {
3033
out := make(chan types.Result[Response])
3134

35+
maps.Copy(headers, cssHeaders)
36+
3237
go func() {
3338
defer close(out)
3439

3540
for offset := 0; offset <= profile.PostCount; offset += 50 {
3641
var posts []Post
3742
url := fmt.Sprintf(baseUrl+"/api/v1/%s/user/%s/posts?o=%d", profile.Service, profile.Id, offset)
38-
resp, err := f.GetResult(url, cssHeaders, &posts)
43+
resp, err := f.GetResult(url, headers, &posts)
3944

4045
if err != nil {
4146
out <- types.Result[Response]{Err: err}
@@ -49,7 +54,7 @@ func getUser(profile Profile) <-chan types.Result[Response] {
4954
}
5055

5156
for _, post := range posts {
52-
result := <-getPost(profile, post.Id)
57+
result := <-getPost(profile, post.Id, headers)
5358
if result.Err != nil {
5459
out <- types.Result[Response]{Err: result.Err}
5560
continue
@@ -65,15 +70,17 @@ func getUser(profile Profile) <-chan types.Result[Response] {
6570
return out
6671
}
6772

68-
func getPost(profile Profile, postId string) <-chan types.Result[Response] {
73+
func getPost(profile Profile, postId string, headers map[string]string) <-chan types.Result[Response] {
6974
out := make(chan types.Result[Response])
7075

76+
maps.Copy(headers, cssHeaders)
77+
7178
go func() {
7279
defer close(out)
7380

7481
var response Response
7582
url := fmt.Sprintf(baseUrl+"/api/v1/%s/user/%s/post/%s", profile.Service, profile.Id, postId)
76-
resp, err := f.GetResult(url, cssHeaders, &response)
83+
resp, err := f.GetResult(url, headers, &response)
7784

7885
if err != nil {
7986
out <- types.Result[Response]{Err: err}
@@ -91,7 +98,7 @@ func getPost(profile Profile, postId string) <-chan types.Result[Response] {
9198
})
9299

93100
if biggestRevision.Post.RevisionId > 0 && (len(response.Images)+len(response.Videos)) < len(biggestRevision.Post.Attachments) {
94-
out <- getRevision(profile, postId, biggestRevision.Post.RevisionId)
101+
out <- getRevision(profile, postId, biggestRevision.Post.RevisionId, headers)
95102
return
96103
}
97104

@@ -101,10 +108,10 @@ func getPost(profile Profile, postId string) <-chan types.Result[Response] {
101108
return out
102109
}
103110

104-
func getRevision(profile Profile, postId string, revisionId int) types.Result[Response] {
111+
func getRevision(profile Profile, postId string, revisionId int, headers map[string]string) types.Result[Response] {
105112
var response ResponseRevision
106113
url := fmt.Sprintf(baseUrl+"/api/v1/%s/user/%s/post/%s/revision/%d", profile.Service, profile.Id, postId, revisionId)
107-
resp, err := f.GetResult(url, cssHeaders, &response)
114+
resp, err := f.GetResult(url, headers, &response)
108115

109116
if err != nil {
110117
return types.Result[Response]{Err: err}

internal/extractors/coomer/main.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,14 @@ func (c *Coomer) QueryMedia(limit int, extensions []string, deep bool) (*types.R
142142
}
143143

144144
func (c *Coomer) DownloadHeaders() map[string]string {
145-
return nil
145+
headers := make(map[string]string)
146+
147+
cookie, exists := c.Metadata[types.Coomer]["cookie"].(string)
148+
if exists {
149+
headers["Cookie"] = cookie
150+
}
151+
152+
return headers
146153
}
147154

148155
// Compile-time assertion to ensure the extractor implements the Extractor interface
@@ -157,9 +164,15 @@ func (c *Coomer) fetchMedia(
157164
) <-chan saktypes.Result[types.Media] {
158165
out := make(chan saktypes.Result[types.Media])
159166

167+
headers := make(map[string]string)
168+
cookie, exists := c.Metadata[types.Coomer]["cookie"].(string)
169+
if exists {
170+
headers["Cookie"] = cookie
171+
}
172+
160173
sourceValue := reflect.ValueOf(source)
161174
serviceField := sourceValue.FieldByName("Service")
162-
profile, pErr := getProfile(serviceField.String(), source.Name())
175+
profile, pErr := getProfile(serviceField.String(), source.Name(), headers)
163176

164177
if pErr != nil {
165178
out <- saktypes.Result[types.Media]{Err: pErr}
@@ -172,9 +185,9 @@ func (c *Coomer) fetchMedia(
172185

173186
switch s := source.(type) {
174187
case SourceUser:
175-
responses = getUser(*profile)
188+
responses = getUser(*profile, headers)
176189
case SourcePost:
177-
responses = getPost(*profile, s.Id)
190+
responses = getPost(*profile, s.Id, headers)
178191
}
179192

180193
for response := range responses {

0 commit comments

Comments
 (0)