first commit

main
aila 2024-01-29 15:54:36 +08:00
commit b8573751dc
8 changed files with 134 additions and 0 deletions

1
README.md Normal file
View File

@ -0,0 +1 @@
""

6
doc.go Normal file
View File

@ -0,0 +1,6 @@
// pixiv project doc.go
/*
pixiv document
*/
package pixiv

9
go.mod Normal file
View File

@ -0,0 +1,9 @@
module git.xiaozou.top/aila/pixiv
go 1.18
require (
github.com/tidwall/gjson v1.17.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
)

6
go.sum Normal file
View File

@ -0,0 +1,6 @@
github.com/tidwall/gjson v1.17.0 h1:/Jocvlh98kcTfpN2+JzGQWQcqrPQwDrVEMApx/M5ZwM=
github.com/tidwall/gjson v1.17.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=

80
novel.go Normal file
View File

@ -0,0 +1,80 @@
// novel
package pixiv
import (
"bytes"
"errors"
"io"
"io/ioutil"
"net/http"
"net/url"
"sync"
"time"
"github.com/tidwall/gjson"
)
type PixivAPI struct {
accessToken string
refreshToken string
httpClient *http.Client
mu sync.Mutex
}
// 初始化
func NewApp() *PixivAPI {
Client := &http.Client{
Timeout: time.Second * 5,
}
return &PixivAPI{httpClient: Client}
}
// 设置Refresh_Token
func (p *PixivAPI) SetRefresh_Token(r string) {
p.mu.Lock()
defer p.mu.Unlock()
p.refreshToken = r
}
// 获取Access_Token
func (p *PixivAPI) GetAccess_Token() error {
URL := "https://oauth.secure.pixiv.net/auth/token"
from := url.Values{}
from.Add("grant_type", "refresh_token")
from.Add("client_id", "MOBrBDS8blbauoSck0ZfDbtuzpyT")
from.Add("client_secret", "lsACyCD94FhDUtGTXi3QzcFE2uU1hqtDaKeqrdwj")
from.Add("include_policy", "true")
from.Add("refresh_token", p.refreshToken)
req, err := http.NewRequest("POST", URL, bytes.NewBufferString(from.Encode()))
req.Header.Set("accept-language", "zh_CN")
req.Header.Set("User-Agent", "PixivAndroidApp/5.0.234")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if err != nil {
return err
}
resp, err := p.httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body := GetBody(resp.Body)
if resp.StatusCode != 200 {
return errors.New(body)
}
AccessToken := gjson.Get(body, "access_token").String()
p.mu.Lock()
p.accessToken = AccessToken
p.mu.Unlock()
return nil
}
// 获取body内容
func GetBody(body io.Reader) string {
b, err := ioutil.ReadAll(body)
if err != nil {
return err.Error()
}
return string(b)
}

18
novel_test.go Normal file
View File

@ -0,0 +1,18 @@
// test
package pixiv
import (
"fmt"
"testing"
)
func TestPixiv(t *testing.T) {
app := NewApp()
app.SetRefresh_Token("JMfu2g_lzwHS7ojLnUA3mifsBRPQLgbfqQLZ9W1M73E")
err := app.GetAccess_Token()
if err != nil {
fmt.Println("获取token失败", err)
return
}
fmt.Println(app.accessToken)
}

2
struct.go Normal file
View File

@ -0,0 +1,2 @@
// struct
package pixiv

12
version.go Normal file
View File

@ -0,0 +1,12 @@
// version
package pixiv
import (
"fmt"
)
const version = 0.1
func Version() {
fmt.Println(version)
}