-
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
661 additions
and
663 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,116 +1,116 @@ | ||
package core | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/chyroc/lark" | ||
"github.com/chyroc/lark" | ||
) | ||
|
||
type Client struct { | ||
larkClient *lark.Lark | ||
larkClient *lark.Lark | ||
} | ||
|
||
func NewClient(appID, appSecret, domain string) *Client { | ||
return &Client{ | ||
larkClient: lark.New( | ||
lark.WithAppCredential(appID, appSecret), | ||
lark.WithOpenBaseURL("https://open."+domain), | ||
), | ||
} | ||
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 | ||
} | ||
return doc, nil | ||
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 | ||
} | ||
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("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 | ||
resp, _, err := c.larkClient.Drive.DownloadDriveMedia(ctx, &lark.DownloadDriveMediaReq{ | ||
FileToken: imgToken, | ||
}) | ||
if err != nil { | ||
return imgToken, err | ||
} | ||
imgDir := ctx.Value("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 | ||
} | ||
} | ||
// data := struct { | ||
// Document *lark.DocxDocument `json:"document"` | ||
// Blocks []*lark.DocxBlock `json:"blocks"` | ||
// } { | ||
// Document: docx, | ||
// Blocks: blocks, | ||
// } | ||
// jsonData, _ := json.Marshal(data) | ||
// fmt.Println(string(jsonData)) | ||
return docx, blocks, nil | ||
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 | ||
} | ||
} | ||
// data := struct { | ||
// Document *lark.DocxDocument `json:"document"` | ||
// Blocks []*lark.DocxBlock `json:"blocks"` | ||
// } { | ||
// Document: docx, | ||
// Blocks: blocks, | ||
// } | ||
// jsonData, _ := json.Marshal(data) | ||
// fmt.Println(string(jsonData)) | ||
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 | ||
resp, _, err := c.larkClient.Drive.GetWikiNode(ctx, &lark.GetWikiNodeReq{ | ||
Token: token, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return resp.Node, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,89 @@ | ||
package core_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"testing" | ||
"context" | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/Wsine/feishu2md/core" | ||
"github.com/Wsine/feishu2md/utils" | ||
"github.com/Wsine/feishu2md/core" | ||
"github.com/Wsine/feishu2md/utils" | ||
) | ||
|
||
func getIdAndSecretFromEnv() (string, string) { | ||
utils.LoadEnv() | ||
appID := os.Getenv("FEISHU_APP_ID") | ||
appSecret := os.Getenv("FEISHU_APP_SECRET") | ||
return appID, appSecret | ||
utils.LoadEnv() | ||
appID := os.Getenv("FEISHU_APP_ID") | ||
appSecret := os.Getenv("FEISHU_APP_SECRET") | ||
return appID, appSecret | ||
} | ||
|
||
func TestNewClient(t *testing.T) { | ||
appID, appSecret := getIdAndSecretFromEnv() | ||
c := core.NewClient(appID, appSecret, "feishu.cn") | ||
if c == nil { | ||
t.Errorf("Error creating DocClient") | ||
} | ||
appID, appSecret := getIdAndSecretFromEnv() | ||
c := core.NewClient(appID, appSecret, "feishu.cn") | ||
if c == nil { | ||
t.Errorf("Error creating DocClient") | ||
} | ||
} | ||
|
||
func TestGetDocContent(t *testing.T) { | ||
appID, appSecret := getIdAndSecretFromEnv() | ||
c := core.NewClient(appID, appSecret, "feishu.cn") | ||
content, err := c.GetDocContent(context.Background(), "doccnzSnjwt7Bd01MMcnNWpwV4d") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
title := content.Title.Elements[0].TextRun.Text | ||
fmt.Println(title) | ||
if title == "" { | ||
t.Errorf("Error: parsed title is empty") | ||
} | ||
appID, appSecret := getIdAndSecretFromEnv() | ||
c := core.NewClient(appID, appSecret, "feishu.cn") | ||
content, err := c.GetDocContent(context.Background(), "doccnzSnjwt7Bd01MMcnNWpwV4d") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
title := content.Title.Elements[0].TextRun.Text | ||
fmt.Println(title) | ||
if title == "" { | ||
t.Errorf("Error: parsed title is empty") | ||
} | ||
} | ||
|
||
func TestDownloadImage(t *testing.T) { | ||
appID, appSecret := getIdAndSecretFromEnv() | ||
c := core.NewClient(appID, appSecret, "feishu.cn") | ||
imgToken := "boxcnA1QKPanfMhLxzF1eMhoArM" | ||
filename, err := c.DownloadImage( | ||
context.WithValue( | ||
context.Background(), "ImageDir", "static", | ||
), | ||
imgToken, | ||
) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if filename != "static/"+imgToken+".png" { | ||
fmt.Println(filename) | ||
t.Errorf("Error: not expected file extension") | ||
} | ||
appID, appSecret := getIdAndSecretFromEnv() | ||
c := core.NewClient(appID, appSecret, "feishu.cn") | ||
imgToken := "boxcnA1QKPanfMhLxzF1eMhoArM" | ||
filename, err := c.DownloadImage( | ||
context.WithValue( | ||
context.Background(), "ImageDir", "static", | ||
), | ||
imgToken, | ||
) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if filename != "static/"+imgToken+".png" { | ||
fmt.Println(filename) | ||
t.Errorf("Error: not expected file extension") | ||
} | ||
} | ||
|
||
func TestGetDocxContent(t *testing.T) { | ||
appID, appSecret := getIdAndSecretFromEnv() | ||
c := core.NewClient(appID, appSecret, "feishu.cn") | ||
docx, blocks, err := c.GetDocxContent(context.Background(), "doxcnXhd93zqoLnmVPGIPTy7AFe") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
fmt.Println(docx.Title) | ||
if docx.Title == "" { | ||
t.Errorf("Error: parsed title is empty") | ||
} | ||
fmt.Printf("number of blocks: %d\n", len(blocks)) | ||
if len(blocks) == 0 { | ||
t.Errorf("Error: parsed blocks are empty") | ||
} | ||
appID, appSecret := getIdAndSecretFromEnv() | ||
c := core.NewClient(appID, appSecret, "feishu.cn") | ||
docx, blocks, err := c.GetDocxContent(context.Background(), "doxcnXhd93zqoLnmVPGIPTy7AFe") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
fmt.Println(docx.Title) | ||
if docx.Title == "" { | ||
t.Errorf("Error: parsed title is empty") | ||
} | ||
fmt.Printf("number of blocks: %d\n", len(blocks)) | ||
if len(blocks) == 0 { | ||
t.Errorf("Error: parsed blocks are empty") | ||
} | ||
} | ||
|
||
func TestGetWikiNodeInfo(t *testing.T) { | ||
appID, appSecret := getIdAndSecretFromEnv() | ||
c := core.NewClient(appID, appSecret, "feishu.cn") | ||
const token = "wikcnLgRX9AMtvaB5x1cl57Yuah" | ||
node, err := c.GetWikiNodeInfo(context.Background(), token) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if node.ObjType != "docx" { | ||
t.Errorf("Error: node type incorrect") | ||
} | ||
appID, appSecret := getIdAndSecretFromEnv() | ||
c := core.NewClient(appID, appSecret, "feishu.cn") | ||
const token = "wikcnLgRX9AMtvaB5x1cl57Yuah" | ||
node, err := c.GetWikiNodeInfo(context.Background(), token) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if node.ObjType != "docx" { | ||
t.Errorf("Error: node type incorrect") | ||
} | ||
} |
Oops, something went wrong.