mirror of
https://gitgud.io/yats/libkiwi.git
synced 2026-06-20 01:55:23 -04:00
refactoring
This commit is contained in:
@@ -29,6 +29,16 @@ func loginForm(xfToken string, creds Credentials) url.Values {
|
||||
}
|
||||
}
|
||||
|
||||
func newFormRequest(ctx context.Context, reqURL string, form url.Values) (*http.Request, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", reqURL, strings.NewReader(form.Encode()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func (kf *KF) newLoginRequest(ctx context.Context, creds Credentials) (*http.Request, error) {
|
||||
u := kf.urlFromPath("login")
|
||||
|
||||
@@ -43,17 +53,8 @@ func (kf *KF) newLoginRequest(ctx context.Context, creds Credentials) (*http.Req
|
||||
return nil, err
|
||||
}
|
||||
|
||||
form := loginForm(xfToken, creds)
|
||||
|
||||
// i.e. https://kiwifarms.net/login/login
|
||||
reqURL := fmt.Sprintf("%s/login", u.String())
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", reqURL, strings.NewReader(form.Encode()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
|
||||
return req, nil
|
||||
// e.g. https://kiwifarms.net/login/login
|
||||
return newFormRequest(ctx, fmt.Sprintf("%s/login", u.String()), loginForm(xfToken, creds))
|
||||
}
|
||||
|
||||
func (kf *KF) Login(ctx context.Context, creds Credentials) (*http.Response, error) {
|
||||
@@ -101,16 +102,7 @@ func (kf *KF) newTwoFactorRequest(ctx context.Context, resp *http.Response, code
|
||||
return nil, err
|
||||
}
|
||||
|
||||
form := twoFactorForm(xfToken, code, u)
|
||||
reqURL := fmt.Sprintf("https://%s/login/two-step", u.Hostname())
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", reqURL, strings.NewReader(form.Encode()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
|
||||
return req, nil
|
||||
return newFormRequest(ctx, fmt.Sprintf("https://%s/login/two-step", u.Hostname()), twoFactorForm(xfToken, code, u))
|
||||
}
|
||||
|
||||
func (kf *KF) TwoFactorAuth(ctx context.Context, resp *http.Response, code uint32) (*http.Response, error) {
|
||||
|
||||
+10
-8
@@ -6,12 +6,13 @@ import (
|
||||
)
|
||||
|
||||
// http.CookieJar interface wrapper to add convenience functions.
|
||||
type jar struct {
|
||||
http.CookieJar
|
||||
type cookieJar struct {
|
||||
jar *http.CookieJar
|
||||
}
|
||||
|
||||
func (j *jar) GetCookie(u *url.URL, name string) *http.Cookie {
|
||||
cookies := j.Cookies(u)
|
||||
func (j *cookieJar) GetCookie(u *url.URL, name string) *http.Cookie {
|
||||
jar := (*j.jar)
|
||||
cookies := jar.Cookies(u)
|
||||
for _, c := range cookies {
|
||||
if c.Name == name {
|
||||
return c
|
||||
@@ -21,18 +22,19 @@ func (j *jar) GetCookie(u *url.URL, name string) *http.Cookie {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (j *jar) SetCookie(u *url.URL, newCookie *http.Cookie) {
|
||||
cookies := j.Cookies(u)
|
||||
func (j *cookieJar) SetCookie(u *url.URL, newCookie *http.Cookie) {
|
||||
jar := (*j.jar)
|
||||
cookies := jar.Cookies(u)
|
||||
|
||||
for i, c := range cookies {
|
||||
if c.Name == newCookie.Name {
|
||||
cookies[i] = newCookie
|
||||
j.SetCookies(u, cookies)
|
||||
jar.SetCookies(u, cookies)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Append if not already existing.
|
||||
cookies = append(cookies, newCookie)
|
||||
j.SetCookies(u, cookies)
|
||||
jar.SetCookies(u, cookies)
|
||||
}
|
||||
|
||||
+2
-4
@@ -13,13 +13,11 @@ import (
|
||||
gq "github.com/PuerkitoBio/goquery"
|
||||
)
|
||||
|
||||
const _USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0"
|
||||
|
||||
type KF struct {
|
||||
client http.Client
|
||||
domain *url.URL
|
||||
|
||||
Cookies jar
|
||||
Cookies cookieJar
|
||||
}
|
||||
|
||||
// Supply your own http.Client to route through any proxies.
|
||||
@@ -41,7 +39,7 @@ func NewKF(hc http.Client, host *url.URL) (KF, error) {
|
||||
return KF{
|
||||
client: hc,
|
||||
domain: u,
|
||||
Cookies: jar{hc.Jar},
|
||||
Cookies: cookieJar{&hc.Jar},
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user