Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
happy888888 committed Dec 22, 2020
1 parent 371e969 commit 86260fc
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 36 deletions.
7 changes: 0 additions & 7 deletions src/WeiboClient/WeiboClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ package WeiboClient
import (
"net/http"
"net/http/cookiejar"
"net/url"
"time"
)

// WeiboClient 微博客户端对象,包含一个http客户端及cookieJar
type WeiboClient struct{
client *http.Client
st string
}

// @title init
Expand All @@ -30,11 +28,6 @@ func (w *WeiboClient) init() {
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse /* 不进入重定向 */
},
Transport: &http.Transport{
Proxy: func(_ *http.Request) (*url.URL, error) {
return url.Parse("http://127.0.0.1:8888")
},
},
}
}

Expand Down
29 changes: 25 additions & 4 deletions src/WeiboClient/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@ func (w *WeiboClient) apiConfig() (map[string]interface{}, error) {
return data, nil
}

// @title getST
// @description 获取st参数
// @auth 星辰
// @param
// @return string st的值
func (w *WeiboClient) getST() string {
st := w.getCookie("XSRF-TOKEN", ".m.weibo.cn")
if st != "" {
return st
}
data, err := w.apiConfig()
if err != nil {
return ""
}
st, ok := data["data"].(map[string]interface{})["st"].(string)
if !ok {
return ""
}
return st
}

// @title GeneralButton
// @description 微博按钮接口
// @auth 星辰
Expand Down Expand Up @@ -135,7 +156,7 @@ func (w *WeiboClient) SuperReceiveScore() (map[string]interface{}, error){
func (w *WeiboClient) ComposeRepost(mid string, content string) (map[string]interface{}, error){
req, err := http.NewRequest("POST",
"https://m.weibo.cn/api/statuses/repost",
strings.NewReader("id="+mid+"&content="+content+"&mid="+mid+"&st="+w.st+"&_spr=screen:1920x1080"),
strings.NewReader("id="+mid+"&content="+content+"&mid="+mid+"&st="+w.getST()+"&_spr=screen:1920x1080"),
)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Referer", "https://m.weibo.cn/compose/repost")
Expand Down Expand Up @@ -164,7 +185,7 @@ func (w *WeiboClient) ComposeRepost(mid string, content string) (map[string]inte
func (w *WeiboClient) DelMyblog(mid string) (map[string]interface{}, error){
req, err := http.NewRequest("POST",
"https://m.weibo.cn/profile/delMyblog",
strings.NewReader("mid="+mid+"&st="+w.st+"&_spr=screen:1920x1080"),
strings.NewReader("mid="+mid+"&st="+w.getST()+"&_spr=screen:1920x1080"),
)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Referer", "https://m.weibo.cn/profile/")
Expand Down Expand Up @@ -194,7 +215,7 @@ func (w *WeiboClient) DelMyblog(mid string) (map[string]interface{}, error){
func (w *WeiboClient) CommentsCreate(mid string, content string) (map[string]interface{}, error){
req, err := http.NewRequest("POST",
"https://m.weibo.cn/api/comments/create",
strings.NewReader("content="+content+"&mid="+mid+"&st="+w.st+"&_spr=screen:1920x1080"),
strings.NewReader("content="+content+"&mid="+mid+"&st="+w.getST()+"&_spr=screen:1920x1080"),
)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Referer", "https://m.weibo.cn/detail/")
Expand Down Expand Up @@ -223,7 +244,7 @@ func (w *WeiboClient) CommentsCreate(mid string, content string) (map[string]int
func (w *WeiboClient) CommentsDestroy(cid string) (map[string]interface{}, error){
req, err := http.NewRequest("POST",
"https://m.weibo.cn/comments/destroy",
strings.NewReader("cid="+cid+"&st="+w.st+"&_spr=screen:1920x1080"),
strings.NewReader("cid="+cid+"&st="+w.getST()+"&_spr=screen:1920x1080"),
)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Referer", "https://m.weibo.cn/detail/")
Expand Down
46 changes: 21 additions & 25 deletions src/WeiboClient/loginByCookies.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,26 @@ func (w *WeiboClient) GetCookies() []Cookie {
return rcookies
}

// @title getCookie
// @description 获取指定cookie
// @auth 星辰
// @param name string cookie名称
// @param domain string cookie所在域名
// @return string cookie的value
func (w *WeiboClient) getCookie(name string, domain string) string {
cookies := w.client.Jar.Cookies(
&url.URL{
Scheme: "https",
Host: domain,
})
for _, cookie := range cookies {
if cookie.Name == name {
return cookie.Value
}
}
return ""
}

// @title isloginWeiboCn
// @description 判断是否登录.weibo.cn,手机版一般用m.weibo.cn
// @auth 星辰
Expand All @@ -83,31 +103,7 @@ func (w *WeiboClient) isloginWeiboCn() bool {
}
defer resp.Body.Close()
ctype := resp.Header.Get("Content-Type")
if ctype != "application/json; charset=utf-8" {
return false
}
data, err := w.apiConfig()
if err != nil {
return false
}
var ok bool
w.st, ok = data["data"].(map[string]interface{})["st"].(string)
if ok && w.st != "" {
/*w.client.Jar.SetCookies(
&url.URL{
Scheme: "https",
Host: ".m.weibo.cn",
},
[]*http.Cookie{
&http.Cookie{
Name: "XSRF-TOKEN",
Value: w.st,
Domain: ".m.weibo.cn",
},
})*/
return true
}
return false
return ctype == "application/json; charset=utf-8"
}

// @title loginWeiboCn
Expand Down

0 comments on commit 86260fc

Please sign in to comment.