-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
156 lines (125 loc) · 3.48 KB
/
api.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package weibo_api
import (
"encoding/hex"
"encoding/json"
"errors"
"github.com/EyciaZhou/msghub-http/Utils"
"github.com/EyciaZhou/msghub.go/HttpUtils"
"github.com/EyciaZhou/msghub.go/interface"
"github.com/EyciaZhou/msghub.go/plugins/weibo/types"
"net/url"
"strconv"
"time"
)
const (
_URL_FRIENDS_TIMELINE = "https://api.weibo.com/2/statuses/friends_timeline.json"
_URL_ACCESS_TOKEN = "https://api.weibo.com/oauth2/access_token"
_URL_GET_TOKEN_INFO = "https://api.weibo.com/oauth2/get_token_info"
//_DEFAULT_WEIBO_DELAY = time.Minute * 10
//_FETCH_EACH = 100
)
func weiboErrorChecker(bs []byte) error {
we := weibo_types.Error{}
err1 := json.Unmarshal(bs, &we)
if err1 != nil {
return err1
}
if we.Error != "" {
return errors.New(we.Error)
}
return nil
}
func (p *FriendsTimelineController) firstPage() ([]*weibo_types.Tweet, error) {
args := url.Values{
"access_token": {p.Token},
"count": {strconv.Itoa(p.FetchEach)},
}
wtl := weibo_types.Timeline{}
err := HttpUtils.JsonCheckError("GET", _URL_FRIENDS_TIMELINE, args, weiboErrorChecker, &wtl)
if err != nil {
return nil, err
}
if wtl.Statuses == nil {
wtl.Statuses = []*weibo_types.Tweet{}
}
return wtl.Statuses, nil
}
func (p *FriendsTimelineController) since(SinceId string) ([]*weibo_types.Tweet, error) {
tweets := []*weibo_types.Tweet{}
for {
tweets_new, err := p.pageFlip(SinceId)
if err != nil {
return nil, err
}
if len(tweets_new) == 0 {
return tweets, nil
}
SinceId = tweets_new[0].Idstr
tweets = append(tweets, tweets_new...)
if len(tweets_new) < p.FetchEach/2 {
return tweets, nil
}
}
}
func (p *FriendsTimelineController) pageFlip(SinceId string) ([]*weibo_types.Tweet, error) {
args := url.Values{
"access_token": {p.Token},
"since_id": {SinceId},
"count": {strconv.Itoa(p.FetchEach)},
}
wtl := weibo_types.Timeline{}
err := HttpUtils.JsonCheckError("GET", _URL_FRIENDS_TIMELINE, args, weiboErrorChecker, &wtl)
if err != nil {
return nil, err
}
if wtl.Statuses == nil {
wtl.Statuses = []*weibo_types.Tweet{}
}
return wtl.Statuses, nil
}
type FriendsTimelineController struct {
Token string `json:"token"`
Lstid string `json:"lstid"`
Delay time.Duration `json:"delay"`
FetchEach int `json:"fetch_each"`
}
func ResumeFriendsTimelineController(Status []byte) (*FriendsTimelineController, error) {
result := &FriendsTimelineController{}
err := json.Unmarshal(Status, result)
return result, err
}
func NewFriendsTimelineController(token string, lstid string, delay time.Duration, fetchEach int) *FriendsTimelineController {
return &FriendsTimelineController{
Token: token,
Lstid: lstid,
Delay: delay,
FetchEach: fetchEach,
}
}
func (p *FriendsTimelineController) Type() string {
return "weibo"
}
func (p *FriendsTimelineController) GetDelayBetweenCatchRound() time.Duration {
return p.Delay
}
func (p *FriendsTimelineController) FetchNew() ([]*Interface.Message, error) {
if p.Lstid == "" {
ts, err := p.firstPage()
if err == nil && len(ts) > 0 {
p.Lstid = ts[0].Idstr
}
return (weibo_types.Tweets)(ts).Convert(), err
}
ts, err := p.since(p.Lstid)
if err == nil && len(ts) > 0 {
p.Lstid = ts[0].Idstr
}
return (weibo_types.Tweets)(ts).Convert(), err
}
func (p *FriendsTimelineController) DumpTaskStatus() (Status []byte) {
result, _ := json.Marshal(p)
return result
}
func (p *FriendsTimelineController) Hash() string {
return "weibo_" + hex.EncodeToString(Utils.Sha1(([]byte)(p.Token)))
}