forked from MrToy/qqbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.go
50 lines (44 loc) · 1.35 KB
/
store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package qqbot
import (
"encoding/json"
"github.com/syndtr/goleveldb/leveldb"
"net/http"
"net/url"
)
type UserStore struct {
DB *leveldb.DB
}
func NewUserStore(p string) *UserStore {
db, _ := leveldb.OpenFile(p, nil)
return &UserStore{DB: db}
}
func (this *UserStore) Put(user *User) {
u, _ := url.Parse("http://s.web2.qq.com/api/getvfwebqq")
u2, _ := url.Parse("http://d1.web2.qq.com/channel/login2")
cstr, _ := json.Marshal(user.Client.Jar.Cookies(u))
cstr2, _ := json.Marshal(user.Client.Jar.Cookies(u2))
ustr, _ := json.Marshal(user)
this.DB.Put([]byte("cookies"), cstr, nil)
this.DB.Put([]byte("cookies2"), cstr2, nil)
this.DB.Put([]byte("user"), ustr, nil)
}
func (this *UserStore) Get() *User {
u, _ := url.Parse("http://s.web2.qq.com/api/getvfwebqq")
u2, _ := url.Parse("http://d1.web2.qq.com/channel/login2")
cstr, _ := this.DB.Get([]byte("cookies"), nil)
cstr2, _ := this.DB.Get([]byte("cookies2"), nil)
ustr, _ := this.DB.Get([]byte("user"), nil)
user := NewUser()
var cookies, cookies2 []*http.Cookie
json.Unmarshal(ustr, &user)
json.Unmarshal(cstr, &cookies)
json.Unmarshal(cstr2, &cookies2)
user.Client.Jar.SetCookies(u, cookies)
user.Client.Jar.SetCookies(u2, cookies2)
return user
}
func (this *UserStore) Clear() {
this.DB.Delete([]byte("user"), nil)
this.DB.Delete([]byte("cookies2"), nil)
this.DB.Delete([]byte("cookies"), nil)
}