-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
client.go
125 lines (113 loc) · 2.88 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package core
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"github.com/Wsine/feishu2md/utils"
"github.com/chyroc/lark"
)
type Client struct {
larkClient *lark.Lark
}
func NewClient(appID, appSecret, domain string) *Client {
return &Client{
larkClient: lark.New(
lark.WithAppCredential(appID, appSecret),
lark.WithOpenBaseURL("https://open."+domain),
),
}
}
func (c *Client) GetDocContent(ctx context.Context, docToken string) (*lark.DocContent, error) {
resp, _, err := c.larkClient.Drive.GetDriveDocContent(ctx, &lark.GetDriveDocContentReq{
DocToken: docToken,
})
if err != nil {
return nil, err
}
doc := &lark.DocContent{}
err = json.Unmarshal([]byte(resp.Content), doc)
if err != nil {
return doc, err
}
if ctx.Value("Verbose").(bool) {
fmt.Println(utils.PrettyPrint(doc))
}
return doc, nil
}
func (c *Client) DownloadImage(ctx context.Context, imgToken string) (string, error) {
resp, _, err := c.larkClient.Drive.DownloadDriveMedia(ctx, &lark.DownloadDriveMediaReq{
FileToken: imgToken,
})
if err != nil {
return imgToken, err
}
imgDir := ctx.Value("OutputConfig").(OutputConfig).ImageDir
fileext := filepath.Ext(resp.Filename)
filename := fmt.Sprintf("%s/%s%s", imgDir, imgToken, fileext)
err = os.MkdirAll(filepath.Dir(filename), 0o755)
if err != nil {
return imgToken, err
}
file, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0o666)
if err != nil {
return imgToken, err
}
defer file.Close()
_, err = io.Copy(file, resp.File)
if err != nil {
return imgToken, err
}
return filename, nil
}
func (c *Client) GetDocxContent(ctx context.Context, docToken string) (*lark.DocxDocument, []*lark.DocxBlock, error) {
resp, _, err := c.larkClient.Drive.GetDocxDocument(ctx, &lark.GetDocxDocumentReq{
DocumentID: docToken,
})
if err != nil {
return nil, nil, err
}
docx := &lark.DocxDocument{
DocumentID: resp.Document.DocumentID,
RevisionID: resp.Document.RevisionID,
Title: resp.Document.Title,
}
var blocks []*lark.DocxBlock
var pageToken *string
for {
resp2, _, err := c.larkClient.Drive.GetDocxBlockListOfDocument(ctx, &lark.GetDocxBlockListOfDocumentReq{
DocumentID: docx.DocumentID,
PageToken: pageToken,
})
if err != nil {
return docx, nil, err
}
blocks = append(blocks, resp2.Items...)
pageToken = &resp2.PageToken
if !resp2.HasMore {
break
}
}
if ctx.Value("Verbose").(bool) {
data := struct {
Document *lark.DocxDocument `json:"document"`
Blocks []*lark.DocxBlock `json:"blocks"`
}{
Document: docx,
Blocks: blocks,
}
fmt.Println(utils.PrettyPrint(data))
}
return docx, blocks, nil
}
func (c *Client) GetWikiNodeInfo(ctx context.Context, token string) (*lark.GetWikiNodeRespNode, error) {
resp, _, err := c.larkClient.Drive.GetWikiNode(ctx, &lark.GetWikiNodeReq{
Token: token,
})
if err != nil {
return nil, err
}
return resp.Node, nil
}