优化群发休眠逻辑
This commit is contained in:
parent
2acac1db49
commit
55cf87e294
68
parser.go
68
parser.go
@ -1,46 +1,54 @@
|
|||||||
package openwechat
|
package openwechat
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ToBuffer(v interface{}) (*bytes.Buffer, error) {
|
func ToBuffer(v interface{}) (*bytes.Buffer, error) {
|
||||||
buf, err := json.Marshal(v)
|
buf, err := json.Marshal(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return bytes.NewBuffer(buf), nil
|
return bytes.NewBuffer(buf), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetRandomDeviceId() string {
|
func GetRandomDeviceId() string {
|
||||||
rand.Seed(time.Now().Unix())
|
rand.Seed(time.Now().Unix())
|
||||||
var builder strings.Builder
|
var builder strings.Builder
|
||||||
builder.WriteString("e")
|
builder.WriteString("e")
|
||||||
for i := 0; i < 15; i++ {
|
for i := 0; i < 15; i++ {
|
||||||
r := rand.Intn(9)
|
r := rand.Intn(9)
|
||||||
builder.WriteString(strconv.Itoa(r))
|
builder.WriteString(strconv.Itoa(r))
|
||||||
}
|
}
|
||||||
return builder.String()
|
return builder.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func getWebWxDataTicket(cookies []*http.Cookie) string {
|
func getWebWxDataTicket(cookies []*http.Cookie) string {
|
||||||
for _, cookie := range cookies {
|
for _, cookie := range cookies {
|
||||||
if cookie.Name == "webwx_data_ticket" {
|
if cookie.Name == "webwx_data_ticket" {
|
||||||
return cookie.Value
|
return cookie.Value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func XmlFormString(text string) string {
|
func XmlFormString(text string) string {
|
||||||
lt := strings.ReplaceAll(text, "<", "<")
|
lt := strings.ReplaceAll(text, "<", "<")
|
||||||
gt := strings.ReplaceAll(lt, ">", ">")
|
gt := strings.ReplaceAll(lt, ">", ">")
|
||||||
br := strings.ReplaceAll(gt, "<br/>", "\n")
|
br := strings.ReplaceAll(gt, "<br/>", "\n")
|
||||||
return strings.ReplaceAll(br, "&amp;", "&")
|
return strings.ReplaceAll(br, "&amp;", "&")
|
||||||
|
}
|
||||||
|
|
||||||
|
func getTotalDuration(delay ...time.Duration) time.Duration {
|
||||||
|
var total time.Duration
|
||||||
|
for _, d := range delay {
|
||||||
|
total += d
|
||||||
|
}
|
||||||
|
return total
|
||||||
}
|
}
|
||||||
|
30
relations.go
30
relations.go
@ -104,10 +104,9 @@ func (f Friends) Search(limit int, condFuncList ...func(friend *Friend) bool) (r
|
|||||||
|
|
||||||
// 向slice的好友依次发送消息
|
// 向slice的好友依次发送消息
|
||||||
func (f Friends) SendMsg(msg *SendMessage, delay ...time.Duration) error {
|
func (f Friends) SendMsg(msg *SendMessage, delay ...time.Duration) error {
|
||||||
|
total := getTotalDuration(delay...)
|
||||||
for _, friend := range f {
|
for _, friend := range f {
|
||||||
if len(delay) != 0 {
|
time.Sleep(total)
|
||||||
time.Sleep(delay[0])
|
|
||||||
}
|
|
||||||
if err := friend.SendMsg(msg); err != nil {
|
if err := friend.SendMsg(msg); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -117,10 +116,9 @@ func (f Friends) SendMsg(msg *SendMessage, delay ...time.Duration) error {
|
|||||||
|
|
||||||
// 向slice的好友依次发送文本消息
|
// 向slice的好友依次发送文本消息
|
||||||
func (f Friends) SendText(text string, delay ...time.Duration) error {
|
func (f Friends) SendText(text string, delay ...time.Duration) error {
|
||||||
|
total := getTotalDuration(delay...)
|
||||||
for _, friend := range f {
|
for _, friend := range f {
|
||||||
if len(delay) != 0 {
|
time.Sleep(total)
|
||||||
time.Sleep(delay[0])
|
|
||||||
}
|
|
||||||
if err := friend.SendText(text); err != nil {
|
if err := friend.SendText(text); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -130,10 +128,9 @@ func (f Friends) SendText(text string, delay ...time.Duration) error {
|
|||||||
|
|
||||||
// 向slice的好友依次发送图片消息
|
// 向slice的好友依次发送图片消息
|
||||||
func (f Friends) SendImage(file *os.File, delay ...time.Duration) error {
|
func (f Friends) SendImage(file *os.File, delay ...time.Duration) error {
|
||||||
|
total := getTotalDuration(delay...)
|
||||||
for _, friend := range f {
|
for _, friend := range f {
|
||||||
if len(delay) != 0 {
|
time.Sleep(total)
|
||||||
time.Sleep(delay[0])
|
|
||||||
}
|
|
||||||
if err := friend.SendImage(file); err != nil {
|
if err := friend.SendImage(file); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -209,10 +206,7 @@ func (g Groups) Last() *Group {
|
|||||||
|
|
||||||
// 向群组依次发送消息, 支持发送延迟
|
// 向群组依次发送消息, 支持发送延迟
|
||||||
func (g Groups) SendMsg(msg *SendMessage, delay ...time.Duration) error {
|
func (g Groups) SendMsg(msg *SendMessage, delay ...time.Duration) error {
|
||||||
var total time.Duration
|
total := getTotalDuration(delay...)
|
||||||
for _, d := range delay {
|
|
||||||
total += d
|
|
||||||
}
|
|
||||||
for _, group := range g {
|
for _, group := range g {
|
||||||
time.Sleep(total)
|
time.Sleep(total)
|
||||||
if err := group.SendMsg(msg); err != nil {
|
if err := group.SendMsg(msg); err != nil {
|
||||||
@ -224,10 +218,7 @@ func (g Groups) SendMsg(msg *SendMessage, delay ...time.Duration) error {
|
|||||||
|
|
||||||
// 向群组依次发送文本消息, 支持发送延迟
|
// 向群组依次发送文本消息, 支持发送延迟
|
||||||
func (g Groups) SendText(text string, delay ...time.Duration) error {
|
func (g Groups) SendText(text string, delay ...time.Duration) error {
|
||||||
var total time.Duration
|
total := getTotalDuration(delay...)
|
||||||
for _, d := range delay {
|
|
||||||
total += d
|
|
||||||
}
|
|
||||||
for _, group := range g {
|
for _, group := range g {
|
||||||
time.Sleep(total)
|
time.Sleep(total)
|
||||||
if err := group.SendText(text); err != nil {
|
if err := group.SendText(text); err != nil {
|
||||||
@ -239,10 +230,7 @@ func (g Groups) SendText(text string, delay ...time.Duration) error {
|
|||||||
|
|
||||||
// 向群组依次发送图片消息, 支持发送延迟
|
// 向群组依次发送图片消息, 支持发送延迟
|
||||||
func (g Groups) SendImage(file *os.File, delay ...time.Duration) error {
|
func (g Groups) SendImage(file *os.File, delay ...time.Duration) error {
|
||||||
var total time.Duration
|
total := getTotalDuration(delay...)
|
||||||
for _, d := range delay {
|
|
||||||
total += d
|
|
||||||
}
|
|
||||||
for _, group := range g {
|
for _, group := range g {
|
||||||
time.Sleep(total)
|
time.Sleep(total)
|
||||||
if err := group.SendImage(file); err != nil {
|
if err := group.SendImage(file); err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user