Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7dcd758

Browse files
author
Sandy
authored
Merge pull request #31 from openset/develop
Udpate: base
2 parents 74d520e + b67f1cf commit 7dcd758

File tree

4 files changed

+69
-35
lines changed

4 files changed

+69
-35
lines changed

‎internal/leetcode/base.go

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package leetcode
33
import (
44
"bytes"
55
"encoding/json"
6+
"errors"
7+
"fmt"
68
"io/ioutil"
79
"net/http"
810
"net/http/cookiejar"
@@ -12,18 +14,24 @@ import (
1214
"path"
1315
)
1416

15-
func checkErr(err error) {
16-
if err != nil {
17-
panic(err)
17+
var err error
18+
19+
func init() {
20+
http.DefaultClient.Jar, err = cookiejar.New(nil)
21+
checkErr(err)
22+
http.DefaultClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
23+
req.Header.Set("Referer", req.URL.String())
24+
fmt.Println(req.URL.String())
25+
if len(via) >= 3 {
26+
return errors.New("stopped after 3 redirects")
27+
}
28+
return nil
1829
}
1930
}
2031

21-
func client() *http.Client {
22-
jar, err := cookiejar.New(nil)
23-
// jar.SetCookies()
24-
checkErr(err)
25-
return &http.Client{
26-
Jar: jar,
32+
func checkErr(err error) {
33+
if err != nil {
34+
panic(err)
2735
}
2836
}
2937

@@ -36,37 +44,59 @@ func getCsrfToken(cookies []*http.Cookie) string {
3644
return ""
3745
}
3846

39-
func getPath(f string) string {
40-
dir := os.TempDir()
47+
func getCachePath(f string) string {
48+
dir, err := os.UserCacheDir()
49+
checkErr(err)
4150
u, err := user.Current()
4251
if err == nil && u.HomeDir != "" {
4352
dir = path.Join(u.HomeDir, ".leetcode")
4453
}
45-
err = os.MkdirAll(dir, 0755)
46-
checkErr(err)
4754
return path.Join(dir, f)
4855
}
4956

50-
func saveCookies(cookies []*http.Cookie) {
51-
data, err := json.Marshal(cookies)
57+
func getFilePath(filename string) string {
58+
if dir := path.Dir(filename); dir != "" {
59+
if err := os.MkdirAll(dir, 0755); err != nil {
60+
checkErr(err)
61+
}
62+
}
63+
return filename
64+
}
65+
66+
func filePutContents(filename string, data []byte) {
67+
filename = getFilePath(filename)
68+
err = ioutil.WriteFile(filename, data, 0644)
69+
checkErr(err)
70+
}
71+
72+
func jsonEncode(v interface{}) []byte {
73+
data, err := json.Marshal(v)
5274
checkErr(err)
5375
dst := bytes.Buffer{}
5476
err = json.Indent(&dst, data, "", "\t")
5577
checkErr(err)
56-
err = ioutil.WriteFile(getPath(cookiesFile), dst.Bytes(), 0755)
57-
checkErr(err)
78+
return dst.Bytes()
79+
}
80+
func saveCookies(cookies []*http.Cookie) {
81+
filePutContents(getCachePath(cookiesFile), jsonEncode(cookies))
5882
}
5983

6084
func getCookies() (cookies []*http.Cookie) {
61-
b, err := ioutil.ReadFile(getPath(cookiesFile))
85+
b, err := ioutil.ReadFile(getCachePath(cookiesFile))
6286
checkErr(err)
6387
err = json.Unmarshal(b, &cookies)
6488
checkErr(err)
6589
return
6690
}
6791

6892
func saveCredential(data url.Values) {
69-
u := url.UserPassword(data.Get("login"), data.Get("password"))
70-
err := ioutil.WriteFile(getPath(credentialsFile), []byte(u.String()), 0755)
93+
filePutContents(getCachePath(credentialsFile), jsonEncode(data))
94+
}
95+
96+
func getCredential() (data url.Values) {
97+
b, err := ioutil.ReadFile(getCachePath(credentialsFile))
98+
checkErr(err)
99+
err = json.Unmarshal(b, &data)
71100
checkErr(err)
101+
return
72102
}

‎internal/leetcode/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ const (
1111

1212
const cookiesFile = "cookies.json"
1313

14-
const credentialsFile = "credentials"
14+
const credentialsFile = "credentials.json"
1515

1616
const problemsAllFile = "problems_all.json"

‎internal/leetcode/login.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,41 @@
11
package leetcode
22

33
import (
4+
"fmt"
45
"net/http"
56
"net/url"
6-
"strings"
77
)
88

99
func AccountsLogin(username, password string) (*http.Response, error) {
1010
resp, err := http.Head(AccountsLoginUrl)
1111
checkErr(err)
1212
defer resp.Body.Close()
13-
cookies := resp.Cookies()
14-
saveCookies(cookies)
15-
csrftoken := getCsrfToken(cookies)
13+
saveCookies(resp.Cookies())
14+
csrftoken := getCsrfToken(resp.Cookies())
1615
data := url.Values{
1716
"login": {username},
1817
"password": {password},
1918
"csrfmiddlewaretoken": {csrftoken},
2019
}
21-
req, err := http.NewRequest("POST", AccountsLoginUrl, strings.NewReader(data.Encode()))
22-
checkErr(err)
23-
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
24-
req.Header.Set("Referer", AccountsLoginUrl)
25-
for _, cookie := range cookies {
26-
req.AddCookie(cookie)
27-
}
28-
resp, err = http.DefaultClient.Do(req)
20+
http.PostForm(AccountsLoginUrl, data)
2921
checkErr(err)
3022
defer resp.Body.Close()
3123
saveCookies(resp.Cookies())
3224
if resp.StatusCode == 200 {
3325
saveCredential(data)
26+
} else {
27+
fmt.Println("login error: ", resp.Status)
3428
}
3529
return resp, err
3630
}
31+
32+
func AutoLogin() (*http.Response, error) {
33+
data := getCredential()
34+
if data.Get("login") == "" {
35+
fmt.Println("can't get username")
36+
}
37+
if data.Get("password") == "" {
38+
fmt.Println("can't get password")
39+
}
40+
return AccountsLogin(data.Get("login"), data.Get("password"))
41+
}

‎internal/leetcode/problem.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ func ProblemsAll() (pa ProblemsAllType) {
7070
dst := bytes.Buffer{}
7171
err = json.Indent(&dst, body, "", "\t")
7272
checkErr(err)
73-
err = ioutil.WriteFile(getPath(problemsAllFile), dst.Bytes(), 0755)
74-
checkErr(err)
73+
filePutContents(getCachePath(problemsAllFile), dst.Bytes())
7574
err = json.Unmarshal(body, &pa)
7675
checkErr(err)
7776
return

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /