From 18a3aee0786269d8eca4e802138b48a46eeae2a0 Mon Sep 17 00:00:00 2001 From: hjhee Date: Sun, 6 Oct 2024 03:24:25 +0000 Subject: [PATCH] add show nickname --- config.toml | 3 +++ fetch.go | 6 +++--- go.mod | 10 +++++++--- go.sum | 44 ++++++++++++++++++++++++++++++++++---------- main.go | 19 +++---------------- parse.go | 10 +++++++--- parse_test.go | 2 +- type.go | 28 +++++++++++++++++++++++----- 8 files changed, 81 insertions(+), 41 deletions(-) diff --git a/config.toml b/config.toml index 6b8d356..acc51b2 100644 --- a/config.toml +++ b/config.toml @@ -10,3 +10,6 @@ storeExternalResource = true # 人工处理百度安全认证 userAgent = "" cookieString = "" + +# 显示用户昵称 +showNickname = true diff --git a/fetch.go b/fetch.go index 842a7e3..9e2ef11 100644 --- a/fetch.go +++ b/fetch.go @@ -3,7 +3,7 @@ package main import ( "bufio" "fmt" - "io/ioutil" + "io" "log" "net/http" "net/url" @@ -235,7 +235,7 @@ func fetchHTMLFromURL(page *HTMLPage) error { if err != nil { return err } - bytes, err := ioutil.ReadAll(resp.Body) + bytes, err := io.ReadAll(resp.Body) if err != nil { return err } @@ -252,7 +252,7 @@ func fetchHTMLFromFile(page *HTMLPage) error { } defer in.Close() reader := bufio.NewReader(in) - bytes, err := ioutil.ReadAll(reader) + bytes, err := io.ReadAll(reader) if err != nil { return err } diff --git a/go.mod b/go.mod index cbe399b..8965a70 100644 --- a/go.mod +++ b/go.mod @@ -1,9 +1,13 @@ module github.com/hjhee/tiebaSpider -go 1.16 +go 1.23 + +toolchain go1.23.1 require ( - github.com/PuerkitoBio/goquery v1.8.0 + github.com/PuerkitoBio/goquery v1.10.0 github.com/pelletier/go-toml v1.9.5 - golang.org/x/net v0.0.0-20220909164309-bea034e7d591 // indirect + golang.org/x/net v0.30.0 ) + +require github.com/andybalholm/cascadia v1.3.2 // indirect diff --git a/go.sum b/go.sum index f2f8ebf..99f09c0 100644 --- a/go.sum +++ b/go.sum @@ -1,18 +1,42 @@ -github.com/PuerkitoBio/goquery v1.8.0 h1:PJTF7AmFCFKk1N6V6jmKfrNH9tV5pNE6lZMkG0gta/U= -github.com/PuerkitoBio/goquery v1.8.0/go.mod h1:ypIiRMtY7COPGk+I/YbZLbxsxn9g5ejnI2HSMtkjZvI= -github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c= -github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA= +github.com/PuerkitoBio/goquery v1.10.0 h1:6fiXdLuUvYs2OJSvNRqlNPoBm6YABE226xrbavY5Wv4= +github.com/PuerkitoBio/goquery v1.10.0/go.mod h1:TjZZl68Q3eGHNBA8CWaxAN7rOU1EbDz3CWuolcO5Yu4= +github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss= +github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU= github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8= github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= -golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.0.0-20220909164309-bea034e7d591 h1:D0B/7al0LLrVC8aWF4+oxpv/m8bc7ViFfVS8/gXGdqI= -golang.org/x/net v0.0.0-20220909164309-bea034e7d591/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns= +golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4= +golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/main.go b/main.go index b3fc47c..0b45eb9 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,6 @@ package main import ( "fmt" "html/template" - "io/ioutil" "log" "math/rand" "os" @@ -12,19 +11,7 @@ import ( "github.com/pelletier/go-toml" ) -var config struct { - NumFetcher int `toml:"numFetcher"` - NumParser int `toml:"numParser"` - NumRenderer int `toml:"numRenderer"` - TemplateName string `toml:"templateName"` - RetryPeriod int `toml:"retryPeriod"` - - HighResImage bool `toml:"highResImage"` - StoreExternalResource bool `toml:"storeExternalResource"` - - UserAgent string `toml:"userAgent"` - CookieString string `toml:"cookieString"` -} +var config Config var version = "debug" @@ -43,7 +30,7 @@ func init() { log.SetFlags(0) log.SetOutput(new(logWriter)) - dataStr, _ := ioutil.ReadFile("config.toml") + dataStr, _ := os.ReadFile("config.toml") err := toml.Unmarshal(dataStr, &config) if err != nil { log.Fatal(err) @@ -59,7 +46,7 @@ func init() { fmt.Fprintf(os.Stderr, "templateName: %s", config.TemplateName) - rand.Seed(time.Now().UnixNano()) + rand.Seed(41) // outputTemplate is used to render output outputTemplate = template.Must(template.New(config.TemplateName).Funcs( diff --git a/parse.go b/parse.go index 14aa172..3cf714f 100644 --- a/parse.go +++ b/parse.go @@ -160,9 +160,10 @@ func pageParserFcn(page *HTMLPage, tf *TemplateField, doc *goquery.Document, pos // get post time // Jquery过滤选择器,选择前几个元素,后几个元素,内容过滤选择器等 // http://www.cnblogs.com/alone2015/p/4962687.html - res.Time = s.Find("span.tail-info:nth-child(4)").Text() // posted from device other than PC - if res.Time == "" { - res.Time = s.Find("span.tail-info:nth-child(3)").Text() // posted from PC + for _, elem := range s.Find("span.tail-info").EachIter() { + if tm, err := time.Parse(`2006-01-02 15:04`, elem.Text()); err == nil { + res.Time = tm.Format("2006-01-02 15:04") + } } tf.Append(&res) @@ -375,6 +376,9 @@ func totalCommentParser(done <-chan struct{}, page *HTMLPage, pc *PageChannel, t comment.Index = int64(i) comment.Time = time.Unix(comment.Timestamp, 0).In(time.Local).Format("2006-01-02 15:04") comment.UserName = handleUserNameEmojiURL(comment.UserName) + if config.ShowNickName && comment.UserNickname != "" { + comment.UserName = comment.UserNickname + } comment.Content = template.HTML(reformatLzlUsername(string(comment.Content))) } // merge maps diff --git a/parse_test.go b/parse_test.go index 995bff4..68f4e54 100644 --- a/parse_test.go +++ b/parse_test.go @@ -31,7 +31,7 @@ func TestTotalCommentParserFcn(t *testing.T) { userNameHtml, _ := s.Html() // t.Errorf(userNameHtml) s.ReplaceWithHtml(userNameHtml) - t.Errorf(bodyDOM.Html()) + t.Logf(bodyDOM.Html()) } }, func(string, string, string) {}) } diff --git a/type.go b/type.go index 1cefeee..cd27183 100644 --- a/type.go +++ b/type.go @@ -9,6 +9,23 @@ import ( "sync/atomic" ) +// Config stores user specified configurations in config.toml +type Config struct { + NumFetcher int `toml:"numFetcher"` + NumParser int `toml:"numParser"` + NumRenderer int `toml:"numRenderer"` + TemplateName string `toml:"templateName"` + RetryPeriod int `toml:"retryPeriod"` + + HighResImage bool `toml:"highResImage"` + StoreExternalResource bool `toml:"storeExternalResource"` + + UserAgent string `toml:"userAgent"` + CookieString string `toml:"cookieString"` + + ShowNickName bool `toml:"showNickname"` +} + // PageChannel share HTML task between fetcher and parser type PageChannel struct { // parser get HTML pages from rec @@ -141,11 +158,12 @@ type LzlContent struct { // ThreadID uint64 `json:"thread_id,string"` // PostID uint64 `json:"post_id,string"` // CommentID uint64 `json:"comment_id,string"` - Index int64 - UserName string `json:"username"` - Content template.HTML `json:"content"` - Timestamp int64 `json:"now_time"` - Time string + Index int64 + UserName string `json:"username"` + UserNickname string `json:"show_nickname,omitempty"` + Content template.HTML `json:"content"` + Timestamp int64 `json:"now_time"` + Time string } // LzlComment indicates the relationship between a Tieba posts and the attached Lzl comment