From f9c19d026ef3948c2b3e1aa4f21b194c6335fe2e Mon Sep 17 00:00:00 2001 From: wedojava Date: Tue, 29 Sep 2020 19:42:48 +0800 Subject: [PATCH] add cna link fetcher and pass test. --- internal/fetcher/links.go | 6 ++++++ internal/fetcher/links_test.go | 23 +++++++++++++++-------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/internal/fetcher/links.go b/internal/fetcher/links.go index 7f392d4..fa0b8dc 100644 --- a/internal/fetcher/links.go +++ b/internal/fetcher/links.go @@ -42,6 +42,12 @@ func (f *Fetcher) SetLinks() error { f.Links = append(append(append(newsWorld, newsChina...), realtimeWorld...), realtimeChina...) case "news.ltn.com.tw": f.Links = LinksFilter(links, `https://news.*/news/.*`) + case "www.cna.com.tw": + newsFirst := LinksFilter(links, `.*?/news/firstnews/.*`) + newsWorld := LinksFilter(links, `.*?/news/aopl/.*`) + newsPolitical := LinksFilter(links, `.*?/news/aipl/.*`) + newsTW := LinksFilter(links, `.*?/news/acn/.*`) + f.Links = append(append(append(newsFirst, newsWorld...), newsPolitical...), newsTW...) } return nil } diff --git a/internal/fetcher/links_test.go b/internal/fetcher/links_test.go index 3d18619..822e6a8 100644 --- a/internal/fetcher/links_test.go +++ b/internal/fetcher/links_test.go @@ -17,7 +17,20 @@ func TestKickOutLinksMatchPath(t *testing.T) { } func TestSetLinks(t *testing.T) { - u, err := url.Parse("https://news.ltn.com.tw/list/breakingnews") + // u, err := url.Parse("https://news.ltn.com.tw/list/breakingnews") + // assertLinks := []string{ + // "https://news.ltn.com.tw/news/society/breakingnews/3278253", + // "https://news.ltn.com.tw/news/society/breakingnews/3278250", + // "https://news.ltn.com.tw/news/politics/breakingnews/3278225", + // "https://news.ltn.com.tw/news/politics/breakingnews/3278170", + // } + u, err := url.Parse("https://www.cna.com.tw/list/aall.aspx") + assertLinks := []string{ + "https://www.cna.com.tw/news/aopl/202009290075.aspx", + "https://www.cna.com.tw/news/firstnews/202009290051.aspx", + "https://www.cna.com.tw/news/acn/202009290063.aspx", + "https://www.cna.com.tw/news/aipl/202009290055.aspx", + } if err != nil { t.Errorf("Url Parse fail!\n%s", err) } @@ -25,12 +38,6 @@ func TestSetLinks(t *testing.T) { Entrance: u, } f.SetLinks() - assertLinks := []string{ - "https://news.ltn.com.tw/news/society/breakingnews/3278253", - "https://news.ltn.com.tw/news/society/breakingnews/3278250", - "https://news.ltn.com.tw/news/politics/breakingnews/3278225", - "https://news.ltn.com.tw/news/politics/breakingnews/3278170", - } shot := 0 for _, link := range f.Links { for _, v := range assertLinks { @@ -39,7 +46,7 @@ func TestSetLinks(t *testing.T) { } } } - if shot == 0 { + if shot != len(assertLinks) { t.Errorf("want: %v, got: %v", len(assertLinks), shot) } }