From cbad28f7c6e576ddad4c32038c206f98cf3d606d Mon Sep 17 00:00:00 2001 From: eson <474420502@qq.com> Date: Fri, 28 Dec 2018 03:29:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=80=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- article/article.go | 28 ++++++++++++++++++---------- article/article_test.go | 40 +++++++++++++++++++++++++++------------- info/info.go | 21 +++++++++------------ info/info_test.go | 2 +- 4 files changed, 55 insertions(+), 36 deletions(-) diff --git a/article/article.go b/article/article.go index dd802df..5ebb0de 100644 --- a/article/article.go +++ b/article/article.go @@ -2,7 +2,6 @@ package main import ( "log" - "regexp" "github.com/tidwall/gjson" @@ -39,19 +38,28 @@ func (te *TaskEx) Execute(glocal map[string]interface{}) imitater.ITask { } var adDataList []string - rule := regexp.MustCompile(`\\"label\\"\:\\"广告\\"|\\"label_style\\"\:3`) - if rule.MatchString(resp.Content()) { - if gjson.Valid(resp.Content()) { - P := gjson.Parse(resp.Content()) - gADDate := P.Get(`data.ordered_info.#.ad_data`) - if gADDate.Exists() { - for _, result := range gADDate.Array() { + + if gjson.Valid(resp.Content()) { + P := gjson.Parse(resp.Content()) + data := P.Get(`data`) + if data.Exists() { + adData := data.Get(`ordered_info.#.ad_data`) + if adData.Exists() { + for _, result := range adData.Array() { adDataList = append(adDataList, result.String()) } } - } else { - log.Println("be careful:", resp.Content()) + + relatedVideo := data.Get(`related_video_toutiao.#[show_tag = "广告"]#`) + if adData.Exists() { + for _, result := range relatedVideo.Array() { + adDataList = append(adDataList, result.String()) + } + } + } + } else { + log.Println("be careful:", resp.Content()) } if imitater.ADDataSave(te, te.db, adDataList) { diff --git a/article/article_test.go b/article/article_test.go index f320f9f..add601b 100644 --- a/article/article_test.go +++ b/article/article_test.go @@ -2,31 +2,45 @@ package main import ( "io/ioutil" - "log" - "regexp" "testing" "github.com/tidwall/gjson" ) func TestTaskEx_Execute(t *testing.T) { - rule := regexp.MustCompile(`\\"label\\"\:\\"广告\\"|\\"label_style\\"\:3`) - data, err := ioutil.ReadFile("../s.json") + data, err := ioutil.ReadFile("../ssss.json") if err != nil { panic(err) } - log.Println(rule.MatchString(string(data))) - log.Println(gjson.Valid(string(data))) - P := gjson.Parse(string(data)) - gADDate := P.Get(`data.ordered_info.#.ad_data`) - log.Println(gADDate.Exists(), gADDate.IsArray()) + var adDataList []string + if gjson.Valid(string(data)) { + P := gjson.Parse(string(data)) + data := P.Get(`data`) + if data.Exists() { + adData := data.Get(`ordered_info.#.ad_data`) + if adData.Exists() { + for _, result := range adData.Array() { + adDataList = append(adDataList, result.String()) + } + } else { + t.Error(`ordered_info.#.ad_data not exists`) + } - if !(gADDate.Exists() && gADDate.IsArray()) { - t.Error("path is error") + relatedVideo := data.Get(`related_video_toutiao.#[show_tag = "广告"]#`) + if adData.Exists() { + for _, result := range relatedVideo.Array() { + adDataList = append(adDataList, result.String()) + } + } else { + t.Error(`related_video_toutiao.#[show_tag = "广告"]# not exists`) + } + + } } - for _, result := range gADDate.Array() { - log.Println(gjson.Valid(result.Raw)) + if len(adDataList) != 2 { + t.Error("adDataList != 2") } + } diff --git a/info/info.go b/info/info.go index 747a1de..6f688b8 100644 --- a/info/info.go +++ b/info/info.go @@ -2,7 +2,6 @@ package main import ( "log" - "regexp" crontab "474420502.top/eson/crontabex" "474420502.top/eson/gjson" @@ -37,19 +36,17 @@ func (te *TaskEx) Execute(glocal map[string]interface{}) imitater.ITask { } var adDataList []string - rule := regexp.MustCompile(`\\"label\\"\:\\"广告\\"|\\"label_style\\"\:3`) - if rule.MatchString(resp.Content()) { - if gjson.Valid(resp.Content()) { - P := gjson.Parse(resp.Content()) - gADDate := P.Get(`data.#[content ~ @label_style\":3@ ]#.content`) - if gADDate.Exists() { - for _, result := range gADDate.Array() { - adDataList = append(adDataList, result.String()) - } + + if gjson.Valid(resp.Content()) { + P := gjson.Parse(resp.Content()) + gADDate := P.Get(`data.#[content ~ @"label":"广告"@ ]#.content`) + if gADDate.Exists() { + for _, result := range gADDate.Array() { + adDataList = append(adDataList, result.String()) } - } else { - log.Println("be careful:", resp.Content()) } + } else { + log.Println("be careful:", resp.Content()) } if imitater.ADDataSave(te, te.db, adDataList) { diff --git a/info/info_test.go b/info/info_test.go index e896b3a..c743771 100644 --- a/info/info_test.go +++ b/info/info_test.go @@ -19,7 +19,7 @@ func TestTaskEx_Execute(t *testing.T) { log.Println(gjson.Valid(string(data))) P := gjson.Parse(string(data)) - gADDate := P.Get(`data.#[content ~ @label_style\":3@ ]#.content`) + gADDate := P.Get(`data.#[content ~ @"label":"广告"@ ]#.content`) log.Println(gADDate.Exists(), gADDate.IsArray()) if !(gADDate.Exists() && gADDate.IsArray()) {