Skip to content

Commit

Permalink
add show nickname
Browse files Browse the repository at this point in the history
  • Loading branch information
hjhee committed Oct 6, 2024
1 parent 259207e commit 18a3aee
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 41 deletions.
3 changes: 3 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ storeExternalResource = true
# 人工处理百度安全认证
userAgent = ""
cookieString = ""

# 显示用户昵称
showNickname = true
6 changes: 3 additions & 3 deletions fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"bufio"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"net/url"
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
10 changes: 7 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
44 changes: 34 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
19 changes: 3 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"fmt"
"html/template"
"io/ioutil"
"log"
"math/rand"
"os"
Expand All @@ -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"

Expand All @@ -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)
Expand All @@ -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(
Expand Down
10 changes: 7 additions & 3 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {})
}
28 changes: 23 additions & 5 deletions type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 18a3aee

Please sign in to comment.