Repository navigation
Expand file tree
/
Copy pathclient.go
More file actions
241 lines (215 loc) · 7.11 KB
/
Copy pathclient.go
File metadata and controls
241 lines (215 loc) · 7.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package javdbapi
import (
"bytes"
"context"
"errors"
"fmt"
"log/slog"
"net/http"
"net/url"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/RPbro/javdbapi/internal/fetch"
"github.com/RPbro/javdbapi/internal/scrape"
"github.com/RPbro/javdbapi/internal/siteurl"
)
func newDocument(body []byte) (*goquery.Document, error) {
return goquery.NewDocumentFromReader(bytes.NewReader(body))
}
// Client is the SDK entry point. All requests issued through one Client
// share the same rate limiter, per the design's shared-budget requirement.
type Client struct {
baseURL *url.URL
locale Locale
fetcher *fetch.Client
logger *slog.Logger
}
func NewClient(config ClientConfig) (*Client, error) {
cfg, err := normalizeClientConfig(config)
if err != nil {
return nil, err
}
baseURL, err := url.Parse(cfg.BaseURL)
if err != nil {
return nil, fmt.Errorf("%w: invalid base url", ErrInvalidConfig)
}
if baseURL.Host == "" {
return nil, fmt.Errorf("%w: invalid base url: missing host", ErrInvalidConfig)
}
if baseURL.User != nil {
return nil, fmt.Errorf("%w: base url must not contain user info", ErrInvalidConfig)
}
if baseURL.Scheme != "http" && baseURL.Scheme != "https" {
return nil, fmt.Errorf("%w: unsupported base url scheme %q", ErrInvalidConfig, baseURL.Scheme)
}
httpClient, err := buildHTTPClient(cfg.HTTP)
if err != nil {
return nil, err
}
fetcher, err := fetch.New(fetch.Config{
HTTPClient: httpClient,
UserAgent: cfg.UserAgent,
MaxResponseBytes: cfg.HTTP.MaxResponseBytes,
CheckRedirect: checkSiteRedirect,
RateLimitDisabled: cfg.RateLimit.Disabled,
RateLimit: cfg.RateLimit.RequestsPerSecond,
RateBurst: cfg.RateLimit.Burst,
RetryDisabled: cfg.Retry.Disabled,
RetryMaxAttempts: cfg.Retry.MaxAttempts,
RetryBaseDelay: cfg.Retry.BaseDelay,
RetryMaxDelay: cfg.Retry.MaxDelay,
})
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrInvalidConfig, err)
}
return &Client{
baseURL: baseURL,
locale: cfg.Locale,
fetcher: fetcher,
logger: cfg.Logger,
}, nil
}
// buildHTTPClient constructs a cloned default transport with an optional
// proxy. The caller's own HTTP.Client, if set, is used and never mutated.
func buildHTTPClient(cfg HTTPConfig) (*http.Client, error) {
if cfg.Client != nil {
return cfg.Client, nil
}
baseTransport, ok := http.DefaultTransport.(*http.Transport)
if !ok {
return nil, fmt.Errorf("%w: default transport is not *http.Transport", ErrInvalidConfig)
}
transport := baseTransport.Clone()
if proxyURL := strings.TrimSpace(cfg.ProxyURL); proxyURL != "" {
parsed, err := url.Parse(proxyURL)
if err != nil {
return nil, fmt.Errorf("%w: invalid proxy url", ErrInvalidConfig)
}
scheme := strings.ToLower(parsed.Scheme)
switch scheme {
case "http", "https", "socks5", "socks5h":
default:
return nil, fmt.Errorf("%w: unsupported proxy scheme %q", ErrInvalidConfig, scheme)
}
parsed.Scheme = scheme
if parsed.Host == "" {
return nil, fmt.Errorf("%w: invalid proxy url: missing host", ErrInvalidConfig)
}
transport.Proxy = http.ProxyURL(parsed)
}
return &http.Client{Timeout: cfg.Timeout, Transport: transport}, nil
}
// VideoURL returns a human-shareable link for id. id is expected to come
// from ParseVideoID or a prior VideoSummary.ID, so URL construction cannot
// fail in practice; on the defensive error path (a hand-built invalid ID)
// it returns an empty string rather than a malformed link.
func (c *Client) VideoURL(id VideoID) string {
target, err := siteurl.Video(c.baseURL, string(id), string(c.locale))
if err != nil {
return ""
}
return target.String()
}
// sanitizedURL strips query values (which may contain the caller's search
// keyword or other sensitive filters) before a URL is surfaced in an error
// or log line.
func sanitizedURL(u *url.URL) string {
if u == nil {
return ""
}
clone := *u
clone.User = nil
clone.RawQuery = ""
clone.ForceQuery = false
clone.Fragment = ""
clone.RawFragment = ""
return clone.String()
}
func mapFetchErr(op string, target *url.URL, err error) error {
var statusErr *fetch.StatusError
switch {
case errors.Is(err, fetch.ErrChallenge):
return &OpError{Op: op, URL: sanitizedURL(target), Err: ErrChallenge}
case errors.Is(err, ErrAuthenticationRequired):
return &OpError{Op: op, URL: sanitizedURL(target), Err: ErrAuthenticationRequired}
case errors.As(err, &statusErr):
return &OpError{Op: op, URL: sanitizedURL(target), Err: &HTTPError{StatusCode: statusErr.StatusCode, URL: sanitizedURL(target)}}
default:
return &OpError{Op: op, URL: sanitizedURL(target), Err: err}
}
}
func mapScrapeErr(op string, target *url.URL, err error) error {
switch {
case errors.Is(err, scrape.ErrEmpty):
return &OpError{Op: op, URL: sanitizedURL(target), Err: ErrEmptyResult}
case errors.Is(err, scrape.ErrParse):
return &OpError{Op: op, URL: sanitizedURL(target), Err: fmt.Errorf("%w: %v", ErrParse, err)}
default:
return &OpError{Op: op, URL: sanitizedURL(target), Err: err}
}
}
func checkSiteRedirect(req *http.Request, via []*http.Request) error {
if err := siteurl.SameOriginRedirect(req.URL, via); err != nil {
return err
}
if req.URL != nil && req.URL.Path == "/login" {
return ErrAuthenticationRequired
}
return nil
}
func (c *Client) logWarnings(op string, warnings []scrape.Warning) {
if c.logger == nil {
return
}
for _, w := range warnings {
c.logger.Warn("parse warning", "op", op, "field", w.Field, "message", w.Message)
}
}
// toPublicScore copies a scrape-layer score into a new public Score so the
// internal and public models never share a mutable pointer.
func toPublicScore(s *scrape.Score) *Score {
if s == nil {
return nil
}
return &Score{Value: s.Value, Count: s.Count}
}
func toPublicSummary(s scrape.Summary) VideoSummary {
return VideoSummary{
ID: VideoID(s.ID),
Code: s.Code,
Title: s.Title,
CoverURL: s.CoverURL,
PublishedAt: s.PublishedAt,
Score: toPublicScore(s.Score),
Availability: Availability{
HasMagnet: s.Availability.HasMagnet,
HasSubtitleMagnet: s.Availability.HasSubtitleMagnet,
IsPlayable: s.Availability.IsPlayable,
},
}
}
func toPublicListPage(p scrape.ListPage) Page[VideoSummary] {
items := make([]VideoSummary, len(p.Items))
for i, s := range p.Items {
items[i] = toPublicSummary(s)
}
return NewPage(p.Number, items, p.HasNext)
}
// fetchList runs the shared fetch -> parse-document -> ParseList -> map flow
// used by every list endpoint (Home, Search, MakerVideos, ActorVideos, Ranking).
func (c *Client) fetchList(ctx context.Context, op string, target *url.URL, page int) (Page[VideoSummary], error) {
body, err := c.fetcher.Get(ctx, target)
if err != nil {
return Page[VideoSummary]{}, mapFetchErr(op+".fetch", target, err)
}
doc, err := newDocument(body)
if err != nil {
return Page[VideoSummary]{}, &OpError{Op: op + ".document", URL: sanitizedURL(target), Err: fmt.Errorf("%w: %v", ErrParse, err)}
}
parsed, err := scrape.ParseList(doc, page)
if err != nil {
return Page[VideoSummary]{}, mapScrapeErr(op+".parse", target, err)
}
c.logWarnings(op+".parse", parsed.Warnings)
return toPublicListPage(parsed.Value), nil
}