diff --git a/material.go b/material.go index 3f238f6..cfca556 100644 --- a/material.go +++ b/material.go @@ -9,12 +9,14 @@ import ( "net/http" ) +//get a list of material type materialQuery struct { Type string `json:"type"` Offiset int `json:"offset"` Count int `json:"count"` } +//overall material count type materialCount struct { VoiceCount int64 `json:"voice_count"` VideoCount int64 `json:"video_count"` @@ -22,6 +24,54 @@ type materialCount struct { NewsCount int64 `json:"news_count"` } +//Material Image Item +type materialItemImage struct { + MediaID string `json:"media_id"` + Name string `json:"name"` + UpdateTime int `json:"update_time"` + URL string `json:"url"` +} + +type materialImageList struct { + TotalCount int `json:"total_count"` + ItemCount int `json:"item_count"` + Items []materialItemImage `json:"item"` +} + +//Material News Item - Article +type materialArticle struct { + Title string `json:"title"` + Author string `json:"author"` + Digest string `json:"digest"` + Content string `json:"content"` + ContentSourceURL string `json:"content_source_url"` + ThumbMediaID string `json:"thumb_media_id"` + ShowCoverPic int `json:"show_cover_pic"` + URL string `json:"url"` + ThumbURL string `json:"thumb_url"` + NeedOpenComment int `json:"need_open_comment"` + OnlyFansCanComment int `json:"only_fans_can_comment"` +} + +type materialNewsItem struct { + MediaID string `json:"media_id"` + UpdateTime int `json:"update_time"` + Content struct { + CreateTime int `json:"create_time"` + UpdateTime int `json:"update_time"` + NewsItem []materialArticle `json:"news_item"` + } `json:"content"` +} + +type materialNewsList struct { + TotalCount int `json:"total_count"` + ItemCount int `json:"item_count"` + Items []materialNewsItem `json:"item"` +} + +// +//-- function definition start +// func getNewList() (jstr string) { jstr = getMediaList("news") return @@ -88,20 +138,12 @@ func getMaterialCount() (materialCount, error) { return mc, nil } -type materialItemImage struct { - MediaID string `json:"media_id"` - Name string `json:"name"` - UpdateTime int `json:"update_time"` - URL string `json:"url"` -} - -type materialListImage struct { - TotalCount int `json:"total_count"` - ItemCount int `json:"item_count"` - Items []materialItemImage `json:"item"` +func unmarshalImageList(jstr string) (r materialImageList) { + json.Unmarshal([]byte(jstr), &r) + return } -func unmarshalImageList(jstr string) (r materialListImage) { +func unmarshalNewsList(jstr string) (r materialNewsList) { json.Unmarshal([]byte(jstr), &r) return } diff --git a/material_test.go b/material_test.go index bc50ccc..bbdf0cc 100644 --- a/material_test.go +++ b/material_test.go @@ -76,3 +76,91 @@ func TestUnmarshalImageList(t *testing.T) { AssertEqual(t, r.Items[2].URL, "http://mmbiz.qpic.cn/mmbiz_png/WiaVAicTdo7zP8HtgLHegmr32SLxcGv0wjQ1pic9oePDgk6XUhDz6xfhicmiaq4ITfibtfPOsulOMD7icIfSicibd8By3Tw/0?wx_fmt=png", "Item[2] URL wrong") } + +func TestUnmarshalNewsList(t *testing.T) { + msg := ` +{ + "item": [ + { + "media_id": "e2iNEiSxCX5TV1WbFd0TQP4_MKtjcY1q-4UPIZFrHhw", + "content": { + "news_item": [ + { + "title": "新人入会前几周推荐", + "author": "孙鹏", + "digest": "摘要", + "content": "

    新人入会,前几周推荐做如下几件事情<\/p>

<\/p>


<\/p>", + "content_source_url": "", + "thumb_media_id": "SCqYVM0F3OSBIqptPWQ5ZjDYlLvKsEtHHaYnHLyXbeM", + "show_cover_pic": 0, + "url": "http:\/\/mp.weixin.qq.com\/s?__biz=MzUxMDAzNDAwMA==&mid=100000008&idx=1&sn=cbfd3f5f5367ca67f9a1184eb394f788&chksm=790868374e7fe121ac5e1d27f6d082c3f8e8033eb4e7158615d0a69dc76ac2971e5650b28fac#rd", + "thumb_url": "http:\/\/mmbiz.qpic.cn\/mmbiz_jpg\/WiaVAicTdo7zMBVOE4QKxFFnFwbnFbyYGUs0YQBiatN97ibIgOWE4picXg49jj1k5TRibswd7DzLC1CCvJco0Kic9T90g\/0?wx_fmt=jpeg", + "need_open_comment": 0, + "only_fans_can_comment": 0 + } + ], + "create_time": 1494488254, + "update_time": 1494488268 + }, + "update_time": 1494488268 + }, + { + "media_id": "e2iNEiSxCX5TV1WbFd0TQMqvVrqFDbDOacdjgQ-OAuE", + "content": { + "news_item": [ + { + "title": "测试消息", + "author": "孙鹏", + "digest": "加入校友会", + "content": "

