Browse Source

替换ini配置文件读取库为goconfig

main
ztino 4 years ago
parent
commit
fa3977881f
  1. 100
      conf/main.go
  2. 2
      go.mod
  3. 4
      go.sum
  4. 64
      jd_seckill/seckill.go
  5. 24
      jd_seckill/user.go
  6. 13
      main.go
  7. 12
      service/email.go

100
conf/main.go

@ -1,100 +0,0 @@
package conf
import (
"bufio"
"io"
"os"
"strings"
)
const middle = "========="
type Config struct {
Mymap map[string]string
strcet string
}
func (c *Config) InitConfig(path string) {
c.Mymap = make(map[string]string)
f, err := os.Open(path)
if err != nil {
panic(err)
}
defer f.Close()
r := bufio.NewReader(f)
for {
b, _, err := r.ReadLine()
if err != nil {
if err == io.EOF {
break
}
panic(err)
}
s := strings.TrimSpace(string(b))
//fmt.Println(s)
if strings.Index(s, "#") == 0 {
continue
}
n1 := strings.Index(s, "[")
n2 := strings.LastIndex(s, "]")
if n1 > -1 && n2 > -1 && n2 > n1+1 {
c.strcet = strings.TrimSpace(s[n1+1 : n2])
continue
}
if len(c.strcet) == 0 {
continue
}
index := strings.Index(s, "=")
if index < 0 {
continue
}
frist := strings.TrimSpace(s[:index])
if len(frist) == 0 {
continue
}
second := strings.TrimSpace(s[index+1:])
pos := strings.Index(second, "\t#")
if pos > -1 {
second = second[0:pos]
}
pos = strings.Index(second, " #")
if pos > -1 {
second = second[0:pos]
}
pos = strings.Index(second, "\t//")
if pos > -1 {
second = second[0:pos]
}
pos = strings.Index(second, " //")
if pos > -1 {
second = second[0:pos]
}
if len(second) == 0 {
continue
}
key := c.strcet + middle + frist
c.Mymap[key] = strings.TrimSpace(second)
}
}
func (c Config) Read(node, key string) string {
key = node + middle + key
v, found := c.Mymap[key]
if !found {
return ""
}
return v
}

2
go.mod

@ -6,7 +6,9 @@ require (
github.com/Albert-Zhan/httpc v0.0.0-20190712132051-aed72924b5e6
github.com/PuerkitoBio/goquery v1.6.0
github.com/tidwall/gjson v1.6.7
github.com/unknwon/goconfig v0.0.0-20200908083735-df7de6a44db8
golang.org/x/text v0.3.4
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
)

4
go.sum

@ -14,8 +14,12 @@ github.com/tidwall/match v1.0.3 h1:FQUVvBImDutD8wJLN6c5eMzWtjgONK9MwIBCOrUJKeE=
github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.0.2 h1:Z7S3cePv9Jwm1KwS0513MRaoUe3S01WPbLNV40pwWZU=
github.com/tidwall/pretty v1.0.2/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/unknwon/goconfig v0.0.0-20200908083735-df7de6a44db8 h1:b/rWs6xu47ewpFN3BZDJ5ppuaPC/yObRC0WJFIlQUZk=
github.com/unknwon/goconfig v0.0.0-20200908083735-df7de6a44db8/go.mod h1:qu2ZQ/wcC/if2u32263HTVC39PeOQRSmidQk3DuDFQ8=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk=
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE=
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw=

64
jd_seckill/seckill.go

