diff --git a/download_test.go b/download_test.go index b1d1e56..71c0ecc 100644 --- a/download_test.go +++ b/download_test.go @@ -1,7 +1,6 @@ package main import ( - "log" "testing" ) @@ -10,8 +9,3 @@ func TestReadVideoURL(t *testing.T) { expected := "http://153.37.232.148/vweixinp.tc.qq.com/1007_baa70feeaffb450682d848707e2d38d9.f10.mp4?vkey=6027E95AC6BADE010F39C1C08FA1E8E264C4702D52BE4B04C6BAF89A0857AB371971463C6C1D5D4779E1B748D1BC21041EC5AFBCD41BE4E7D0295A3DA96254505E8ACBDF1C427FD2FC92D5AB0CA9BFD16C0E02D99675DED4&sha=0&save=1" AssertEqual(t, u, expected, "URL should be equal to "+expected) } -func TestGetMediaList(t *testing.T) { - SetupConfig() - l := getNewList() - log.Printf("file is : %s", l) -} diff --git a/material.go b/material.go index 039e71a..fff155f 100644 --- a/material.go +++ b/material.go @@ -2,19 +2,20 @@ package main import ( "bytes" + "encoding/json" "fmt" "io/ioutil" "log" "net/http" ) -type mediaQuery struct { +type materialQuery struct { Type string `json:"type"` - Offiset string `json:"offset"` + Offiset int `json:"offset"` Count int `json:"count"` } -type mediaCount struct { +type materialCount struct { VoiceCount int64 `json:"voice_count"` VideoCount int64 `json:"video_count"` ImageCount int64 `json:"image_count"` @@ -27,8 +28,9 @@ func getNewList() (jstr string) { } func getMediaList(mediaType string) (jstr string) { - var jsonStr = []byte(`{"type":mediaType, "offset":0, "count":20}`) - u := url4NewsList() + var q = materialQuery{mediaType, 0, 20} + jsonStr, _ := json.Marshal(q) + u := url4MaterialList() req, err := http.NewRequest("POST", u, bytes.NewBuffer(jsonStr)) log.Println(u) if err != nil { @@ -57,3 +59,16 @@ func url4MaterialCount() string { u := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token=%s", atk) return u } + +func getMaterialCount() (materialCount, error) { + mc := materialCount{} + url := url4MaterialCount() + r, err := http.Get(url) + if err != nil { + return mc, err + } + defer r.Body.Close() + b, _ := ioutil.ReadAll(r.Body) + err = json.Unmarshal(b, &mc) + return mc, nil +} diff --git a/material_test.go b/material_test.go new file mode 100644 index 0000000..8a644c4 --- /dev/null +++ b/material_test.go @@ -0,0 +1,32 @@ +package main + +import ( + "log" + "testing" +) + +func TestGetMaterialList(t *testing.T) { + SetupConfig() + l := getNewList() + + log.Printf("file is : %s", l) + + // l = getVoiceList() + // log.Printf("file is : %s", l) + // l = getVideoList() + // log.Printf("file is : %s", l) + // l = getImageList() + // log.Printf("file is : %s", l) +} + +func TestGetMaterialCount(t *testing.T) { + SetupConfig() + mc, err := getMaterialCount() + if err != nil { + log.Fatal(err) + } + log.Printf("Voice: %d", mc.VoiceCount) + log.Printf("Video: %d", mc.VideoCount) + log.Printf("Image: %d", mc.ImageCount) + log.Printf("News : %d", mc.NewsCount) +}