加入校友会<\/p>

<\/p>


<\/p>", + "content_source_url": "http:\/\/www.hitxy.org.au\/join", + "thumb_media_id": "SCqYVM0F3OSBIqptPWQ5ZjDYlLvKsEtHHaYnHLyXbeM", + "show_cover_pic": 0, + "url": "http:\/\/mp.weixin.qq.com\/s?__biz=MzUxMDAzNDAwMA==&mid=100000002&idx=1&sn=723bf16b52f88345e5a73ccde1c79bdb&chksm=7908683d4e7fe12bcaefa9c563809db5546ab97af7184ec648f20930fab5c1d95ee825409c0d#rd", + "thumb_url": "http:\/\/mmbiz.qpic.cn\/mmbiz_png\/WiaVAicTdo7zP8HtgLHegmr32SLxcGv0wjQ1pic9oePDgk6XUhDz6xfhicmiaq4ITfibtfPOsulOMD7icIfSicibd8By3Tw\/0?wx_fmt=png", + "need_open_comment": 0, + "only_fans_can_comment": 0 + } + ], + "create_time": 1492696196, + "update_time": 1492696243 + }, + "update_time": 1492696243 + } + ], + "total_count": 20, + "item_count": 2 +} + ` + r := unmarshalNewsList(msg) + AssertEqual(t, r.TotalCount, 20, "Total count should be 20") + AssertEqual(t, r.ItemCount, 2, "Item count should be 2") + cnt := len(r.Items) + AssertEqual(t, cnt, 2, "") + + //first item + item := r.Items[0] + AssertEqual(t, item.MediaID, "e2iNEiSxCX5TV1WbFd0TQP4_MKtjcY1q-4UPIZFrHhw", "Item[0] media ID wrong") + //AssertEqual(t, item.CreateTime, 1494488254, "Item[0] create time wrong") + AssertEqual(t, item.UpdateTime, 1494488268, "Item[0] update time wrong") + + //second item + item = r.Items[1] + AssertEqual(t, item.MediaID, "e2iNEiSxCX5TV1WbFd0TQMqvVrqFDbDOacdjgQ-OAuE", "Item[1] media ID wrong") + //AssertEqual(t, item.CreateTime, 1494488254, "Item[1] create time wrong") + AssertEqual(t, item.UpdateTime, 1492696243, "Item[1] update time wrong") + AssertEqual(t, item.Content.CreateTime, 1492696196, "Item[1] update time wrong") + AssertEqual(t, item.Content.UpdateTime, 1492696243, "Item[1] update time wrong") + AssertEqual(t, item.Content.NewsItem[0].Title, "测试消息", "Item[1] update time wrong") + AssertEqual(t, item.Content.NewsItem[0].Author, "孙鹏", "Item[1] update time wrong") + AssertEqual(t, item.Content.NewsItem[0].Digest, "加入校友会", "Item[1] update time wrong") + AssertEqual(t, item.Content.NewsItem[0].Content, "

加入校友会


