From c78fe5411e1e68784173f31333adf02a4ae80ccd Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 22 Apr 2024 17:29:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=95=E4=B8=AA=E5=BA=94=E7=94=A8=E5=B9=BF?= =?UTF-8?q?=E5=91=8A=E8=B4=B9=E6=8A=93=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wx.go | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 wx.go diff --git a/wx.go b/wx.go new file mode 100644 index 0000000..e7c5d1d --- /dev/null +++ b/wx.go @@ -0,0 +1,140 @@ +package main +import ( + "database/sql" + "encoding/json" + "fmt" + _ "github.com/go-sql-driver/mysql" + "io/ioutil" + "log" + "net/http" + "strings" + "time" +) +type AdDataAll struct { + BaseResp struct { + ErrMsg string `json:"err_msg"` + Ret int `json:"ret"` + } `json:"base_resp"` + List []struct { + AdSlot string `json:"ad_slot"` + ClickCount int `json:"click_count"` + ClickRate float64 `json:"click_rate"` + Date string `json:"date"` + Ecpm float64 `json:"ecpm"` + ExposureCount int `json:"exposure_count"` + ExposureRate float64 `json:"exposure_rate"` + Income int `json:"income"` + ReqSuccCount int `json:"req_succ_count"` + SlotID int64 `json:"slot_id"` + SlotStr string `json:"slot_str"` + } `json:"list"` + Summary struct { + ClickCount int `json:"click_count"` + ClickRate float64 `json:"click_rate"` + Ecpm float64 `json:"ecpm"` + ExposureCount int `json:"exposure_count"` + ExposureRate float64 `json:"exposure_rate"` + Income int `json:"income"` + ReqSuccCount int `json:"req_succ_count"` + } `json:"summary"` + TotalNum int `json:"total_num"` +} + +func main() { + appID := "wx3742274d720f7aa2" + appSecret := "99898fe4b9e05bea29466933568ad8cc" + // 获取Access Token + tokenURL := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", appID, appSecret) + resp, err := http.Get(tokenURL) + if err != nil { + fmt.Println("Failed to get access token.") + return + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + fmt.Println("Failed to read response body.") + return + } + + var result map[string]interface{} + if err := json.Unmarshal(body, &result); err != nil { + fmt.Println("Failed to parse access token response.") + return + } + accessToken := result["access_token"].(string) + //---注意替换日期 + adURL := fmt.Sprintf("https://api.weixin.qq.com/publisher/stat?action=publisher_adpos_general&access_token=%s&start_date=2024-04-01&end_date=2024-04-22&page_size=30&page=1",accessToken) + adResp, err := http.Get(adURL) + if err != nil { + fmt.Println("Failed to get ad data.") + return + } + defer adResp.Body.Close() + con, err := ioutil.ReadAll(adResp.Body) + if err != nil { + fmt.Println("Failed to read ad data response body.") + return + } + var adData AdDataAll + err = json.Unmarshal(con, &adData) + if err != nil { + fmt.Println("Error JSON:", err) + return + } + // 连接数据库 + db, err := sql.Open("mysql", "root:123456@tcp(localhost:3306)/wxcj") + if err != nil { + log.Fatal(err) + } + defer db.Close() + + err = db.Ping() + if err != nil { + log.Fatal(err) + } + for _,v:=range adData.List{ + appId:=1111 //--切换成本地的app_id + date:=strings.Replace(v.Date,"-","",-1) + adSlot:=v.AdSlot + slotId:=v.SlotID + income:=v.Income + reqSuccCount:=v.ReqSuccCount + exposureCount:=v.ExposureCount + clickCount:=v.ClickCount + ecpm:=v.Ecpm + updatedAt:=time.Now().Unix() + // 查询某个应用某天某个广告位是否存在 + var id int + row := db.QueryRow("SELECT id FROM wx_app_advertisement WHERE app_id = ? and date=? and ad_slot=?", appId,date,adSlot) + err = row.Scan(&id) + if err != nil { + if err!=sql.ErrNoRows{ + continue + } + } + if id>0{ + fmt.Printf("appid=%d,日期=%s,广告位=%s 已存在 \n",appId,date,adSlot) + continue + } + // 插入数据到数据库 + stmt, err := db.Prepare("INSERT INTO wx_app_advertisement(`app_id`,`date`,`ad_slot`,`slot_id`,`income`,`req_succ_count`,`exposure_count`,`click_count`,`ecpm`,`updated_at`) VALUES(?,?,?,?,?,?,?,?,?,?)") + if err != nil { + log.Fatal(err) + } + defer stmt.Close() + + res, err:= stmt.Exec(appId, date,adSlot,slotId,income,reqSuccCount,exposureCount,clickCount,ecpm,updatedAt) + if err != nil { + log.Fatal(err) + continue + } + // 获取插入ID + newId, err := res.LastInsertId() + if err != nil { + log.Fatal(err) + continue + } + fmt.Printf("Inserted row ID: %d\n", newId) + } +}