@ -6,8 +6,8 @@ import (
"github.com/Albert-Zhan/httpc"
"github.com/PuerkitoBio/goquery"
"github.com/tidwall/gjson"
"github.com/unknwon/goconfig"
"github.com/ztino/jd_seckill/common"
"github.com/ztino/jd_seckill/conf"
"github.com/ztino/jd_seckill/service"
"log"
"net/http"
@ -18,15 +18,19 @@ import (
type Seckill struct {
client *httpc.HttpClient
conf *conf.Config
conf *goconfig.ConfigFile
}
func NewSeckill(client *httpc.HttpClient,conf *conf.Config) *Seckill {
func NewSeckill(client *httpc.HttpClient,conf *goconfig.ConfigFile) *Seckill {
return &Seckill{client: client,conf: conf}
}
func (this *Seckill) getUserAgent() string {
return this.conf.MustValue("config","default_user_agent","")
}
func (this *Seckill) SkuTitle() (string,error) {
skuId:=this.conf.Read("config","sku_id")
skuId:=this.conf.MustValue("config","sku_id","")
req:=httpc.NewRequest(this.client)
resp,body,err:=req.SetUrl(fmt.Sprintf("https://item.jd.com/%s.html",skuId)).SetMethod("get").Send().End()
if err!=nil || resp.StatusCode!=http.StatusOK {
@ -45,9 +49,9 @@ func (this *Seckill) MakeReserve() {
}else{
log.Println("商品名称:"+shopTitle)
}
skuId:=this.conf.Read("config","sku_id")
skuId:=this.conf.MustValue("config","sku_id","")
req:=httpc.NewRequest(this.client)
req.SetHeader("User-Agent",this.conf.Read("config","default_user_agent"))
req.SetHeader("User-Agent",this.getUserAgent())
req.SetHeader("Referer",fmt.Sprintf("https://item.jd.com/%s.html",skuId))
resp,body,err:=req.SetUrl("https://yushou.jd.com/youshouinfo.action?callback=fetchJSON&sku="+skuId+"&_="+strconv.Itoa(int(time.Now().Unix()*1000))).SetMethod("get").Send().End()
if err!=nil || resp.StatusCode!=http.StatusOK {
@ -61,9 +65,9 @@ func (this *Seckill) MakeReserve() {
}
func (this *Seckill) getSeckillUrl() (string,error) {
skuId:=this.conf.Read("config","sku_id")
skuId:=this.conf.MustValue("config","sku_id","")
req:=httpc.NewRequest(this.client)
req.SetHeader("User-Agent",this.conf.Read("config","default_user_agent"))
req.SetHeader("User-Agent",this.getUserAgent())
req.SetHeader("Host","itemko.jd.com")
req.SetHeader("Referer",fmt.Sprintf("https://item.jd.com/%s.html",skuId))
url:=""
@ -95,9 +99,9 @@ func (this *Seckill) RequestSeckillUrl() {
log.Println("商品名称:"+shopTitle)
}
url,_:=this.getSeckillUrl()
skuId:=this.conf.Read("config","sku_id")
skuId:=this.conf.MustValue("config","sku_id","")
req:=httpc.NewRequest(this.client)
req.SetHeader("User-Agent",this.conf.Read("config","default_user_agent"))
req.SetHeader("User-Agent",this.getUserAgent())
req.SetHeader("Host","marathon.jd.com")
req.SetHeader("Referer",fmt.Sprintf("https://item.jd.com/%s.html",skuId))
_,_,_=req.SetUrl(url).SetMethod("get").Send().End()
@ -105,10 +109,10 @@ func (this *Seckill) RequestSeckillUrl() {
func (this *Seckill) SeckillPage() {
log.Println("访问抢购订单结算页面...")
skuId:=this.conf.Read("config","sku_id")
seckillNum:=this.conf.Read("config","seckill_num")
skuId:=this.conf.MustValue("config","sku_id","")
seckillNum:=this.conf.MustValue("config","seckill_num","")
req:=httpc.NewRequest(this.client)
req.SetHeader("User-Agent",this.conf.Read("config","default_user_agent"))
req.SetHeader("User-Agent",this.getUserAgent())
req.SetHeader("Host","marathon.jd.com")
req.SetHeader("Referer",fmt.Sprintf("https://item.jd.com/%s.html",skuId))
_,_,_=req.SetUrl("https://marathon.jd.com/seckill/seckill.action?skuId="+skuId+"&num="+seckillNum+"&rid="+strconv.Itoa(int(time.Now().Unix()))).SetMethod("get").Send().End()
@ -116,10 +120,10 @@ func (this *Seckill) SeckillPage() {
func (this *Seckill) SeckillInitInfo() (string,error) {
log.Println("获取秒杀初始化信息...")
skuId:=this.conf.Read("config","sku_id")
seckillNum:=this.conf.Read("config","seckill_num")
skuId:=this.conf.MustValue("config","sku_id","")
seckillNum:=this.conf.MustValue("config","seckill_num","")
req:=httpc.NewRequest(this.client)
req.SetHeader("User-Agent",this.conf.Read("config","default_user_agent"))
req.SetHeader("User-Agent",this.getUserAgent())
req.SetHeader("Host","marathon.jd.com")
req.SetData("sku",skuId)
req.SetData("num",seckillNum)
@ -137,11 +141,11 @@ func (this *Seckill) SeckillInitInfo() (string,error) {
}
func (this *Seckill) SubmitSeckillOrder() bool {
eid:=this.conf.Read("config","eid")
fp:=this.conf.Read("config","fp")
skuId:=this.conf.Read("config","sku_id")
seckillNum:=this.conf.Read("config","seckill_num")
paymentPwd:=this.conf.Read("account","payment_pwd")
eid:=this.conf.MustValue("config","eid","")
fp:=this.conf.MustValue("config","fp","")
skuId:=this.conf.MustValue("config","sku_id","")
seckillNum:=this.conf.MustValue("config","seckill_num","")
paymentPwd:=this.conf.MustValue("account","payment_pwd","")
initInfo,err:=this.SeckillInitInfo()
if err!=nil {
log.Println(fmt.Sprintf("抢购失败,无法获取生成订单的基本信息,接口返回:【%s】",err))
@ -167,7 +171,7 @@ func (this *Seckill) SubmitSeckillOrder() bool {
token:=gjson.Get(initInfo,"token").String()
log.Println("提交抢购订单...")
req:=httpc.NewRequest(this.client)
req.SetHeader("User-Agent",this.conf.Read("config","default_user_agent"))
req.SetHeader("User-Agent",this.getUserAgent())
req.SetHeader("Host","marathon.jd.com")
req.SetHeader("Referer",fmt.Sprintf("https://marathon.jd.com/seckill/seckill.action?skuId=%s&num=%s&rid=%d",skuId,seckillNum,int(time.Now().Unix())))
req.SetData("skuId",skuId)
@ -206,17 +210,17 @@ func (this *Seckill) SubmitSeckillOrder() bool {
resp,body,err:=req.SetUrl("https://marathon.jd.com/seckillnew/orderService/pc/submitOrder.action?skuId="+skuId).SetMethod("post").Send().End()
if err!=nil || resp.StatusCode!=http.StatusOK {
log.Println("抢购失败,网络错误")
if this.conf.Read("messenger","enable")=="true" && this.conf.Read("messenger","type")=="smtp" {
if this.conf.MustValue("messenger","enable","false")=="true" && this.conf.MustValue("messenger","type","none")=="smtp" {
email:=service.NerEmail(this.conf)
_=email.SendMail([]string{this.conf.Read("messenger","email")},"茅台抢购通知","抢购失败,网络错误")
_=email.SendMail([]string{this.conf.MustValue("messenger","email","")},"茅台抢购通知","抢购失败,网络错误")
}
return false
}
if !gjson.Valid(body) {
log.Println("抢购失败,返回信息:"+body)
if this.conf.Read("messenger","enable")=="true" && this.conf.Read("messenger","type")=="smtp" {
if this.conf.MustValue("messenger","enable","false")=="true" && this.conf.MustValue("messenger","type","none")=="smtp" {
email:=service.NerEmail(this.conf)
_=email.SendMail([]string{this.conf.Read("messenger","email")},"茅台抢购通知","抢购失败,返回信息:"+body)
_=email.SendMail([]string{this.conf.MustValue("messenger","email","")},"茅台抢购通知","抢购失败,返回信息:"+body)
}
return false
}
@ -225,16 +229,16 @@ func (this *Seckill) SubmitSeckillOrder() bool {
totalMoney:=gjson.Get(body,"totalMoney").String()
payUrl:="https:"+gjson.Get(body,"pcUrl").String()
log.Println(fmt.Sprintf("抢购成功,订单号:%s, 总价:%s, 电脑端付款链接:%s",orderId,totalMoney,payUrl))
if this.conf.Read("messenger","enable")=="true" && this.conf.Read("messenger","type")=="smtp" {
if this.conf.MustValue("messenger","enable","false")=="true" && this.conf.MustValue("messenger","type","none")=="smtp" {
email:=service.NerEmail(this.conf)
_=email.SendMail([]string{this.conf.Read("messenger","email")},"茅台抢购通知",fmt.Sprintf("抢购成功,订单号:%s, 总价:%s, 电脑端付款链接:%s",orderId,totalMoney,payUrl))
_=email.SendMail([]string{this.conf.MustValue("messenger","email","")},"茅台抢购通知",fmt.Sprintf("抢购成功,订单号:%s, 总价:%s, 电脑端付款链接:%s",orderId,totalMoney,payUrl))
}
return true
}else{
log.Println("抢购失败,返回信息:"+body)
if this.conf.Read("messenger","enable")=="true" && this.conf.Read("messenger","type")=="smtp" {
if this.conf.MustValue("messenger","enable","false")=="true" && this.conf.MustValue("messenger","type","none")=="smtp" {
email:=service.NerEmail(this.conf)
_=email.SendMail([]string{this.conf.Read("messenger","email")},"茅台抢购通知","抢购失败,返回信息:"+body)
_=email.SendMail([]string{this.conf.MustValue("messenger","email","")},"茅台抢购通知","抢购失败,返回信息:"+body)
}
return false
}

24
jd_seckill/user.go

@ -1,12 +1,12 @@
package jd_seckill
import (
"../common"
"../conf"
"errors"
"fmt"
"github.com/Albert-Zhan/httpc"
"github.com/tidwall/gjson"
"github.com/unknwon/goconfig"
"github.com/ztino/jd_seckill/common"
"log"
"net/http"
"os"
@ -16,16 +16,20 @@ import (
type User struct {
client *httpc.HttpClient
conf *conf.Config
conf *goconfig.ConfigFile
}
func NewUser(client *httpc.HttpClient,conf *conf.Config) *User {
func NewUser(client *httpc.HttpClient,conf *goconfig.ConfigFile) *User {
return &User{client: client,conf:conf }
}
func (this *User) getUserAgent() string {
return this.conf.MustValue("config","default_user_agent","")
}
func (this *User) loginPage() {
req:=httpc.NewRequest(this.client)
req.SetHeader("User-Agent",this.conf.Read("config","default_user_agent"))
req.SetHeader("User-Agent",this.getUserAgent())
req.SetHeader("Connection","keep-alive")
req.SetHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3")
_,_,_=req.SetUrl("https://passport.jd.com/new/login.aspx").SetMethod("get").Send().End()
@ -36,7 +40,7 @@ func (this *User) QrLogin() (string,error) {
this.loginPage()
//二维码登录
req:=httpc.NewRequest(this.client)
req.SetHeader("User-Agent",this.conf.Read("config","default_user_agent"))
req.SetHeader("User-Agent",this.getUserAgent())
req.SetHeader("Referer","https://passport.jd.com/new/login.aspx")
resp,err:=req.SetUrl("https://qr.m.jd.com/show?appid=133&size=300&t="+strconv.Itoa(int(time.Now().Unix()*1000))).SetMethod("get").Send().EndFile("./","qr_code.png")
if err!=nil || resp.StatusCode!=http.StatusOK {
@ -59,7 +63,7 @@ func (this *User) QrLogin() (string,error) {
func (this *User) QrcodeTicket(wlfstkSmdl string) (string,error) {
req:=httpc.NewRequest(this.client)
req.SetHeader("User-Agent",this.conf.Read("config","default_user_agent"))
req.SetHeader("User-Agent",this.getUserAgent())
req.SetHeader("Referer","https://passport.jd.com/new/login.aspx")
resp,body,err:=req.SetUrl("https://qr.m.jd.com/check?appid=133&callback=jQuery"+strconv.Itoa(common.Rand(1000000,9999999))+"&token="+wlfstkSmdl+"&_="+strconv.Itoa(int(time.Now().Unix()*1000))).SetMethod("get").Send().End()
if err!=nil || resp.StatusCode!=http.StatusOK {
@ -76,7 +80,7 @@ func (this *User) QrcodeTicket(wlfstkSmdl string) (string,error) {
func (this *User) TicketInfo(ticket string) (string,error) {
req:=httpc.NewRequest(this.client)
req.SetHeader("User-Agent",this.conf.Read("config","default_user_agent"))
req.SetHeader("User-Agent",this.getUserAgent())
req.SetHeader("Referer","https://passport.jd.com/uc/login?ltype=logout")
resp,body,err:=req.SetUrl("https://passport.jd.com/uc/qrCodeTicketValidation?t="+ticket).SetMethod("get").Send().End()
if err!=nil || resp.StatusCode!=http.StatusOK {
@ -94,7 +98,7 @@ func (this *User) TicketInfo(ticket string) (string,error) {
func (this *User) RefreshStatus() error {
req:=httpc.NewRequest(this.client)
req.SetHeader("User-Agent",this.conf.Read("config","default_user_agent"))
req.SetHeader("User-Agent",this.getUserAgent())
resp,_,err:=req.SetUrl("https://order.jd.com/center/list.action?rid="+strconv.Itoa(int(time.Now().Unix()*1000))).SetMethod("get").Send().End()
if err==nil && resp.StatusCode==http.StatusOK {
return nil
@ -105,7 +109,7 @@ func (this *User) RefreshStatus() error {
func (this *User) GetUserInfo() (string,error) {
req:=httpc.NewRequest(this.client)
req.SetHeader("User-Agent",this.conf.Read("config","default_user_agent"))
req.SetHeader("User-Agent",this.getUserAgent())
req.SetHeader("Referer","https://order.jd.com/center/list.action")
errorCount:=5
nickName:=""

13
main.go

@ -5,8 +5,7 @@ import (
"fmt"
"github.com/Albert-Zhan/httpc"
"github.com/tidwall/gjson"
"github.com/ztino/jd_seckill/common"
"github.com/ztino/jd_seckill/conf"
"github.com/unknwon/goconfig"
"github.com/ztino/jd_seckill/jd_seckill"
"log"
"net/http"
@ -20,7 +19,7 @@ var client *httpc.HttpClient
var cookieJar *httpc.CookieJar
var config *conf.Config
var config *goconfig.ConfigFile
var wg *sync.WaitGroup
@ -32,14 +31,14 @@ func init() {
client.SetRedirect(func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
})
//配置文件初始化
confFile:="./conf.ini"
if !common.Exists(confFile) {
var err error
if config,err=goconfig.LoadConfigFile(confFile);err!=nil {
log.Println("配置文件不存在,程序退出")
os.Exit(0)
}
config=&conf.Config{}
config.InitConfig(confFile)
wg=new(sync.WaitGroup)
wg.Add(1)
@ -75,7 +74,7 @@ func main() {
//等待抢购/开始抢购
nowLocalTime:=time.Now().UnixNano()/1e6
jdTime,_:=getJdTime()
buyDate:=config.Read("config","buy_time")
buyDate:=config.MustValue("config","buy_time","")
loc, _ := time.LoadLocation("Local")
t,_:=time.ParseInLocation("2006-01-02 15:04:05",buyDate,loc)
buyTime:=t.UnixNano()/1e6

12
service/email.go

@ -1,7 +1,7 @@
package service
import (
"github.com/ztino/jd_seckill/conf"
"github.com/unknwon/goconfig"
"gopkg.in/gomail.v2"
"strconv"
)
@ -13,11 +13,11 @@ type Email struct {
pass string
}
func NerEmail(conf *conf.Config) *Email {
host:=conf.Read("smtp","email_host")
port:=conf.Read("smtp","port")
user:=conf.Read("smtp","email_user")
pass:=conf.Read("smtp","email_pwd")
func NerEmail(conf *goconfig.ConfigFile) *Email {
host:=conf.MustValue("smtp","email_host","")
port:=conf.MustValue("smtp","port","")
user:=conf.MustValue("smtp","email_user","")
pass:=conf.MustValue("smtp","email_pwd","")
return &Email{host: host,port: port,user: user,pass: pass}
}

Loading…
Cancel
Save