", "article content wrong") + AssertEqual(t, item.Content.NewsItem[0].ContentSourceURL, "http://www.hitxy.org.au/join", "Item[1] update time wrong") + AssertEqual(t, item.Content.NewsItem[0].ThumbMediaID, "SCqYVM0F3OSBIqptPWQ5ZjDYlLvKsEtHHaYnHLyXbeM", "Item[1] update time wrong") + AssertEqual(t, item.Content.NewsItem[0].ShowCoverPic, 0, "Item[1] update time wrong") + AssertEqual(t, item.Content.NewsItem[0].URL, "http://mp.weixin.qq.com/s?__biz=MzUxMDAzNDAwMA==&mid=100000002&idx=1&sn=723bf16b52f88345e5a73ccde1c79bdb&chksm=7908683d4e7fe12bcaefa9c563809db5546ab97af7184ec648f20930fab5c1d95ee825409c0d#rd", "Article URL is not right") + AssertEqual(t, item.Content.NewsItem[0].ThumbURL, "http://mmbiz.qpic.cn/mmbiz_png/WiaVAicTdo7zP8HtgLHegmr32SLxcGv0wjQ1pic9oePDgk6XUhDz6xfhicmiaq4ITfibtfPOsulOMD7icIfSicibd8By3Tw/0?wx_fmt=png", "Article Thumb URL wrong") + AssertEqual(t, item.Content.NewsItem[0].NeedOpenComment, 0, "Need Open Comment should be 0") + AssertEqual(t, item.Content.NewsItem[0].OnlyFansCanComment, 0, "OnlyFansCanComment should be 0") + +} diff --git a/sample_data/material_list.json b/sample_data/material_list.json index 1260fe9..78f42a1 100644 --- a/sample_data/material_list.json +++ b/sample_data/material_list.json @@ -1,5 +1,5 @@ -2017/05/11 11: 27: 17 https: //api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=RB8viLdrGw91XDjX29crjt9Dz242CWL6QqXQmyYcr2Pb012ZkdzdJR17uLwqjZApTnSVZYfofLWdVMN2wmszwk_1ACT29Xev1PwnKUuZEXpoChl8U-MteAgNBa34kLANUMNhABALDE 2017/05/11 11: 27: 18 news is: { - "item": [ +//2017/05/11 11: 27: 17 https: //api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=RB8viLdrGw91XDjX29crjt9Dz242CWL6QqXQmyYcr2Pb012ZkdzdJR17uLwqjZApTnSVZYfofLWdVMN2wmszwk_1ACT29Xev1PwnKUuZEXpoChl8U-MteAgNBa34kLANUMNhABALDE 2017/05/11 11: 27: 18 news is: { +{ "item": [ { "media_id": "e2iNEiSxCX5TV1WbFd0TQMqvVrqFDbDOacdjgQ-OAuE", "content": { @@ -27,13 +27,13 @@ "total_count": 1, "item_count": 1 } -2017/05/11 11: 27: 18 read authtoke from /tmp/wechat_hitxy_access_token2017/05/11 11: 27: 18 https: //api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=RB8viLdrGw91XDjX29crjt9Dz242CWL6QqXQmyYcr2Pb012ZkdzdJR17uLwqjZApTnSVZYfofLWdVMN2wmszwk_1ACT29Xev1PwnKUuZEXpoChl8U-MteAgNBa34kLANUMNhABALDE 2017/05/11 11: 27: 18 voice is: { - "item": [], +//2017/05/11 11: 27: 18 read authtoke from /tmp/wechat_hitxy_access_token2017/05/11 11: 27: 18 https: //api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=RB8viLdrGw91XDjX29crjt9Dz242CWL6QqXQmyYcr2Pb012ZkdzdJR17uLwqjZApTnSVZYfofLWdVMN2wmszwk_1ACT29Xev1PwnKUuZEXpoChl8U-MteAgNBa34kLANUMNhABALDE 2017/05/11 11: 27: 18 voice is: { +{ "item": [], "total_count": 0, "item_count": 0 } -2017/05/11 11: 27: 18 read authtoke from /tmp/wechat_hitxy_access_token2017/05/11 11: 27: 18 https: //api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=RB8viLdrGw91XDjX29crjt9Dz242CWL6QqXQmyYcr2Pb012ZkdzdJR17uLwqjZApTnSVZYfofLWdVMN2wmszwk_1ACT29Xev1PwnKUuZEXpoChl8U-MteAgNBa34kLANUMNhABALDE 2017/05/11 11: 27: 19 video is: { - "item": [ +//2017/05/11 11: 27: 18 read authtoke from /tmp/wechat_hitxy_access_token2017/05/11 11: 27: 18 https: //api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=RB8viLdrGw91XDjX29crjt9Dz242CWL6QqXQmyYcr2Pb012ZkdzdJR17uLwqjZApTnSVZYfofLWdVMN2wmszwk_1ACT29Xev1PwnKUuZEXpoChl8U-MteAgNBa34kLANUMNhABALDE 2017/05/11 11: 27: 19 video is: { +{ "item": [ { "media_id": "e2iNEiSxCX5TV1WbFd0TQPZtC5Oz_9VZmIKEyC7WW8w", "name": "哈工大校歌", diff --git a/sample_data/sample_news_list.json b/sample_data/sample_news_list.json index 5fef760..a79534f 100644 --- a/sample_data/sample_news_list.json +++ b/sample_data/sample_news_list.json @@ -1,5 +1,28 @@ { "item": [ + { + "media_id": "e2iNEiSxCX5TV1WbFd0TQP4_MKtjcY1q-4UPIZFrHhw", + "content": { + "news_item": [ + { + "title": "新人入会前几周推荐", + "author": "孙鹏", + "digest": "摘要", + "content": "

    新人入会,前几周推荐做如下几件事情<\/p>

<\/p>


<\/p>", + "content_source_url": "", + "thumb_media_id": "SCqYVM0F3OSBIqptPWQ5ZjDYlLvKsEtHHaYnHLyXbeM", + "show_cover_pic": 0, + "url": "http:\/\/mp.weixin.qq.com\/s?__biz=MzUxMDAzNDAwMA==&mid=100000008&idx=1&sn=cbfd3f5f5367ca67f9a1184eb394f788&chksm=790868374e7fe121ac5e1d27f6d082c3f8e8033eb4e7158615d0a69dc76ac2971e5650b28fac#rd", + "thumb_url": "http:\/\/mmbiz.qpic.cn\/mmbiz_jpg\/WiaVAicTdo7zMBVOE4QKxFFnFwbnFbyYGUs0YQBiatN97ibIgOWE4picXg49jj1k5TRibswd7DzLC1CCvJco0Kic9T90g\/0?wx_fmt=jpeg", + "need_open_comment": 0, + "only_fans_can_comment": 0 + } + ], + "create_time": 1494488254, + "update_time": 1494488268 + }, + "update_time": 1494488268 + }, { "media_id": "e2iNEiSxCX5TV1WbFd0TQMqvVrqFDbDOacdjgQ-OAuE", "content": { @@ -24,6 +47,6 @@ "update_time": 1492696243 } ], - "total_count": 1, - "item_count": 1 + "total_count": 2, + "item_count": 2 } \ No newline at end of file