Skip to content

Commit

Permalink
add empty test file
Browse files Browse the repository at this point in the history
  • Loading branch information
PhiFever committed Aug 11, 2024
1 parent 5258dc5 commit 553dcad
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 67 deletions.
27 changes: 14 additions & 13 deletions afdian/afdian.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ func GetAuthorId(authorName string, referer string, cookieString string) string
return authorId
}

// GetAuthorArticleUrlListByInterface 获取作者的文章列表
// GetAuthorMotionUrlList 获取作者的文章列表
// publish_sn获取的逻辑是第一轮请求为空,然后第二轮请求输入上一轮获取到的最后一篇文章的publish_sn,以此类推,直到获取到的publish_sn为空结束
func GetAuthorArticleUrlListByInterface(userName string, cookieString string, prevPublishSn string) ([]Article, string) {
func GetAuthorMotionUrlList(userName string, cookieString string, prevPublishSn string) ([]Article, string) {
userReferer := fmt.Sprintf("%s/a/%s", Host, userName)
userId := GetAuthorId(userName, userReferer, cookieString)
apiUrl := fmt.Sprintf("%s/api/post/get-list?user_id=%s&type=new&publish_sn=%s&per_page=10&group_id=&all=1&is_public=&plan_id=&title=&name=", Host, userId, prevPublishSn)
Expand All @@ -172,8 +172,8 @@ func GetAuthorArticleUrlListByInterface(userName string, cookieString string, pr
return authorArticleList, publishSn
}

// GetAlbumListByInterface 获取作者的作品集列表
func GetAlbumListByInterface(userId string, referer string, cookieString string) []Album {
// GetAlbumList 获取作者的作品集列表
func GetAlbumList(userId string, referer string, cookieString string) []Album {
apiUrl := fmt.Sprintf("%s/api/user/get-album-list?user_id=%s", Host, userId)
body := NewRequestGet(apiUrl, cookieString, referer)
//fmt.Printf("%s\n", body)
Expand All @@ -192,8 +192,8 @@ func GetAlbumListByInterface(userId string, referer string, cookieString string)
return albumList
}

// GetAlbumArticleListByInterface 获取作品集的所有文章
func GetAlbumArticleListByInterface(albumId string, authToken string) []Article {
// GetAlbumArticleList 获取作品集的所有文章
func GetAlbumArticleList(albumId string, authToken string) []Article {
//log.Println("albumId:", albumId)
postCountApiUrl := fmt.Sprintf("%s/api/user/get-album-info?album_id=%s", Host, albumId)
authTokenCookie := fmt.Sprintf("auth_token=%s", authToken)
Expand Down Expand Up @@ -223,8 +223,8 @@ func GetAlbumArticleListByInterface(albumId string, authToken string) []Article
return albumArticleList
}

// GetArticleContentByInterface 获取文章正文内容
func GetArticleContentByInterface(articleUrl string, authToken string, converter *md.Converter) string {
// GetArticleContent 获取文章正文内容
func GetArticleContent(articleUrl string, authToken string, converter *md.Converter) string {
//在album内的: https://afdian.net/api/post/get-detail?post_id={post_id}&album_id={album_id}
//在album外的: https://afdian.net/api/post/get-detail?post_id={post_id}&album_id=
log.Println("articleUrl:", articleUrl)
Expand All @@ -250,10 +250,10 @@ func GetArticleContentByInterface(articleUrl string, authToken string, converter
return markdown
}

// GetArticleCommentByInterface 获取文章评论
// GetArticleComment 获取文章评论
// TODO:根据publish_sn获取全部评论
// https://afdian.net/api/comment/get-list?post_id={post_id}&publish_sn={publish_sn}&type=old&hot=
func GetArticleCommentByInterface(articleUrl string, cookieString string) (commentString string, hotCommentString string) {
func GetArticleComment(articleUrl string, cookieString string) (commentString string, hotCommentString string) {
//https://afdian.net/api/comment/get-list?post_id={post_id}&publish_sn=&type=old&hot=1
splitUrl := strings.Split(articleUrl, "/")
postId := splitUrl[len(splitUrl)-1]
Expand Down Expand Up @@ -295,12 +295,13 @@ func getCommentString(commentJson gjson.Result) string {
}

func SaveContentIfNotExist(articleName string, filePath string, articleUrl string, authToken string, converter *md.Converter) error {
_, fileExists := utils.FileExists(filePath)
_, err := os.Stat(filePath)
fileExists := err == nil || os.IsExist(err)
log.Println("fileExists:", fileExists)
//如果文件不存在,则下载
if !fileExists {
content := GetArticleContentByInterface(articleUrl, authToken, converter)
commentString, hotCommentString := GetArticleCommentByInterface(articleUrl, authToken)
content := GetArticleContent(articleUrl, authToken, converter)
commentString, hotCommentString := GetArticleComment(articleUrl, authToken)
//Refer中需要把articleUrl中的post替换成p才能在浏览器正常访问
articleContent := "## " + articleName + "\n\n### Refer\n\n" + strings.Replace(articleUrl, "post", "p", 1) + "\n\n### 正文\n\n" + fmt.Sprintf("%s\n\n%s\n\n%s", content, hotCommentString, commentString)
//log.Println("articleContent:", articleContent)
Expand Down
150 changes: 150 additions & 0 deletions afdian/afdian_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package afdian

import (
md "github.com/JohannesKaufmann/html-to-markdown"
"github.com/stretchr/testify/assert"
"github.com/tidwall/gjson"
"testing"
)

func TestGetAuthorId(t *testing.T) {
type args struct {
authorName string
referer string
cookieString string
}
tests := []struct {
name string
args args
want string
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, GetAuthorId(tt.args.authorName, tt.args.referer, tt.args.cookieString), "GetAuthorId(%v, %v, %v)", tt.args.authorName, tt.args.referer, tt.args.cookieString)
})
}
}

func TestGetAuthorMotionUrlList(t *testing.T) {
type args struct {
userName string
cookieString string
prevPublishSn string
}
tests := []struct {
name string
args args
want []Article
want1 string
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1 := GetAuthorMotionUrlList(tt.args.userName, tt.args.cookieString, tt.args.prevPublishSn)
assert.Equalf(t, tt.want, got, "GetAuthorMotionUrlList(%v, %v, %v)", tt.args.userName, tt.args.cookieString, tt.args.prevPublishSn)
assert.Equalf(t, tt.want1, got1, "GetAuthorMotionUrlList(%v, %v, %v)", tt.args.userName, tt.args.cookieString, tt.args.prevPublishSn)
})
}
}

func TestGetAlbumList(t *testing.T) {
type args struct {
userId string
referer string
cookieString string
}
tests := []struct {
name string
args args
want []Album
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, GetAlbumList(tt.args.userId, tt.args.referer, tt.args.cookieString), "GetAlbumList(%v, %v, %v)", tt.args.userId, tt.args.referer, tt.args.cookieString)
})
}
}

func TestGetAlbumArticleList(t *testing.T) {
type args struct {
albumId string
authToken string
}
tests := []struct {
name string
args args
want []Article
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, GetAlbumArticleList(tt.args.albumId, tt.args.authToken), "GetAlbumArticleList(%v, %v)", tt.args.albumId, tt.args.authToken)
})
}
}

