-
Notifications
You must be signed in to change notification settings - Fork 30
feat(pixiv): 实现 Pixiv API 客户端及插画获取模型 #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xyy0411
wants to merge
6
commits into
FloatTech:main
Choose a base branch
from
xyy0411:pr1-pixiv
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1fee742
feat(pixiv): 实现 Pixiv API 客户端及插画获取模型
xyy0411 fc2ea7e
fix:更新gomod
xyy0411 ccf7504
修lint
xyy0411 c8f2179
修lint
xyy0411 3b316ec
Merge branch 'main' into pr1-pixiv
fumiama 9544617
Merge branch 'main' into pr1-pixiv
fumiama File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| // Package pixiv (copied from plugin/pixiv/api/client.go) | ||
| package pixiv | ||
|
|
||
| import ( | ||
| "crypto/tls" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "net/url" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/FloatTech/AnimeAPI/pixiv/model" | ||
| ) | ||
|
|
||
| // HTTPStatusError ... | ||
| type HTTPStatusError struct { | ||
| StatusCode int | ||
| URL string | ||
| } | ||
|
|
||
| func (e *HTTPStatusError) Error() string { | ||
| return fmt.Sprintf("下载图片失败: HTTP %d", e.StatusCode) | ||
| } | ||
|
|
||
| // Client 封装 HTTP 客户端与 Pixiv 请求逻辑 | ||
| type Client struct { | ||
| *http.Client | ||
| transport *http.Transport | ||
| } | ||
|
|
||
| // NewClient ... | ||
| func NewClient() *Client { | ||
| transport := &http.Transport{ | ||
| TLSClientConfig: &tls.Config{MaxVersion: tls.VersionTLS13}, | ||
| Proxy: http.ProxyFromEnvironment, | ||
| } | ||
| return &Client{ | ||
| Client: &http.Client{ | ||
| Transport: transport, | ||
| Timeout: time.Minute, | ||
| }, | ||
| transport: transport, | ||
| } | ||
| } | ||
|
|
||
| // SetProxy 设置代理 | ||
| func (c *Client) SetProxy(proxyURL string) error { | ||
| if c == nil || c.transport == nil { | ||
| return errors.New("pixiv client is nil") | ||
| } | ||
| proxyURL = strings.TrimSpace(proxyURL) | ||
| if proxyURL == "" { | ||
| c.transport.Proxy = http.ProxyFromEnvironment | ||
| return nil | ||
| } | ||
| u, err := url.Parse(proxyURL) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| c.transport.Proxy = http.ProxyURL(u) | ||
| return nil | ||
| } | ||
|
|
||
| // SearchPixivIllustrations ... | ||
| func (c *Client) SearchPixivIllustrations(accessToken, url string) (*model.RootEntity, error) { | ||
| req, err := http.NewRequest("GET", url, nil) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| req.Header.Set("Authorization", "Bearer "+accessToken) | ||
|
|
||
| req.Header.Set("User-Agent", "PixivAndroidApp/5.0.234 (Android 11; Pixel 5)") | ||
| req.Header.Set("App-OS", "android") | ||
| req.Header.Set("App-OS-Version", "11") | ||
| req.Header.Set("App-Version", "5.0.234") | ||
|
|
||
| req.Header.Set("Accept-Language", "en_US") | ||
| req.Header.Set("Referer", "https://app-api.pixiv.net/") | ||
| req.Header.Set("Connection", "keep-alive") | ||
|
|
||
| req.Host = "app-api.pixiv.net" | ||
|
|
||
| resp, err := c.Do(req) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| body, _ := io.ReadAll(resp.Body) | ||
| if resp.StatusCode != 200 { | ||
| return nil, errors.New("搜索失败: " + resp.Status + "\nbody: " + string(body)) | ||
| } | ||
|
|
||
| var result model.RootEntity | ||
| if err := json.Unmarshal(body, &result); err != nil { | ||
| return nil, err | ||
| } | ||
| return &result, nil | ||
| } | ||
|
|
||
| func (c *Client) fetchOnce(targetURL, referer string) ([]byte, int, error) { | ||
| req, err := http.NewRequest("GET", targetURL, nil) | ||
| if err != nil { | ||
| return nil, 0, err | ||
| } | ||
|
|
||
| req.Header.Set("Referer", referer) | ||
| req.Header.Set("User-Agent", "PixivAndroidApp/5.0.234 (Android 11; Pixel 5)") | ||
|
|
||
| resp, err := c.Do(req) | ||
| if err != nil { | ||
| return nil, 0, errors.New("请求失败: " + err.Error()) | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| data, err := io.ReadAll(resp.Body) | ||
| if err != nil { | ||
| return nil, resp.StatusCode, err | ||
| } | ||
|
|
||
| return data, resp.StatusCode, nil | ||
| } | ||
|
|
||
| // FetchPixivImage 直接从 Pixiv 下载图片 | ||
| func (c *Client) FetchPixivImage(illust model.IllustCache, url string) ([]byte, error) { | ||
| fmt.Println("下载", illust.PID) | ||
|
|
||
| if c == nil { | ||
| fmt.Println("FetchPixivImage called on nil IllustCache") | ||
| return nil, nil | ||
| } | ||
|
|
||
| data, status, err := c.fetchOnce(url, "https://www.pixiv.net/") | ||
| if err == nil && status == http.StatusOK { | ||
| return data, nil | ||
| } | ||
| if status == http.StatusNotFound { | ||
| return nil, &HTTPStatusError{StatusCode: status, URL: url} | ||
| } | ||
|
|
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if status != 0 { | ||
| return nil, &HTTPStatusError{StatusCode: status, URL: url} | ||
| } | ||
|
|
||
| return nil, errors.New("下载图片失败") | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
看笑了,这个illust变量的作用是?