diff options
Diffstat (limited to 'kbbi_client.go')
| -rw-r--r-- | kbbi_client.go | 45 |
1 files changed, 9 insertions, 36 deletions
diff --git a/kbbi_client.go b/kbbi_client.go index 23767b4..0caee38 100644 --- a/kbbi_client.go +++ b/kbbi_client.go @@ -9,7 +9,7 @@ import ( "encoding/gob" "errors" "fmt" - "io/ioutil" + "io" "log" "net/http" "net/http/cookiejar" @@ -20,9 +20,8 @@ import ( "strings" "time" - "github.com/shuLhan/share/lib/debug" - libhttp "github.com/shuLhan/share/lib/http" - "github.com/shuLhan/share/lib/net/html" + "git.sr.ht/~shulhan/pakakeh.go/lib/html" + libhttp "git.sr.ht/~shulhan/pakakeh.go/lib/http" "golang.org/x/net/publicsuffix" ) @@ -64,9 +63,7 @@ const ( maxPageNumber = 501 ) -// // KbbiClient client for official KBBI web using HTTP. -// type KbbiClient struct { baseDir string cookieURL *url.URL @@ -74,10 +71,8 @@ type KbbiClient struct { httpc *http.Client } -// // NewKbbiClient create and initialize new client that connect directly to // KBBI official website. -// func NewKbbiClient() (cl *KbbiClient, err error) { cookieURL, err := url.Parse(kbbiUrlBase) if err != nil { @@ -113,9 +108,7 @@ func NewKbbiClient() (cl *KbbiClient, err error) { return cl, nil } -// // Lookup lookup definition of one or more words. -// func (cl *KbbiClient) Lookup(ins []string) (res LookupResponse, err error) { res = make(LookupResponse, len(ins)) @@ -137,16 +130,12 @@ func (cl *KbbiClient) Lookup(ins []string) (res LookupResponse, err error) { defer httpRes.Body.Close() - body, err := ioutil.ReadAll(httpRes.Body) + body, err := io.ReadAll(httpRes.Body) if err != nil { kata.err = err continue } - if debug.Value >= 3 { - fmt.Printf(">>> HTML body for %s:\n%s", entriURL, body) - } - err = kata.parseHTMLEntri(in, body) if err != nil { kata.err = err @@ -160,9 +149,7 @@ func (cl *KbbiClient) Lookup(ins []string) (res LookupResponse, err error) { return res, nil } -// // ListRootWords list all of the root words in dictionary. -// func (cl *KbbiClient) ListRootWords() (rootWords Words, err error) { params := url.Values{ paramNameMasukan: []string{paramValueDasar}, @@ -189,7 +176,7 @@ func (cl *KbbiClient) ListRootWords() (rootWords Words, err error) { defer res.Body.Close() - body, err := ioutil.ReadAll(res.Body) + body, err := io.ReadAll(res.Body) if err != nil { return rootWords, fmt.Errorf("ListRootWords: page %d: %w", pageNumber, err) @@ -213,17 +200,13 @@ func (cl *KbbiClient) ListRootWords() (rootWords Words, err error) { return rootWords, nil } -// // IsAuthenticated will return true if the client already login; otherwise it // will return false. -// func (cl *KbbiClient) IsAuthenticated() bool { return len(cl.cookies) > 0 } -// // Login authenticate the client using user email and password. -// func (cl *KbbiClient) Login(email, pass string) (err error) { tokenLogin, err := cl.preLogin() if err != nil { @@ -253,7 +236,7 @@ func (cl *KbbiClient) Login(email, pass string) (err error) { defer res.Body.Close() - resBody, err := ioutil.ReadAll(res.Body) + resBody, err := io.ReadAll(res.Body) if err != nil { return fmt.Errorf("Login: %w", err) } @@ -269,9 +252,7 @@ func (cl *KbbiClient) Login(email, pass string) (err error) { return nil } -// // setCookies for HTTP request that need an authentication. -// func (cl *KbbiClient) setCookies() { cl.httpc.Jar.SetCookies(cl.cookieURL, cl.cookies) } @@ -304,9 +285,7 @@ func (cl *KbbiClient) parseHTMLRootWords(htmlBody []byte) ( return rootWords, nil } -// // parseHTMLLogin get the token at the form login. -// func (cl *KbbiClient) parseHTMLLogin(htmlBody []byte) ( token string, err error, ) { @@ -332,9 +311,7 @@ func (cl *KbbiClient) parseHTMLLogin(htmlBody []byte) ( return "", fmt.Errorf("token login not found") } -// // preLogin initialize the client to get the first cookie. -// func (cl *KbbiClient) preLogin() (token string, err error) { req, err := http.NewRequest(http.MethodGet, kbbiUrlLogin, nil) if err != nil { @@ -348,7 +325,7 @@ func (cl *KbbiClient) preLogin() (token string, err error) { defer res.Body.Close() - body, err := ioutil.ReadAll(res.Body) + body, err := io.ReadAll(res.Body) if err != nil { return "", err } @@ -361,9 +338,7 @@ func (cl *KbbiClient) preLogin() (token string, err error) { return token, nil } -// // loadCookies load the KBBI cookies from file. -// func (cl *KbbiClient) loadCookies() (err error) { cl.baseDir, err = os.UserConfigDir() if err != nil { @@ -377,7 +352,7 @@ func (cl *KbbiClient) loadCookies() (err error) { return nil } - body, err := ioutil.ReadFile(f) + body, err := os.ReadFile(f) if err != nil { return fmt.Errorf("loadCookies: %w", err) } @@ -392,9 +367,7 @@ func (cl *KbbiClient) loadCookies() (err error) { return nil } -// // saveCookies store the client cookies to the file for future use. -// func (cl *KbbiClient) saveCookies() { err := os.MkdirAll(filepath.Join(cl.baseDir, configDir), 0700) if err != nil { @@ -410,7 +383,7 @@ func (cl *KbbiClient) saveCookies() { log.Println("saveCookies: ", err) } - err = ioutil.WriteFile(f, buf.Bytes(), 0600) + err = os.WriteFile(f, buf.Bytes(), 0600) if err != nil { log.Println("saveCookies: ", err) } |