func TestGetArticleContent(t *testing.T) {
type args struct {
articleUrl string
authToken string
converter *md.Converter
}
tests := []struct {
name string
args args
want string
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, GetArticleContent(tt.args.articleUrl, tt.args.authToken, tt.args.converter), "GetArticleContent(%v, %v, %v)", tt.args.articleUrl, tt.args.authToken, tt.args.converter)
})
}
}

func TestGetArticleComment(t *testing.T) {
type args struct {
articleUrl string
cookieString string
}
tests := []struct {
name string
args args
wantCommentString string
wantHotCommentString string
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotCommentString, gotHotCommentString := GetArticleComment(tt.args.articleUrl, tt.args.cookieString)
assert.Equalf(t, tt.wantCommentString, gotCommentString, "GetArticleComment(%v, %v)", tt.args.articleUrl, tt.args.cookieString)
assert.Equalf(t, tt.wantHotCommentString, gotHotCommentString, "GetArticleComment(%v, %v)", tt.args.articleUrl, tt.args.cookieString)
})
}
}

func Test_getCommentString(t *testing.T) {
type args struct {
commentJson gjson.Result
}
tests := []struct {
name string
args args
want string
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, getCommentString(tt.args.commentJson), "getCommentString(%v)", tt.args.commentJson)
})
}
}
4 changes: 2 additions & 2 deletions afdian/album/album.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func GetAlbums(authorName string) error {

userId := afdian.GetAuthorId(authorName, albumHost, cookieString)
//log.Println("userId:", userId)
albumList := afdian.GetAlbumListByInterface(userId, albumHost, cookieString)
albumList := afdian.GetAlbumList(userId, albumHost, cookieString)
//log.Println("albumList:", utils.ToJSON(albumList))

authToken := afdian.GetAuthTokenCookieString(cookies)
Expand All @@ -34,7 +34,7 @@ func GetAlbums(authorName string) error {
log.Println("Find album: ", album.AlbumName)
//获取作品集的所有文章
//album.AlbumUrl会类似于 https://afdian.com/album/xyz
albumArticleList := afdian.GetAlbumArticleListByInterface(strings.Replace(album.AlbumUrl, "https://afdian.com/album/", "", -1), authToken)
albumArticleList := afdian.GetAlbumArticleList(strings.Replace(album.AlbumUrl, "https://afdian.com/album/", "", -1), authToken)
time.Sleep(time.Millisecond * time.Duration(afdian.DelayMs))

//log.Println("albumArticleList:", utils.ToJSON(albumArticleList))
Expand Down
2 changes: 1 addition & 1 deletion afdian/motion/motion.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func GetMotions(authorName string) error {
var articleList []afdian.Article
for {
//获取作者作品列表
subArticleList, publishSn := afdian.GetAuthorArticleUrlListByInterface(authorName, cookieString, prevPublishSn)
subArticleList, publishSn := afdian.GetAuthorMotionUrlList(authorName, cookieString, prevPublishSn)
articleList = append(articleList, subArticleList...)
prevPublishSn = publishSn
if publishSn == "" {
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ go 1.22

require (
github.com/JohannesKaufmann/html-to-markdown v1.6.0
github.com/carlmjohnson/requests v0.24.1
github.com/carlmjohnson/requests v0.24.2
github.com/fatih/color v1.17.0
github.com/spf13/cast v1.6.0
github.com/spf13/cast v1.7.0
github.com/stretchr/testify v1.9.0
github.com/tidwall/gjson v1.17.1
github.com/tidwall/gjson v1.17.3
github.com/urfave/cli/v2 v2.27.3
)

Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/carlmjohnson/requests v0.23.5 h1:NPANcAofwwSuC6SIMwlgmHry2V3pLrSqRiSB
github.com/carlmjohnson/requests v0.23.5/go.mod h1:zG9P28thdRnN61aD7iECFhH5iGGKX2jIjKQD9kqYH+o=
github.com/carlmjohnson/requests v0.24.1 h1:M8hmzyJr3A9D3u96MjCNuUVLd7Z3hQb7UjP5DsBp3lE=
github.com/carlmjohnson/requests v0.24.1/go.mod h1:duYA/jDnyZ6f3xbcF5PpZ9N8clgopubP2nK5i6MVMhU=
github.com/carlmjohnson/requests v0.24.2 h1:JDakhAmTIKL/qL/1P7Kkc2INGBJIkIFP6xUeUmPzLso=
github.com/carlmjohnson/requests v0.24.2/go.mod h1:duYA/jDnyZ6f3xbcF5PpZ9N8clgopubP2nK5i6MVMhU=
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -45,13 +47,17 @@ github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
github.com/spf13/cast v1.7.0 h1:ntdiHjuueXFgm5nzDRdOS4yfT43P5Fnud6DH50rz/7w=
github.com/spf13/cast v1.7.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tidwall/gjson v1.17.1 h1:wlYEnwqAHgzmhNUFfw7Xalt2JzQvsMx2Se4PcoFCT/U=
github.com/tidwall/gjson v1.17.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.17.3 h1:bwWLZU7icoKRG+C+0PNwIKC6FCJO/Q3p2pZvuP0jN94=
github.com/tidwall/gjson v1.17.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
Expand Down
5 changes: 0 additions & 5 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,6 @@ func ToJSON(JSONString interface{}) string {
return out.String()
}

func FileExists(filePath string) (os.FileInfo, bool) {
fileInfo, err := os.Stat(filePath)
return fileInfo, err == nil || os.IsExist(err)
}

// CheckAndListAuthors 通过检查当前目录下是否有二级文件夹 motion 来获取所有的作者名
// 如果有,则返回所有一级文件夹名
func CheckAndListAuthors() ([]string, error) {
Expand Down
43 changes: 0 additions & 43 deletions utils/utils_test.go

This file was deleted.

0 comments on commit 553dcad

Please sign in to comment.