diff --git a/.gitignore b/.gitignore index 52240ec..52da4be 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ sillyGirl dev.go .DS_Store develop/jd_cookie -*.cache \ No newline at end of file +*.cache +jd_wskey \ No newline at end of file diff --git a/core/store.go b/core/store.go index 6f3f737..b24a1e7 100644 --- a/core/store.go +++ b/core/store.go @@ -4,36 +4,54 @@ import ( "github.com/boltdb/bolt" ) -var name = "sillyGirl" +var sillyGirl Bucket = "sillyGirl" var db *bolt.DB +type Bucket string + func init() { var err error - db, err = bolt.Open(ExecPath+"/"+name+".cache", 0600, nil) + db, err = bolt.Open(ExecPath+"/sillyGirl.cache", 0600, nil) if err != nil { panic(err) } - err = db.Update(func(tx *bolt.Tx) error { - b := tx.Bucket([]byte(name)) - if b == nil { - tx.CreateBucket([]byte(name)) - } - return nil - }) } -func Set(key string, value string) { +func (bucket Bucket) Set(key, value string) { db.Update(func(tx *bolt.Tx) error { - tx.Bucket([]byte(name)).Put([]byte(key), []byte(value)) + b := tx.Bucket([]byte(bucket)) + if b == nil { + b, _ = tx.CreateBucket([]byte(bucket)) + } + b.Put([]byte(key), []byte(value)) return nil }) } -func Get(key string) string { - value := "" +func (bucket Bucket) Get(kv ...string) string { + var key, value string + for i := range kv { + if i == 0 { + key = kv[0] + } else { + value = kv[1] + } + } db.View(func(tx *bolt.Tx) error { - value = string(tx.Bucket([]byte(name)).Get([]byte(key))) + b := tx.Bucket([]byte(bucket)) + if b == nil { + return nil + } + value = string(b.Get([]byte(key))) return nil }) return value } + +func (bucket Bucket) Foreach(f func(k, v []byte) error) { + db.View(func(tx *bolt.Tx) error { + b := tx.Bucket([]byte(bucket)) + b.ForEach(f) + return nil + }) +} diff --git a/core/test.go b/core/test.go index c536d6f..cf7a4dd 100644 --- a/core/test.go +++ b/core/test.go @@ -11,21 +11,21 @@ func init() { { Rules: []string{"set ? ?"}, Handle: func(s im.Sender) interface{} { - Set(s.Get(0), s.Get(1)) + sillyGirl.Set(s.Get(0), s.Get(1)) return "设置成功" }, }, { Rules: []string{"delete ?"}, Handle: func(s im.Sender) interface{} { - Set(s.Get(), "") + sillyGirl.Set(s.Get(), "") return "删除成功" }, }, { Rules: []string{"get ?"}, Handle: func(s im.Sender) interface{} { - v := Get(s.Get()) + v := sillyGirl.Get(s.Get()) if v == "" { return errors.New("空值") } diff --git a/develop/jd_asset/asset.go b/develop/jd_asset/asset.go index adf9ee6..d79c6b7 100644 --- a/develop/jd_asset/asset.go +++ b/develop/jd_asset/asset.go @@ -1132,7 +1132,15 @@ type UserInfoResult struct { Timestamp int64 `json:"timestamp"` } -func FetchJdCookieValue(key string, cookies string) string { +func FetchJdCookieValue(ps ...string) string { + var key, cookies string + if len(ps) == 2 { + if len(ps[0]) > len(ps[1]) { + key, cookies = ps[1], ps[0] + } else { + key, cookies = ps[0], ps[1] + } + } match := regexp.MustCompile(key + `=([^;]*);{0,1}`).FindStringSubmatch(cookies) if len(match) == 2 { return match[1]