102 lines
2.7 KiB
Go
102 lines
2.7 KiB
Go
|
package api
|
|||
|
|
|||
|
//爬虫的参数组装,返回结构体解析,存入数据库的方法有关的包
|
|||
|
import (
|
|||
|
"crypto/md5"
|
|||
|
"encoding/hex"
|
|||
|
"encoding/json"
|
|||
|
"errors"
|
|||
|
ctxLogger "github.com/luizsuper/ctxLoggers"
|
|||
|
"github.com/spf13/cast"
|
|||
|
"go.uber.org/zap"
|
|||
|
"gorm.io/driver/mysql"
|
|||
|
"gorm.io/gorm"
|
|||
|
"io"
|
|||
|
"io/ioutil"
|
|||
|
"net/http"
|
|||
|
"strings"
|
|||
|
"time"
|
|||
|
)
|
|||
|
|
|||
|
//针对http访问,checkSum计算的抽象
|
|||
|
var (
|
|||
|
M = make(map[string]string)
|
|||
|
DB = new(gorm.DB)
|
|||
|
)
|
|||
|
|
|||
|
const o = `$&*#$%YSGHsghdfg256456857ughdgfEYTHFDBNF5678hncg~!@#$%&$&Uxvcbdfget2577ifhnxgfart$%^&@#$%@%&ghsdft256WRETsgsr623$%TStw45`
|
|||
|
|
|||
|
//todo:配置文件
|
|||
|
//初始化DB,以及请求的
|
|||
|
func init() {
|
|||
|
err := errors.New("")
|
|||
|
dsn := "root:123456qwe@tcp(127.0.0.1:3306)/mijuan?charset=utf8mb4&parseTime=True&loc=Local"
|
|||
|
DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
|
|||
|
if err != nil {
|
|||
|
ctxLogger.Error(nil, "DB connect err", zap.String("reason", err.Error()))
|
|||
|
}
|
|||
|
M["Content-Type"] = "application/json"
|
|||
|
M["version"] = "1.1.9"
|
|||
|
//todo: nonce 提取出来
|
|||
|
M["nonce"] = "0.5794467510454496"
|
|||
|
M["curTime"] = cast.ToString(TimeToUnix(time.Now()))
|
|||
|
M["clientVersion"] = "3.0.0"
|
|||
|
M["userToken"] = ""
|
|||
|
M["clientType"] = "2"
|
|||
|
M["cityCode"] = "310000"
|
|||
|
M["mobileType"] = "2"
|
|||
|
}
|
|||
|
|
|||
|
// TimeToUnix 转化为13位时间戳
|
|||
|
func TimeToUnix(e time.Time) int64 {
|
|||
|
timeUnix, _ := time.Parse("2006-01-02 15:04:05", e.Format("2006-01-02 15:04:05"))
|
|||
|
return timeUnix.UnixNano() / 1e6
|
|||
|
}
|
|||
|
|
|||
|
func GetCheckSum(u string) string {
|
|||
|
a := 0.5794467510454496
|
|||
|
toString := cast.ToString(a)
|
|||
|
s := toString + u + o
|
|||
|
bytes := []byte(s)
|
|||
|
h := md5.New()
|
|||
|
h.Write(bytes)
|
|||
|
k := hex.EncodeToString(h.Sum(nil))
|
|||
|
return k
|
|||
|
}
|
|||
|
|
|||
|
func Attach(url, getCheckSum string, sbody interface{}, headers map[string]string, marshalFunc func(req, body []byte) (interface{}, error)) (interface{}, error) {
|
|||
|
M["checkSum"] = getCheckSum
|
|||
|
body, err := PostHeader(url, sbody, headers, marshalFunc)
|
|||
|
if err != nil {
|
|||
|
return nil, err
|
|||
|
}
|
|||
|
return body, nil
|
|||
|
}
|
|||
|
|
|||
|
func PostHeader(url string, sbody interface{}, headers map[string]string, marshalFunc func(req, body []byte) (resp interface{}, err error)) (interface{}, error) {
|
|||
|
client := &http.Client{}
|
|||
|
marshal, err := json.Marshal(sbody)
|
|||
|
if err != nil {
|
|||
|
return nil, err
|
|||
|
}
|
|||
|
req, err := http.NewRequest("POST", url, strings.NewReader(cast.ToString(marshal)))
|
|||
|
if err != nil {
|
|||
|
return nil, err
|
|||
|
}
|
|||
|
for key, header := range headers {
|
|||
|
req.Header.Set(key, header)
|
|||
|
}
|
|||
|
resp, err := client.Do(req)
|
|||
|
defer func(Body io.ReadCloser) {
|
|||
|
err := Body.Close()
|
|||
|
if err != nil {
|
|||
|
ctxLogger.Error(nil, "net body close error", zap.String("errReason", err.Error()))
|
|||
|
}
|
|||
|
}(resp.Body)
|
|||
|
body, err := ioutil.ReadAll(resp.Body)
|
|||
|
if err != nil {
|
|||
|
return nil, err
|
|||
|
}
|
|||
|
return marshalFunc(marshal, body)
|
|||
|
}
|